Update app.py
Browse files
app.py
CHANGED
|
@@ -2,73 +2,101 @@ import gradio as gr
|
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
| 4 |
import time
|
|
|
|
| 5 |
|
| 6 |
# =========================
|
| 7 |
-
#
|
| 8 |
# =========================
|
| 9 |
api_key = os.getenv("GROQ_API_KEY")
|
| 10 |
|
| 11 |
if not api_key:
|
| 12 |
-
raise ValueError("GROQ_API_KEY not found
|
| 13 |
|
| 14 |
client = Groq(api_key=api_key.strip())
|
| 15 |
|
| 16 |
# =========================
|
| 17 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# =========================
|
| 19 |
def chat(message, history):
|
| 20 |
-
try:
|
| 21 |
-
messages = [
|
| 22 |
-
{
|
| 23 |
-
"role": "system",
|
| 24 |
-
"content": "You are a helpful AI assistant."
|
| 25 |
-
}
|
| 26 |
-
]
|
| 27 |
|
| 28 |
-
|
| 29 |
-
for user, bot in history:
|
| 30 |
-
messages.append({"role": "user", "content": user})
|
| 31 |
-
messages.append({"role": "assistant", "content": bot})
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
stream=True,
|
| 40 |
-
temperature=0.7
|
| 41 |
-
)
|
| 42 |
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
for chunk in stream:
|
| 47 |
-
if chunk.choices[0].delta.content:
|
| 48 |
-
token = chunk.choices[0].delta.content
|
| 49 |
-
response_text += token
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
# =========================
|
| 63 |
-
# UI
|
| 64 |
# =========================
|
| 65 |
demo = gr.ChatInterface(
|
| 66 |
fn=chat,
|
| 67 |
title="Streaming AI Chatbot using Groq",
|
| 68 |
-
description="AI chatbot with
|
| 69 |
)
|
| 70 |
|
| 71 |
-
# =========================
|
| 72 |
-
# LAUNCH APP
|
| 73 |
-
# =========================
|
| 74 |
demo.launch()
|
|
|
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
| 4 |
import time
|
| 5 |
+
import json
|
| 6 |
|
| 7 |
# =========================
|
| 8 |
+
# API KEY
|
| 9 |
# =========================
|
| 10 |
api_key = os.getenv("GROQ_API_KEY")
|
| 11 |
|
| 12 |
if not api_key:
|
| 13 |
+
raise ValueError("GROQ_API_KEY not found")
|
| 14 |
|
| 15 |
client = Groq(api_key=api_key.strip())
|
| 16 |
|
| 17 |
# =========================
|
| 18 |
+
# MEMORY FILE
|
| 19 |
+
# =========================
|
| 20 |
+
MEMORY_FILE = "memory.json"
|
| 21 |
+
|
| 22 |
+
def load_memory():
|
| 23 |
+
if os.path.exists(MEMORY_FILE):
|
| 24 |
+
with open(MEMORY_FILE, "r") as f:
|
| 25 |
+
return json.load(f)
|
| 26 |
+
return {"facts": {}}
|
| 27 |
+
|
| 28 |
+
def save_memory(data):
|
| 29 |
+
with open(MEMORY_FILE, "w") as f:
|
| 30 |
+
json.dump(data, f)
|
| 31 |
+
|
| 32 |
+
memory = load_memory()
|
| 33 |
+
|
| 34 |
+
# =========================
|
| 35 |
+
# CHAT FUNCTION
|
| 36 |
# =========================
|
| 37 |
def chat(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
global memory
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# -------------------------
|
| 42 |
+
# MEMORY DETECTION
|
| 43 |
+
# -------------------------
|
| 44 |
+
if "my name is" in message.lower():
|
| 45 |
+
name = message.lower().split("my name is")[-1].strip()
|
| 46 |
+
memory["facts"]["name"] = name
|
| 47 |
+
save_memory(memory)
|
| 48 |
|
| 49 |
+
# -------------------------
|
| 50 |
+
# SYSTEM PROMPT WITH MEMORY
|
| 51 |
+
# -------------------------
|
| 52 |
+
system_prompt = "You are a helpful AI assistant."
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
if "name" in memory["facts"]:
|
| 55 |
+
system_prompt += f" User name is {memory['facts']['name']}."
|
| 56 |
|
| 57 |
+
messages = [{"role": "system", "content": system_prompt}]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
# -------------------------
|
| 60 |
+
# SAFE HISTORY HANDLING (FIXED ERROR)
|
| 61 |
+
# -------------------------
|
| 62 |
+
for item in history:
|
| 63 |
+
if isinstance(item, (list, tuple)) and len(item) == 2:
|
| 64 |
+
user_msg, bot_msg = item
|
| 65 |
+
messages.append({"role": "user", "content": user_msg})
|
| 66 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 67 |
|
| 68 |
+
messages.append({"role": "user", "content": message})
|
| 69 |
|
| 70 |
+
# -------------------------
|
| 71 |
+
# STREAM RESPONSE
|
| 72 |
+
# -------------------------
|
| 73 |
+
stream = client.chat.completions.create(
|
| 74 |
+
model="llama-3.3-70b-versatile",
|
| 75 |
+
messages=messages,
|
| 76 |
+
stream=True,
|
| 77 |
+
temperature=0.7
|
| 78 |
+
)
|
| 79 |
|
| 80 |
+
response_text = ""
|
| 81 |
+
|
| 82 |
+
for chunk in stream:
|
| 83 |
+
if chunk.choices[0].delta.content:
|
| 84 |
+
token = chunk.choices[0].delta.content
|
| 85 |
+
response_text += token
|
| 86 |
+
|
| 87 |
+
time.sleep(0.03)
|
| 88 |
+
|
| 89 |
+
yield response_text + "▌"
|
| 90 |
+
|
| 91 |
+
yield response_text
|
| 92 |
|
| 93 |
# =========================
|
| 94 |
+
# UI
|
| 95 |
# =========================
|
| 96 |
demo = gr.ChatInterface(
|
| 97 |
fn=chat,
|
| 98 |
title="Streaming AI Chatbot using Groq",
|
| 99 |
+
description="AI chatbot with memory + streaming",
|
| 100 |
)
|
| 101 |
|
|
|
|
|
|
|
|
|
|
| 102 |
demo.launch()
|