File size: 5,186 Bytes
6135b02
 
 
 
fa370b9
6cff41c
fa370b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a9c4f6
 
fa370b9
7a9c4f6
 
 
fa370b9
 
 
 
cf21c6e
fa370b9
 
 
 
 
 
 
 
 
 
 
 
 
ae7b53e
28e5fa5
ae7b53e
 
28e5fa5
 
 
 
 
 
 
ae7b53e
fa370b9
28e5fa5
 
fa370b9
28e5fa5
 
 
 
7310ae0
 
ae7b53e
cf21c6e
fa370b9
7a9c4f6
 
fa370b9
 
 
 
 
ae7b53e
 
 
 
fa370b9
 
284b445
 
 
fa370b9
284b445
fa370b9
7a9c4f6
 
ae7b53e
 
babc3a2
284b445
8be162a
 
1da9e54
8be162a
f7fbc97
 
 
 
 
 
28e5fa5
7310ae0
 
28e5fa5
 
 
 
 
 
dfbfd57
 
28e5fa5
 
 
 
 
 
dfbfd57
 
ae7b53e
fa370b9
 
27d1272
7310ae0
 
ae7b53e
fa370b9
 
1da9e54
7a9c4f6
 
ae7b53e
7a9c4f6
fa370b9
 
 
 
 
f7fbc97
 
 
 
 
7a9c4f6
 
ae7b53e
 
 
 
 
7a9c4f6
fa370b9
 
 
 
 
7a9c4f6
fa370b9
ae7b53e
 
 
 
28e5fa5
fa370b9
 
 
 
 
 
c8abe6c
ae7b53e
7a9c4f6
fa370b9
 
 
2ecf823
 
28e5fa5
fa370b9
2a0622a
fa370b9
7a9c4f6
 
28e5fa5
7310ae0
fa370b9
7a9c4f6
fa370b9
 
 
7a9c4f6
fa370b9
28e5fa5
fa370b9
 
ae7b53e
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
import os
import gradio as gr
from groq import Groq

# =====================================================
# Groq Client Initialization (GLOBAL)
# =====================================================
def get_groq_client():
    api_key = os.getenv("GROQ_API_KEY")
    if not api_key:
        raise RuntimeError("GROQ_API_KEY not set")
    return Groq(api_key=api_key)

client = get_groq_client()

# =====================================================
# Chat Logic (Gradio 6 compatible)
# =====================================================
def chat_with_groq(user_message, history):
    if not user_message or not user_message.strip():
        return history, ""

    history.append({"role": "user", "content": user_message})
    history.append({"role": "assistant", "content": "⚡ Groq is thinking..."})

    messages = [
        {"role": msg["role"], "content": msg["content"]}
        for msg in history[:-1]
    ]

    try:
        completion = client.chat.completions.create(
            model="llama-3.3-70b-versatile",
            messages=messages,
        )
        history[-1] = {
            "role": "assistant",
            "content": completion.choices[0].message.content
        }
    except Exception as e:
        history[-1] = {
            "role": "assistant",
            "content": f"⚠️ Error: {str(e)}"
        }

    return history, ""

# =====================================================
# Clear Chat
# =====================================================
def clear_chat():
    return [], ""

# =====================================================
# CSS (FINAL FIXED VERSION)
# =====================================================
custom_css = """
/* ---------- GLOBAL BOX FIX ---------- */
*,
*::before,
*::after {
  box-sizing: border-box !important;
}

/* ---------- Color System ---------- */
:root {
  --bg: #0b0f19;
  --surface: #111827;
  --user: #2563eb;
  --assistant: #1f2937;
  --text: #f9fafb;
  --muted: #9ca3af;
  --border: #1f2937;
}

/* ---------- Base Layout ---------- */
body {
  background-color: var(--bg);
}

.gradio-container {
  max-width: 1100px !important;
  margin: auto;
  padding: 24px;
  overflow-x: hidden !important;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* ---------- Chatbot ---------- */
.chatbot {
  background: var(--surface);
  border-radius: 16px;
  padding: 16px;
  height: 75vh;
  width: 100% !important;
  border: 1px solid var(--border);
  overflow-y: auto;
}

/* ---------- Messages ---------- */
.message {
  max-width: 100%;
  padding: 15px 20px;
  border-radius: 12px;
  margin-bottom: 12px;
  line-height: 1.7;
  font-size: 15px;

  white-space: normal !important;
  word-break: keep-all !important;
  overflow-wrap: break-word !important;

  box-sizing: border-box !important;
  background-clip: padding-box !important;
}

/* Remove ghost masks */
.chatbot .message,
.chatbot .message * {
  mask: none !important;
  -webkit-mask: none !important;
  background-image: none !important;
}

.chatbot .message::before,
.chatbot .message::after,
.chatbot .message *::before,
.chatbot .message *::after {
  display: none !important;
  content: none !important;
}

.message.user {
  background: var(--user);
  color: white;
  margin-left: 0 !important;
}

.message.bot {
  background: var(--assistant);
  color: var(--text);
  margin-right: 0 !important;
}

/* ---------- Input ---------- */
textarea {
  background: var(--surface) !important;
  color: var(--text) !important;
  border: 1px solid var(--border) !important;
  border-radius: 12px !important;
  padding: 12px !important;
  font-size: 15px !important;

  white-space: pre-wrap !important;
  word-break: normal !important;
  overflow-wrap: break-word !important;
}

textarea::placeholder {
  color: var(--muted);
}

/* ---------- Buttons ---------- */
button {
  background: var(--user) !important;
  color: white !important;
  border-radius: 12px !important;
  padding: 10px 16px !important;
  font-weight: 600 !important;
}

button:hover {
  opacity: 0.9;
}

/* Hide gradio default tools */
.chatbot-toolbar,
.chatbot-actions,
button[aria-label="Copy"],
button[aria-label="Share"],
button[aria-label="Clear"] {
  display: none !important;
}
"""

# =====================================================
# Gradio UI
# =====================================================
with gr.Blocks(title="GROQ CHATBOT") as demo:
    gr.Markdown("# 🤖 GROQ CHATBOT")
    gr.Markdown("<div class='subtitle'>Fast, reliable AI powered by Groq & LLaMA-3.3</div>")

    chatbot = gr.Chatbot(elem_classes="chatbot")
    state = gr.State([])

    with gr.Row():
        txt = gr.Textbox(placeholder="Type your message…", show_label=False, scale=5)
        send = gr.Button("Send", scale=1)
        clear = gr.Button("Clear", scale=1)

    send.click(chat_with_groq, [txt, state], [chatbot, txt])
    txt.submit(chat_with_groq, [txt, state], [chatbot, txt])
    clear.click(clear_chat, outputs=[chatbot, txt], show_progress=False)

# =====================================================
# Launch
# =====================================================
if __name__ == "__main__":
    demo.launch(css=custom_css)