Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,43 +2,49 @@ import os
|
|
| 2 |
from groq import Groq
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
# Initialize
|
| 6 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 7 |
|
| 8 |
-
# System prompt
|
| 9 |
SYSTEM_PROMPT = {
|
| 10 |
"role": "system",
|
| 11 |
-
"content": "You are a helpful, friendly
|
| 12 |
}
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Chatbot function
|
| 15 |
def chatbot(message, history):
|
| 16 |
try:
|
| 17 |
messages = [SYSTEM_PROMPT]
|
| 18 |
|
| 19 |
-
# Limit history
|
| 20 |
history = history[-6:] if history else []
|
| 21 |
|
| 22 |
-
# Handle BOTH formats safely
|
| 23 |
for item in history:
|
| 24 |
-
# Case 1: (user, bot)
|
| 25 |
if isinstance(item, (list, tuple)) and len(item) == 2:
|
| 26 |
user_msg, bot_msg = item
|
| 27 |
|
| 28 |
if user_msg:
|
| 29 |
-
messages.append({"role": "user", "content": user_msg})
|
| 30 |
if bot_msg:
|
| 31 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
| 32 |
|
| 33 |
-
# Case 2: dict
|
| 34 |
elif isinstance(item, dict):
|
| 35 |
if "role" in item and "content" in item:
|
| 36 |
-
messages.append(item)
|
| 37 |
|
| 38 |
# Add current message
|
| 39 |
-
messages.append({"role": "user", "content": message})
|
| 40 |
|
| 41 |
-
# Call Groq
|
| 42 |
response = client.chat.completions.create(
|
| 43 |
model="llama-3.3-70b-versatile",
|
| 44 |
messages=messages,
|
|
@@ -46,20 +52,19 @@ def chatbot(message, history):
|
|
| 46 |
max_tokens=1024,
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
return reply
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
return f"⚠️ Error: {str(e)}"
|
| 54 |
|
| 55 |
|
| 56 |
-
#
|
| 57 |
demo = gr.ChatInterface(
|
| 58 |
fn=chatbot,
|
| 59 |
title="🚀 Groq AI Chatbot",
|
| 60 |
description="Fast chatbot powered by Groq",
|
| 61 |
)
|
| 62 |
|
| 63 |
-
# Launch
|
| 64 |
if __name__ == "__main__":
|
| 65 |
demo.launch()
|
|
|
|
| 2 |
from groq import Groq
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
# Initialize client
|
| 6 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 7 |
|
| 8 |
+
# System prompt
|
| 9 |
SYSTEM_PROMPT = {
|
| 10 |
"role": "system",
|
| 11 |
+
"content": "You are a helpful, friendly AI assistant."
|
| 12 |
}
|
| 13 |
|
| 14 |
+
# Clean message (IMPORTANT FIX)
|
| 15 |
+
def clean_message(msg):
|
| 16 |
+
return {
|
| 17 |
+
"role": msg.get("role"),
|
| 18 |
+
"content": msg.get("content")
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
# Chatbot function
|
| 22 |
def chatbot(message, history):
|
| 23 |
try:
|
| 24 |
messages = [SYSTEM_PROMPT]
|
| 25 |
|
| 26 |
+
# Limit history
|
| 27 |
history = history[-6:] if history else []
|
| 28 |
|
|
|
|
| 29 |
for item in history:
|
| 30 |
+
# Case 1: tuple (user, bot)
|
| 31 |
if isinstance(item, (list, tuple)) and len(item) == 2:
|
| 32 |
user_msg, bot_msg = item
|
| 33 |
|
| 34 |
if user_msg:
|
| 35 |
+
messages.append({"role": "user", "content": str(user_msg)})
|
| 36 |
if bot_msg:
|
| 37 |
+
messages.append({"role": "assistant", "content": str(bot_msg)})
|
| 38 |
|
| 39 |
+
# Case 2: dict (REMOVE metadata)
|
| 40 |
elif isinstance(item, dict):
|
| 41 |
if "role" in item and "content" in item:
|
| 42 |
+
messages.append(clean_message(item))
|
| 43 |
|
| 44 |
# Add current message
|
| 45 |
+
messages.append({"role": "user", "content": str(message)})
|
| 46 |
|
| 47 |
+
# Call Groq
|
| 48 |
response = client.chat.completions.create(
|
| 49 |
model="llama-3.3-70b-versatile",
|
| 50 |
messages=messages,
|
|
|
|
| 52 |
max_tokens=1024,
|
| 53 |
)
|
| 54 |
|
| 55 |
+
return response.choices[0].message.content.strip()
|
|
|
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
return f"⚠️ Error: {str(e)}"
|
| 59 |
|
| 60 |
|
| 61 |
+
# UI
|
| 62 |
demo = gr.ChatInterface(
|
| 63 |
fn=chatbot,
|
| 64 |
title="🚀 Groq AI Chatbot",
|
| 65 |
description="Fast chatbot powered by Groq",
|
| 66 |
)
|
| 67 |
|
| 68 |
+
# Launch
|
| 69 |
if __name__ == "__main__":
|
| 70 |
demo.launch()
|