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("