Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,39 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from groq import Groq
|
| 3 |
|
| 4 |
-
# === API
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
# === Initialize Groq client ===
|
| 9 |
client = Groq(api_key=GROQ_API_KEY)
|
| 10 |
|
| 11 |
-
# ===
|
| 12 |
def chat_with_groq(message, history):
|
| 13 |
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 14 |
for user_msg, bot_msg in history:
|
| 15 |
messages.append({"role": "user", "content": user_msg})
|
| 16 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 17 |
messages.append({"role": "user", "content": message})
|
| 18 |
-
|
| 19 |
try:
|
| 20 |
response = client.chat.completions.create(
|
| 21 |
-
model="llama3-8b-8192", # ✅
|
| 22 |
messages=messages,
|
| 23 |
temperature=0.7,
|
| 24 |
)
|
| 25 |
reply = response.choices[0].message.content
|
| 26 |
except Exception as e:
|
| 27 |
reply = f"Error: {e}"
|
| 28 |
-
|
| 29 |
return reply
|
| 30 |
|
| 31 |
-
# === Gradio
|
| 32 |
chatbot = gr.ChatInterface(fn=chat_with_groq, title="Groq Chatbot")
|
| 33 |
|
| 34 |
-
# ===
|
| 35 |
if __name__ == "__main__":
|
| 36 |
chatbot.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
+
# === Load API Key from environment ===
|
| 6 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
+
if not GROQ_API_KEY:
|
| 8 |
+
raise ValueError("Missing GROQ_API_KEY. Please set it in the Hugging Face Space 'Secrets'.")
|
| 9 |
|
| 10 |
# === Initialize Groq client ===
|
| 11 |
client = Groq(api_key=GROQ_API_KEY)
|
| 12 |
|
| 13 |
+
# === Chat function ===
|
| 14 |
def chat_with_groq(message, history):
|
| 15 |
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 16 |
for user_msg, bot_msg in history:
|
| 17 |
messages.append({"role": "user", "content": user_msg})
|
| 18 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 19 |
messages.append({"role": "user", "content": message})
|
| 20 |
+
|
| 21 |
try:
|
| 22 |
response = client.chat.completions.create(
|
| 23 |
+
model="llama3-8b-8192", # ✅ currently supported
|
| 24 |
messages=messages,
|
| 25 |
temperature=0.7,
|
| 26 |
)
|
| 27 |
reply = response.choices[0].message.content
|
| 28 |
except Exception as e:
|
| 29 |
reply = f"Error: {e}"
|
| 30 |
+
|
| 31 |
return reply
|
| 32 |
|
| 33 |
+
# === Gradio UI ===
|
| 34 |
chatbot = gr.ChatInterface(fn=chat_with_groq, title="Groq Chatbot")
|
| 35 |
|
| 36 |
+
# === Run app ===
|
| 37 |
if __name__ == "__main__":
|
| 38 |
chatbot.launch()
|
| 39 |
+
|