File size: 1,556 Bytes
b665c14 537ad90 b665c14 537ad90 b665c14 537ad90 b665c14 537ad90 b665c14 537ad90 b665c14 537ad90 b665c14 537ad90 b665c14 537ad90 b665c14 537ad90 b665c14 537ad90 b665c14 537ad90 | 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import os
import gradio as gr
from groq import Groq
# Get API key from Hugging Face Secrets
GROQ_API_KEY = os.environ.get("Keypass")
if GROQ_API_KEY is None:
raise ValueError("GROQ_API_KEY not found. Add it in Hugging Face โ Settings โ Secrets.")
client = Groq(api_key=GROQ_API_KEY)
# Chat function
def chat(message, history):
try:
messages = []
# Add previous conversation
for user, bot in history:
messages.append({"role": "user", "content": user})
messages.append({"role": "assistant", "content": bot})
# Add current message
messages.append({"role": "user", "content": message})
response = client.chat.completions.create(
model="llama3-70b-8192",
messages=messages,
temperature=0.7,
max_tokens=512,
)
reply = response.choices[0].message.content
return reply
except Exception as e:
return f"Error: {str(e)}"
# Gradio UI (Stable Version)
with gr.Blocks() as demo:
gr.Markdown("## ๐ Groq AI Chatbot")
gr.Markdown("Simple chatbot using Groq API + Gradio")
chatbot = gr.Chatbot()
msg = gr.Textbox(label="Your Message")
clear = gr.Button("Clear Chat")
def respond(message, chat_history):
bot_message = chat(message, chat_history)
chat_history.append((message, bot_message))
return "", chat_history
msg.submit(respond, [msg, chatbot], [msg, chatbot])
clear.click(lambda: None, None, chatbot, queue=False)
demo.launch() |