File size: 4,404 Bytes
e29e4db
2e1cf12
 
e29e4db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e1cf12
e29e4db
 
 
 
 
 
2e1cf12
 
e29e4db
 
2e1cf12
e29e4db
2e1cf12
 
e29e4db
2e1cf12
 
 
 
 
 
e29e4db
 
2e1cf12
 
e29e4db
2e1cf12
e29e4db
2e1cf12
 
 
 
 
e29e4db
2e1cf12
 
 
 
 
e29e4db
2e1cf12
 
e29e4db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d396c90
e29e4db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d396c90
 
 
 
e29e4db
 
 
 
 
 
 
 
 
 
 
 
 
947ce07
e29e4db
 
 
d396c90
e29e4db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e1cf12
 
 
 
 
 
 
 
 
 
e29e4db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e1cf12
e29e4db
 
d396c90
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# =====================================
# πŸ’ŒπŸ’—πŸŒ» Kubra's AI Chatbot (FINAL FIX)
# Gradio 6 + Groq + HF Ready
# =====================================

import os
import gradio as gr
from groq import Groq


# -------------------------------------
# Load API Key
# -------------------------------------

GROQ_API_KEY = os.environ.get("GROQ_API_KEY")

if not GROQ_API_KEY:
    raise ValueError("❌ GROQ_API_KEY is missing.")


# -------------------------------------
# Init Groq Client
# -------------------------------------

client = Groq(api_key=GROQ_API_KEY)


# -------------------------------------
# Universal History Converter
# -------------------------------------

def build_messages(history, user_msg):

    messages = []

    if not history:
        history = []


    for item in history:

        # Case 1: New Gradio format (dict)
        if isinstance(item, dict):

            if "role" in item and "content" in item:

                messages.append({
                    "role": item["role"],
                    "content": str(item["content"])
                })


        # Case 2: Old format (tuple/list)
        elif isinstance(item, (list, tuple)) and len(item) == 2:

            human, ai = item

            if isinstance(human, str):
                messages.append({
                    "role": "user",
                    "content": human
                })

            if isinstance(ai, str):
                messages.append({
                    "role": "assistant",
                    "content": ai
                })


    # Add current user message
    messages.append({
        "role": "user",
        "content": user_msg
    })

    return messages


# -------------------------------------
# Chat Function
# -------------------------------------

def chat_with_ai(user_message, history):

    try:

        messages = build_messages(history, user_message)

        response = client.chat.completions.create(
            messages=messages,
            model="llama-3.3-70b-versatile"
        )

        return response.choices[0].message.content


    except Exception as e:

        return f"⚠️ System Error:\n{str(e)}"


# -------------------------------------
# CSS
# -------------------------------------

custom_css = """

body {
    background: linear-gradient(135deg, #ffd6ec, #e7ddff);
    font-family: system-ui;
}

.gradio-container {
    max-width: 1000px !important;
    margin: auto;
    padding: 20px;
}

div[data-testid="chatbot"] {
    background: white;
    border-radius: 18px;
    border: 2px solid #d7c7ff;
    min-height: 500px;
}

textarea {
    min-height: 90px !important;
    font-size: 17px !important;
    padding: 14px !important;
    border-radius: 14px !important;
}

button {
    background: #7a4cff !important;
    color: white !important;
    border-radius: 14px !important;
    font-weight: bold;
}

button:hover {
    background: #9c73ff !important;
}

"""


# -------------------------------------
# App
# -------------------------------------

with gr.Blocks(
    title="πŸ’ŒπŸ’—πŸŒ» Kubra's AI Chatbot"
) as demo:


    gr.Markdown("# πŸ’ŒπŸ’—πŸŒ» Kubra's AI Chatbot")
    gr.Markdown("### Your Friendly AI Assistant 🌷🀍")


    chatbot = gr.Chatbot(
        height=520
    )


    with gr.Row():

        user_input = gr.Textbox(
            placeholder="Type your message here...",
            lines=4,
            show_label=False
        )

        send_btn = gr.Button("Send πŸ’Œ")


    clear_btn = gr.Button("Clear 🧹")


    # Response handler
    def respond(msg, history):

        if not msg.strip():
            return "", history

        if history is None:
            history = []

        reply = chat_with_ai(msg, history)

        # Gradio 6 expects dict format
        history.append({
            "role": "user",
            "content": msg
        })

        history.append({
            "role": "assistant",
            "content": reply
        })

        return "", history


    send_btn.click(
        respond,
        [user_input, chatbot],
        [user_input, chatbot]
    )


    user_input.submit(
        respond,
        [user_input, chatbot],
        [user_input, chatbot]
    )


    clear_btn.click(
        lambda: [],
        None,
        chatbot
    )


# -------------------------------------
# Launch
# -------------------------------------

demo.launch(css=custom_css)