Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -6,25 +6,26 @@ import requests
|
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
# ----------------------------------------------------------------------
|
| 9 |
-
#
|
| 10 |
# ----------------------------------------------------------------------
|
| 11 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 12 |
GROQ_ENDPOINT = "https://api.groq.com/openai/v1/chat/completions"
|
| 13 |
MODEL_NAME = os.environ.get("MODEL_NAME", "llama-3.1-8b-instant")
|
| 14 |
|
| 15 |
# ----------------------------------------------------------------------
|
| 16 |
-
# CSS
|
| 17 |
# ----------------------------------------------------------------------
|
| 18 |
CSS = """
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
}
|
| 22 |
-
#business-input textarea:focus {
|
| 23 |
-
border-color: #1E90FF;
|
| 24 |
}
|
| 25 |
#generate-btn {
|
| 26 |
background-color: #1E90FF;
|
| 27 |
color: white;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
}
|
| 29 |
#generate-btn:hover {
|
| 30 |
background-color: #1C86EE;
|
|
@@ -32,16 +33,27 @@ CSS = """
|
|
| 32 |
"""
|
| 33 |
|
| 34 |
# ----------------------------------------------------------------------
|
| 35 |
-
#
|
| 36 |
# ----------------------------------------------------------------------
|
| 37 |
-
def
|
| 38 |
"""
|
| 39 |
-
|
| 40 |
"""
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
# ----------------------------------------------------------------------
|
| 9 |
+
# Configuration
|
| 10 |
# ----------------------------------------------------------------------
|
| 11 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 12 |
GROQ_ENDPOINT = "https://api.groq.com/openai/v1/chat/completions"
|
| 13 |
MODEL_NAME = os.environ.get("MODEL_NAME", "llama-3.1-8b-instant")
|
| 14 |
|
| 15 |
# ----------------------------------------------------------------------
|
| 16 |
+
# CSS (passed to demo.launch)
|
| 17 |
# ----------------------------------------------------------------------
|
| 18 |
CSS = """
|
| 19 |
+
#chatbot .message {
|
| 20 |
+
font-family: Arial, Helvetica, sans-serif;
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
#generate-btn {
|
| 23 |
background-color: #1E90FF;
|
| 24 |
color: white;
|
| 25 |
+
border: none;
|
| 26 |
+
padding: 0.5rem 1rem;
|
| 27 |
+
font-size: 1rem;
|
| 28 |
+
cursor: pointer;
|
| 29 |
}
|
| 30 |
#generate-btn:hover {
|
| 31 |
background-color: #1C86EE;
|
|
|
|
| 33 |
"""
|
| 34 |
|
| 35 |
# ----------------------------------------------------------------------
|
| 36 |
+
# Helper to call Groq API
|
| 37 |
# ----------------------------------------------------------------------
|
| 38 |
+
def call_groq(messages):
|
| 39 |
"""
|
| 40 |
+
Sends a chat completion request to Groq and returns the assistant's reply.
|
| 41 |
"""
|
| 42 |
+
if not GROQ_API_KEY:
|
| 43 |
+
raise RuntimeError("GROQ_API_KEY not set in environment.")
|
| 44 |
+
headers = {
|
| 45 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 46 |
+
"Content-Type": "application/json"
|
| 47 |
+
}
|
| 48 |
+
payload = {
|
| 49 |
+
"model": MODEL_NAME,
|
| 50 |
+
"messages": messages,
|
| 51 |
+
"temperature": 0.7
|
| 52 |
+
}
|
| 53 |
+
response = requests.post(GROQ_ENDPOINT, headers=headers, json=payload, timeout=30)
|
| 54 |
+
response.raise_for_status()
|
| 55 |
+
data = response.json()
|
| 56 |
+
# Extract the assistant's content
|
| 57 |
+
content = data["choices"][0]["message"]["content"]
|
| 58 |
+
# Some models wrap the answer in markdown code fences; clean them
|
| 59 |
+
if content.startswith("
|