github-actions commited on
Commit
aada87f
·
0 Parent(s):

Automated sync from GitHub Actions

Browse files
Files changed (4) hide show
  1. .github/workflows/sync_to_hf_space.yml +66 -0
  2. README.md +9 -0
  3. app.py +111 -0
  4. requirements.txt +7 -0
.github/workflows/sync_to_hf_space.yml ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Sync to Hugging Face Space
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main # Or whichever branch you want to trigger deployment from
7
+
8
+ jobs:
9
+ sync-to-hub:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ # Step 1: Check out the code from the GitHub repository
13
+ - name: Checkout repository
14
+ uses: actions/checkout@v4
15
+ with:
16
+ fetch-depth: 0 # Fetch all history for proper syncing
17
+
18
+ # Step 2: Setup Git LFS
19
+ - name: Setup Git LFS
20
+ run: |
21
+ git lfs install
22
+
23
+ # Step 3: Configure Git
24
+ - name: Configure Git
25
+ run: |
26
+ git config --global user.name "github-actions"
27
+ git config --global user.email "[email protected]"
28
+
29
+ # Step 4: List files to confirm what's available
30
+ - name: List files
31
+ run: |
32
+ ls -la
33
+
34
+ # Step 5: Create and push to Hugging Face Space
35
+ - name: Push to Hugging Face Space
36
+ env:
37
+ HF_TOKEN: ${{ secrets.HF_TOKEN_WRITE }}
38
+ run: |
39
+ # Clean any existing hf_repo directory
40
+ rm -rf hf_repo
41
+
42
+ # Create a new directory for the HF repo
43
+ mkdir -p hf_repo
44
+
45
+ # Copy specific files to the HF repo
46
+ cp -r README.md app.py requirements.txt .github hf_repo/
47
+
48
+ # Initialize git in the new directory
49
+ cd hf_repo
50
+ git init
51
+ git branch -m main
52
+
53
+ # Show what was copied
54
+ ls -la
55
+
56
+ # Set up the remote
57
+ git remote add origin https://USER:[email protected]/spaces/sikeaditya/LLM-Cloud-Demo
58
+
59
+ # Add all files
60
+ git add .
61
+
62
+ # Commit files
63
+ git commit -m "Automated sync from GitHub Actions"
64
+
65
+ # Force push to HF Space
66
+ git push -f origin main
README.md ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: LLM-Cloud-Demo
3
+ sdk: gradio
4
+ emoji: ⚡
5
+ colorFrom: yellow
6
+ colorTo: red
7
+ ---
8
+
9
+ # LLM-Cloud-Demo
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+ import json # Import json for better error message handling
5
+
6
+ # --- Configuration ---
7
+ # Using the specific Nebius/HF router URL from your snippet
8
+ API_URL = "https://router.huggingface.co/nebius/v1/chat/completions"
9
+ # Using the model from your snippet
10
+ MODEL_ID = "google/gemma-3-27b-it-fast"
11
+ # Get Hugging Face token from environment variable/secrets
12
+ HF_TOKEN = os.getenv("HF_TOKEN")
13
+
14
+ if not HF_TOKEN:
15
+ raise ValueError("Hugging Face token not found. Please set the HF_TOKEN environment variable or secret.")
16
+
17
+ HEADERS = {"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"}
18
+
19
+ # --- No Local Model Loading Needed ---
20
+ print(f"Application configured to use Hugging Face Inference API.")
21
+ print(f"Target Model: AgriAssist_LLM")
22
+ print(f"API Endpoint: {API_URL}")
23
+
24
+ # --- Inference Function (Using Hugging Face API) ---
25
+ def generate_response(prompt, max_new_tokens=512): # Using max_tokens from your snippet
26
+ print(f"Received prompt: {prompt}")
27
+ print("Preparing payload for API...")
28
+
29
+ # Construct the payload based on the API requirements
30
+ # NOTE: This version assumes text-only input matching the Gradio interface.
31
+ # To handle image input like your snippet, the Gradio interface
32
+ # and payload structure would need modification.
33
+ payload = {
34
+ "messages": [
35
+ {
36
+ "role": "user",
37
+ "content": prompt
38
+ # Example for multimodal if Gradio input changes:
39
+ # "content": [
40
+ # {"type": "text", "text": prompt},
41
+ # {"type": "image_url", "image_url": {"url": "some_image_url.jpg"}}
42
+ # ]
43
+ }
44
+ ],
45
+ "model": MODEL_ID,
46
+ "max_tokens": max_new_tokens,
47
+ # Optional parameters you might want to add:
48
+ # "temperature": 0.7,
49
+ # "top_p": 0.9,
50
+ # "stream": False # Set to True for streaming responses if API supports it
51
+ }
52
+
53
+ print(f"Sending request to API for model AgriAssist_LLM...")
54
+ try:
55
+ # Make the POST request
56
+ response = requests.post(API_URL, headers=HEADERS, json=payload)
57
+
58
+ # Raise an exception for bad status codes (like 4xx or 5xx)
59
+ response.raise_for_status()
60
+
61
+ # Parse the JSON response
62
+ result = response.json()
63
+ print("API Response Received Successfully.")
64
+
65
+ # Extract the generated text - Structure matches your snippet's expectation
66
+ if "choices" in result and len(result["choices"]) > 0 and "message" in result["choices"][0] and "content" in result["choices"][0]["message"]:
67
+ api_response_content = result["choices"][0]["message"]["content"]
68
+ print(f"API generated content: {api_response_content}")
69
+ return api_response_content
70
+ else:
71
+ # Handle unexpected response structure
72
+ print(f"Unexpected API response structure: {result}")
73
+ return f"Error: Unexpected API response structure. Full response: {json.dumps(result)}"
74
+
75
+ except requests.exceptions.RequestException as e:
76
+ # Handle network errors, timeout errors, invalid responses, etc.
77
+ error_message = f"Error calling Hugging Face API: {e}"
78
+ # Try to get more details from the response body if it exists
79
+ error_detail = ""
80
+ if e.response is not None:
81
+ try:
82
+ error_detail = e.response.json() # Try parsing JSON error
83
+ except json.JSONDecodeError:
84
+ error_detail = e.response.text # Fallback to raw text
85
+ print(f"{error_message}\nResponse details: {error_detail}")
86
+ return f"{error_message}\nDetails: {error_detail}"
87
+
88
+ except Exception as e:
89
+ # Handle other potential errors during processing
90
+ print(f"An unexpected error occurred: {e}")
91
+ return f"An unexpected error occurred: {e}"
92
+
93
+ # --- Gradio Interface ---
94
+ iface = gr.Interface(
95
+ fn=generate_response,
96
+ inputs=gr.Textbox(lines=5, label="Enter your prompt", placeholder="Type your question or instruction here..."),
97
+ outputs=gr.Textbox(lines=8, label=f"AgriAssist_LLM Says (via API):"), # Updated label
98
+ title=f"Chat with AgriAssist_LLM via Inference API", # Updated title
99
+ description=("This demo sends your text to a remote server for processing."), # Updated description
100
+ allow_flagging="never",
101
+ examples=[ # Examples should still be relevant
102
+ ["Explain the concept of cloud computing in simple terms."],
103
+ ["Write Python code to list files in a directory."],
104
+ ["What are the main benefits of using Generative AI?"],
105
+ ["Translate 'Cloud computing offers scalability' to German."],
106
+ ]
107
+ )
108
+
109
+ # --- Launch the App ---
110
+ # You can add share=True if you want to create a temporary public link (use with caution)
111
+ iface.launch(server_name="0.0.0.0", server_port=7860) # Makes it accessible in Codespaces/docker
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ transformers>=4.38.0 # Or a recent version supporting Gemma
2
+ accelerate
3
+ bitsandbytes # Often needed for efficient loading
4
+ torch
5
+ gradio
6
+ # Add sentencepiece if not included by transformers
7
+ sentencepiece