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