File size: 2,516 Bytes
411fbf0
 
 
 
08494c4
411fbf0
 
08494c4
411fbf0
 
 
 
08494c4
411fbf0
 
 
 
08494c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
411fbf0
 
 
08494c4
411fbf0
08494c4
 
 
 
 
 
 
411fbf0
08494c4
 
 
 
411fbf0
08494c4
 
411fbf0
08494c4
411fbf0
08494c4
 
 
 
 
 
 
 
411fbf0
08494c4
411fbf0
08494c4
 
 
 
 
 
 
 
 
411fbf0
08494c4
 
 
 
 
 
 
 
 
 
 
 
411fbf0
 
08494c4
411fbf0
 
 
 
08494c4
411fbf0
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import gradio as gr
from groq import Groq
import os
import time
import json

# =========================
# API KEY
# =========================
api_key = os.getenv("GROQ_API_KEY")

if not api_key:
    raise ValueError("GROQ_API_KEY not found")

client = Groq(api_key=api_key.strip())

# =========================
# MEMORY FILE
# =========================
MEMORY_FILE = "memory.json"

def load_memory():
    if os.path.exists(MEMORY_FILE):
        with open(MEMORY_FILE, "r") as f:
            return json.load(f)
    return {"facts": {}}

def save_memory(data):
    with open(MEMORY_FILE, "w") as f:
        json.dump(data, f)

memory = load_memory()

# =========================
# CHAT FUNCTION
# =========================
def chat(message, history):

    global memory

    # -------------------------
    # MEMORY DETECTION
    # -------------------------
    if "my name is" in message.lower():
        name = message.lower().split("my name is")[-1].strip()
        memory["facts"]["name"] = name
        save_memory(memory)

    # -------------------------
    # SYSTEM PROMPT WITH MEMORY
    # -------------------------
    system_prompt = "You are a helpful AI assistant."

    if "name" in memory["facts"]:
        system_prompt += f" User name is {memory['facts']['name']}."

    messages = [{"role": "system", "content": system_prompt}]

    # -------------------------
    # SAFE HISTORY HANDLING (FIXED ERROR)
    # -------------------------
    for item in history:
        if isinstance(item, (list, tuple)) and len(item) == 2:
            user_msg, bot_msg = item
            messages.append({"role": "user", "content": user_msg})
            messages.append({"role": "assistant", "content": bot_msg})

    messages.append({"role": "user", "content": message})

    # -------------------------
    # STREAM RESPONSE
    # -------------------------
    stream = client.chat.completions.create(
        model="llama-3.3-70b-versatile",
        messages=messages,
        stream=True,
        temperature=0.7
    )

    response_text = ""

    for chunk in stream:
        if chunk.choices[0].delta.content:
            token = chunk.choices[0].delta.content
            response_text += token

            time.sleep(0.03)

            yield response_text + "▌"

    yield response_text

# =========================
# UI
# =========================
demo = gr.ChatInterface(
    fn=chat,
    title="Streaming AI Chatbot using Groq",
    description="AI chatbot with memory + streaming",
)

demo.launch()