Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,27 +2,56 @@ import os
|
|
| 2 |
from groq import Groq
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
#
|
| 6 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def chatbot(message, history):
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
messages.append({"role": "user", "content":
|
| 13 |
-
messages.append({"role": "assistant", "content": bot})
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
model="llama-3.3-70b-versatile",
|
| 20 |
-
)
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
fn=chatbot,
|
| 26 |
title="🚀 Groq AI Chatbot",
|
| 27 |
-
description="Fast chatbot
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from groq import Groq
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
# Initialize Groq client using HF secret
|
| 6 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 7 |
|
| 8 |
+
# System prompt (bot personality)
|
| 9 |
+
SYSTEM_PROMPT = {
|
| 10 |
+
"role": "system",
|
| 11 |
+
"content": "You are a helpful, friendly, and intelligent AI assistant. Answer clearly and concisely."
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
# Chatbot function
|
| 15 |
def chatbot(message, history):
|
| 16 |
+
try:
|
| 17 |
+
messages = [SYSTEM_PROMPT]
|
| 18 |
+
|
| 19 |
+
# Limit history to avoid token overflow
|
| 20 |
+
history = history[-5:] if history else []
|
| 21 |
+
|
| 22 |
+
# Convert Gradio history → Groq format
|
| 23 |
+
for user_msg, bot_msg in history:
|
| 24 |
+
if user_msg:
|
| 25 |
+
messages.append({"role": "user", "content": user_msg})
|
| 26 |
+
if bot_msg:
|
| 27 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 28 |
|
| 29 |
+
# Add current message
|
| 30 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
| 31 |
|
| 32 |
+
# API call
|
| 33 |
+
response = client.chat.completions.create(
|
| 34 |
+
model="llama-3.3-70b-versatile",
|
| 35 |
+
messages=messages,
|
| 36 |
+
temperature=0.7, # creativity
|
| 37 |
+
max_tokens=1024, # response length
|
| 38 |
+
)
|
| 39 |
|
| 40 |
+
reply = response.choices[0].message.content.strip()
|
| 41 |
+
return reply
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return f"⚠️ Error: {str(e)}"
|
| 45 |
|
| 46 |
+
|
| 47 |
+
# UI
|
| 48 |
+
demo = gr.ChatInterface(
|
| 49 |
fn=chatbot,
|
| 50 |
title="🚀 Groq AI Chatbot",
|
| 51 |
+
description="Fast, smart chatbot powered by Groq",
|
| 52 |
+
theme="soft",
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Launch (important for Hugging Face)
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
demo.launch()
|