Simple_ChatBot / app.py
aazankhanYousafzai's picture
Update app.py
dfbfd57 verified
raw
history blame
5.7 kB
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 (Light/Dark + Gradio 6 UI FIX)
# =====================================================
custom_css = """
/* ---------- Color System ---------- */
:root {
--bg: #f9fafb;
--surface: #ffffff;
--user: #2563eb;
--assistant: #e5e7eb;
--text: #111827;
--muted: #6b7280;
--border: #d1d5db;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #0b0f19;
--surface: #111827;
--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;
}
/* ---------- Typography ---------- */
h1 {
color: var(--text);
text-align: center;
font-size: 2rem;
margin-bottom: 6px;
}
.subtitle {
color: var(--muted);
text-align: center;
margin-bottom: 20px;
}
/* ---------- Chatbot ---------- */
.chatbot {
background: var(--surface);
border-radius: 16px;
padding: 16px;
height: 70vh;
width: 100% !important;
border: 1px solid var(--border);
overflow-y: auto;
}
/* ---------- Messages ---------- */
.message {
max-width: 72%;
padding: 8px 10px;
border-radius: 10px;
margin-bottom: 9px;
line-height: 1.3;
font-size: 14px;
white-space: normal !important;
word-break: keep-all !important;
overflow-wrap: break-word !important;
box-sizing: border-box !important;
}
/* 🔧 REMOVE BLACK GHOST BLOCKS */
.message::before,
.message::after,
.chatbot .message div::before,
.chatbot .message div::after {
display: none !important;
content: none !important;
}
.message {
overflow: visible !important;
background-clip: padding-box !important;
}
.chatbot * {
box-shadow: none !important;
filter: none !important;
}
.message.user {
background: var(--user);
color: white;
margin-left: auto;
border-bottom-right-radius: 4px;
}
.message.bot {
background: var(--assistant);
color: var(--text);
margin-right: auto;
border-bottom-left-radius: 4px;
}
/* ---------- 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;
}
/* ---------- GRADIO 6 FIXES ---------- */
.chatbot-toolbar,
.chatbot-actions,
button[aria-label="Copy"],
button[aria-label="Share"],
button[aria-label="Clear"] {
display: none !important;
}
.chatbot .scroll-to-bottom {
right: 16px !important;
bottom: 80px !important;
}
"""
# =====================================================
# Gradio UI
# =====================================================
with gr.Blocks(title="GROQ AI CHATBOT") as demo:
gr.Markdown("# 🤖 GROQ AI 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 (Gradio 6)
# =====================================================
if __name__ == "__main__":
demo.launch(css=custom_css)