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 6 |
+
# Groq Client Initialization
|
| 7 |
+
# -----------------------------
|
| 8 |
+
def get_groq_client():
|
| 9 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 10 |
+
if not api_key:
|
| 11 |
+
raise RuntimeError("GROQ_API_KEY not set")
|
| 12 |
+
return Groq(api_key=api_key)
|
| 13 |
+
|
| 14 |
+
client = get_groq_client()
|
| 15 |
+
|
| 16 |
+
# -----------------------------
|
| 17 |
+
# Model Inference Logic
|
| 18 |
+
# -----------------------------
|
| 19 |
+
def chat_with_groq(user_message, history):
|
| 20 |
+
if not user_message or not user_message.strip():
|
| 21 |
+
return history, ""
|
| 22 |
+
|
| 23 |
+
history.append((user_message, "⚡ Groq is thinking..."))
|
| 24 |
+
|
| 25 |
+
messages = []
|
| 26 |
+
for user, assistant in history[:-1]:
|
| 27 |
+
messages.append({"role": "user", "content": user})
|
| 28 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 29 |
+
|
| 30 |
+
messages.append({"role": "user", "content": user_message})
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
chat_completion = client.chat.completions.create(
|
| 34 |
+
model="llama-3.3-70b-versatile",
|
| 35 |
+
messages=messages,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
response = chat_completion.choices[0].message.content
|
| 39 |
+
history[-1] = (user_message, response)
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
history[-1] = (user_message, f"⚠️ Error: {str(e)}")
|
| 43 |
+
|
| 44 |
+
return history, ""
|
| 45 |
+
|
| 46 |
+
# -----------------------------
|
| 47 |
+
# Clear Chat Logic
|
| 48 |
+
# -----------------------------
|
| 49 |
+
def clear_chat():
|
| 50 |
+
return [], ""
|
| 51 |
+
|
| 52 |
+
# -----------------------------
|
| 53 |
+
# Neon CSS
|
| 54 |
+
# -----------------------------
|
| 55 |
+
custom_css = """
|
| 56 |
+
body {
|
| 57 |
+
background: radial-gradient(circle at top, #020617, #020617);
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
.gradio-container {
|
| 61 |
+
max-width: 900px !important;
|
| 62 |
+
margin: auto;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
h1 {
|
| 66 |
+
text-align: center;
|
| 67 |
+
font-size: 2.6rem;
|
| 68 |
+
background: linear-gradient(90deg, #38bdf8, #22d3ee, #818cf8);
|
| 69 |
+
-webkit-background-clip: text;
|
| 70 |
+
-webkit-text-fill-color: transparent;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
.chatbot {
|
| 74 |
+
background: rgba(15, 23, 42, 0.75);
|
| 75 |
+
backdrop-filter: blur(16px);
|
| 76 |
+
border-radius: 18px;
|
| 77 |
+
box-shadow: 0 0 35px rgba(56, 189, 248, 0.2);
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
.message {
|
| 81 |
+
animation: fadeSlide 0.35s ease-in-out;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
textarea {
|
| 85 |
+
background: rgba(2, 6, 23, 0.85) !important;
|
| 86 |
+
color: #e5e7eb !important;
|
| 87 |
+
border: 1px solid #38bdf8 !important;
|
| 88 |
+
border-radius: 14px !important;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
textarea:focus {
|
| 92 |
+
box-shadow: 0 0 18px #38bdf8 !important;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
button {
|
| 96 |
+
background: linear-gradient(135deg, #22d3ee, #3b82f6) !important;
|
| 97 |
+
color: #020617 !important;
|
| 98 |
+
font-weight: 800 !important;
|
| 99 |
+
border-radius: 14px !important;
|
| 100 |
+
box-shadow: 0 0 22px rgba(56, 189, 248, 0.7);
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
button:hover {
|
| 104 |
+
transform: scale(1.05);
|
| 105 |
+
box-shadow: 0 0 35px rgba(56, 189, 248, 1);
|
| 106 |
+
}
|
| 107 |
+
"""
|
| 108 |
+
|
| 109 |
+
# -----------------------------
|
| 110 |
+
# Gradio UI
|
| 111 |
+
# -----------------------------
|
| 112 |
+
with gr.Blocks(css=custom_css, title="Groq Neon ChatBot") as demo:
|
| 113 |
+
gr.Markdown("# ⚡ GROQ NEON CHATBOT")
|
| 114 |
+
gr.Markdown("### Ultra-fast AI • Cyber UI • LLaMA 3.3")
|
| 115 |
+
|
| 116 |
+
chatbot = gr.Chatbot(elem_classes="chatbot")
|
| 117 |
+
state = gr.State([])
|
| 118 |
+
|
| 119 |
+
with gr.Row():
|
| 120 |
+
txt = gr.Textbox(
|
| 121 |
+
placeholder="Ask anything…",
|
| 122 |
+
show_label=False,
|
| 123 |
+
scale=4,
|
| 124 |
+
)
|
| 125 |
+
send = gr.Button("🚀 Send", scale=1)
|
| 126 |
+
clear = gr.Button("🧹 Clear", scale=1)
|
| 127 |
+
|
| 128 |
+
send.click(
|
| 129 |
+
chat_with_groq,
|
| 130 |
+
inputs=[txt, state],
|
| 131 |
+
outputs=[chatbot, txt],
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
txt.submit(
|
| 135 |
+
chat_with_groq,
|
| 136 |
+
inputs=[txt, state],
|
| 137 |
+
outputs=[chatbot, txt],
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
clear.click(
|
| 141 |
+
clear_chat,
|
| 142 |
+
outputs=[chatbot, txt],
|
| 143 |
+
show_progress=False,
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
if __name__ == "__main__":
|
| 147 |
+
demo.launch()
|