Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
from groq import Groq
|
|
@@ -7,49 +6,23 @@ from groq import Groq
|
|
| 7 |
# 1. Setup
|
| 8 |
# =========================
|
| 9 |
|
| 10 |
-
# Get API key from environment variables (set in Hugging Face Spaces settings)
|
| 11 |
groq_api_key = os.environ.get("GROQ_API_KEY")
|
| 12 |
-
|
| 13 |
if not groq_api_key:
|
| 14 |
-
raise ValueError(
|
| 15 |
-
"GROQ_API_KEY not found. Please set it in Hugging Face Spaces → Settings → Repository Secrets."
|
| 16 |
-
)
|
| 17 |
|
| 18 |
client = Groq(api_key=groq_api_key)
|
| 19 |
-
|
| 20 |
MODEL_NAME = "llama-3.3-70b-versatile"
|
| 21 |
|
| 22 |
-
|
| 23 |
# =========================
|
| 24 |
# 2. Chat Function
|
| 25 |
# =========================
|
| 26 |
|
| 27 |
def chat_with_groq(message, history):
|
| 28 |
-
"""
|
| 29 |
-
Gradio 6 passes history as OpenAI-style messages:
|
| 30 |
-
[
|
| 31 |
-
{"role": "user", "content": "..."},
|
| 32 |
-
{"role": "assistant", "content": "..."}
|
| 33 |
-
]
|
| 34 |
-
"""
|
| 35 |
-
|
| 36 |
-
messages = [
|
| 37 |
-
{
|
| 38 |
-
"role": "system",
|
| 39 |
-
"content": "You are a helpful, clear, and practical AI assistant."
|
| 40 |
-
}
|
| 41 |
-
]
|
| 42 |
-
|
| 43 |
-
# Add previous conversation history
|
| 44 |
if history:
|
| 45 |
for item in history:
|
| 46 |
-
role
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
if role in ["user", "assistant"] and isinstance(content, str):
|
| 50 |
-
messages.append({"role": role, "content": content})
|
| 51 |
-
|
| 52 |
-
# Add latest user message
|
| 53 |
messages.append({"role": "user", "content": message})
|
| 54 |
|
| 55 |
response = client.chat.completions.create(
|
|
@@ -58,10 +31,8 @@ def chat_with_groq(message, history):
|
|
| 58 |
temperature=0.7,
|
| 59 |
max_completion_tokens=1024,
|
| 60 |
)
|
| 61 |
-
|
| 62 |
return response.choices[0].message.content
|
| 63 |
|
| 64 |
-
|
| 65 |
# =========================
|
| 66 |
# 3. Gradio App
|
| 67 |
# =========================
|
|
@@ -80,15 +51,15 @@ demo = gr.ChatInterface(
|
|
| 80 |
fill_width=False,
|
| 81 |
)
|
| 82 |
|
| 83 |
-
|
| 84 |
# =========================
|
| 85 |
# 4. Launch
|
| 86 |
# =========================
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
demo.launch(
|
| 90 |
-
server_name="0.0.0.0",
|
| 91 |
server_port=7860,
|
| 92 |
-
theme=gr.themes.Soft(),
|
| 93 |
debug=True
|
|
|
|
|
|
|
| 94 |
)
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
|
|
|
| 6 |
# 1. Setup
|
| 7 |
# =========================
|
| 8 |
|
|
|
|
| 9 |
groq_api_key = os.environ.get("GROQ_API_KEY")
|
|
|
|
| 10 |
if not groq_api_key:
|
| 11 |
+
raise ValueError("GROQ_API_KEY not found. Please set it in Hugging Face Spaces → Settings → Repository Secrets.")
|
|
|
|
|
|
|
| 12 |
|
| 13 |
client = Groq(api_key=groq_api_key)
|
|
|
|
| 14 |
MODEL_NAME = "llama-3.3-70b-versatile"
|
| 15 |
|
|
|
|
| 16 |
# =========================
|
| 17 |
# 2. Chat Function
|
| 18 |
# =========================
|
| 19 |
|
| 20 |
def chat_with_groq(message, history):
|
| 21 |
+
messages = [{"role": "system", "content": "You are a helpful, clear, and practical AI assistant."}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
if history:
|
| 23 |
for item in history:
|
| 24 |
+
if item.get("role") in ["user", "assistant"] and isinstance(item.get("content"), str):
|
| 25 |
+
messages.append({"role": item["role"], "content": item["content"]})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
response = client.chat.completions.create(
|
|
|
|
| 31 |
temperature=0.7,
|
| 32 |
max_completion_tokens=1024,
|
| 33 |
)
|
|
|
|
| 34 |
return response.choices[0].message.content
|
| 35 |
|
|
|
|
| 36 |
# =========================
|
| 37 |
# 3. Gradio App
|
| 38 |
# =========================
|
|
|
|
| 51 |
fill_width=False,
|
| 52 |
)
|
| 53 |
|
|
|
|
| 54 |
# =========================
|
| 55 |
# 4. Launch
|
| 56 |
# =========================
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
demo.launch(
|
| 60 |
+
server_name="0.0.0.0",
|
| 61 |
server_port=7860,
|
|
|
|
| 62 |
debug=True
|
| 63 |
+
# Remove theme if Gradio < 6
|
| 64 |
+
# theme=gr.themes.Soft()
|
| 65 |
)
|