Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,26 +2,41 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
# Groq Client Initialization
|
| 7 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def chat_with_groq(user_message, history):
|
| 9 |
if not user_message or not user_message.strip():
|
| 10 |
return history, ""
|
| 11 |
|
| 12 |
-
# Add
|
| 13 |
-
history.append({
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
})
|
| 23 |
|
| 24 |
-
messages
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
try:
|
| 27 |
chat_completion = client.chat.completions.create(
|
|
@@ -31,6 +46,7 @@ def chat_with_groq(user_message, history):
|
|
| 31 |
|
| 32 |
response = chat_completion.choices[0].message.content
|
| 33 |
|
|
|
|
| 34 |
history[-1] = {
|
| 35 |
"role": "assistant",
|
| 36 |
"content": response
|
|
@@ -44,105 +60,143 @@ def chat_with_groq(user_message, history):
|
|
| 44 |
|
| 45 |
return history, ""
|
| 46 |
|
| 47 |
-
#
|
| 48 |
# Clear Chat Logic
|
| 49 |
-
#
|
| 50 |
def clear_chat():
|
| 51 |
return [], ""
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
#
|
| 55 |
-
#
|
| 56 |
custom_css = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
body {
|
| 58 |
-
|
| 59 |
}
|
| 60 |
|
| 61 |
.gradio-container {
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
|
| 66 |
h1 {
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
}
|
| 73 |
|
| 74 |
.chatbot {
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
}
|
| 80 |
|
| 81 |
.message {
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
}
|
| 84 |
|
| 85 |
textarea {
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
| 90 |
}
|
| 91 |
|
| 92 |
-
textarea
|
| 93 |
-
|
| 94 |
}
|
| 95 |
|
| 96 |
button {
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
}
|
| 103 |
|
| 104 |
button:hover {
|
| 105 |
-
|
| 106 |
-
box-shadow: 0 0 35px rgba(56, 189, 248, 1);
|
| 107 |
}
|
| 108 |
"""
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
# Gradio UI
|
| 112 |
-
#
|
| 113 |
-
with gr.Blocks(css=custom_css, title="
|
| 114 |
-
gr.Markdown("#
|
| 115 |
-
gr.Markdown(
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
chatbot = gr.Chatbot(elem_classes="chatbot")
|
| 118 |
state = gr.State([])
|
| 119 |
|
| 120 |
with gr.Row():
|
| 121 |
txt = gr.Textbox(
|
| 122 |
-
placeholder="
|
| 123 |
show_label=False,
|
| 124 |
-
scale=
|
| 125 |
)
|
| 126 |
-
send = gr.Button("
|
| 127 |
-
clear = gr.Button("
|
| 128 |
|
| 129 |
-
send.click(
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
outputs=[chatbot, txt],
|
| 133 |
-
)
|
| 134 |
-
|
| 135 |
-
txt.submit(
|
| 136 |
-
chat_with_groq,
|
| 137 |
-
inputs=[txt, state],
|
| 138 |
-
outputs=[chatbot, txt],
|
| 139 |
-
)
|
| 140 |
-
|
| 141 |
-
clear.click(
|
| 142 |
-
clear_chat,
|
| 143 |
-
outputs=[chatbot, txt],
|
| 144 |
-
show_progress=False,
|
| 145 |
-
)
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|
| 148 |
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
+
# =====================================================
|
| 6 |
+
# Groq Client Initialization (GLOBAL – IMPORTANT)
|
| 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() # ✅ must be global
|
| 15 |
+
|
| 16 |
+
# =====================================================
|
| 17 |
+
# Model Inference Logic (HF-safe, Gradio v4+)
|
| 18 |
+
# =====================================================
|
| 19 |
def chat_with_groq(user_message, history):
|
| 20 |
if not user_message or not user_message.strip():
|
| 21 |
return history, ""
|
| 22 |
|
| 23 |
+
# Add user message
|
| 24 |
+
history.append({
|
| 25 |
+
"role": "user",
|
| 26 |
+
"content": user_message
|
| 27 |
+
})
|
| 28 |
|
| 29 |
+
# Add temporary thinking message
|
| 30 |
+
history.append({
|
| 31 |
+
"role": "assistant",
|
| 32 |
+
"content": "⚡ Groq is thinking..."
|
| 33 |
+
})
|
|
|
|
| 34 |
|
| 35 |
+
# Prepare messages for Groq
|
| 36 |
+
messages = [
|
| 37 |
+
{"role": msg["role"], "content": msg["content"]}
|
| 38 |
+
for msg in history[:-1]
|
| 39 |
+
]
|
| 40 |
|
| 41 |
try:
|
| 42 |
chat_completion = client.chat.completions.create(
|
|
|
|
| 46 |
|
| 47 |
response = chat_completion.choices[0].message.content
|
| 48 |
|
| 49 |
+
# Replace thinking message
|
| 50 |
history[-1] = {
|
| 51 |
"role": "assistant",
|
| 52 |
"content": response
|
|
|
|
| 60 |
|
| 61 |
return history, ""
|
| 62 |
|
| 63 |
+
# =====================================================
|
| 64 |
# Clear Chat Logic
|
| 65 |
+
# =====================================================
|
| 66 |
def clear_chat():
|
| 67 |
return [], ""
|
| 68 |
|
| 69 |
+
# =====================================================
|
| 70 |
+
# CSS (Visible in Light + Dark Mode)
|
| 71 |
+
# =====================================================
|
| 72 |
custom_css = """
|
| 73 |
+
:root {
|
| 74 |
+
--bg: #f9fafb;
|
| 75 |
+
--surface: #ffffff;
|
| 76 |
+
--user: #2563eb;
|
| 77 |
+
--assistant: #e5e7eb;
|
| 78 |
+
--text: #111827;
|
| 79 |
+
--muted: #6b7280;
|
| 80 |
+
--border: #d1d5db;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
@media (prefers-color-scheme: dark) {
|
| 84 |
+
:root {
|
| 85 |
+
--bg: #0b0f19;
|
| 86 |
+
--surface: #111827;
|
| 87 |
+
--assistant: #1f2937;
|
| 88 |
+
--text: #f9fafb;
|
| 89 |
+
--muted: #9ca3af;
|
| 90 |
+
--border: #1f2937;
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
body {
|
| 95 |
+
background-color: var(--bg);
|
| 96 |
}
|
| 97 |
|
| 98 |
.gradio-container {
|
| 99 |
+
max-width: 1100px !important;
|
| 100 |
+
margin: auto;
|
| 101 |
+
padding: 24px;
|
| 102 |
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
| 103 |
}
|
| 104 |
|
| 105 |
h1 {
|
| 106 |
+
color: var(--text);
|
| 107 |
+
text-align: center;
|
| 108 |
+
font-size: 2rem;
|
| 109 |
+
margin-bottom: 6px;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
.subtitle {
|
| 113 |
+
color: var(--muted);
|
| 114 |
+
text-align: center;
|
| 115 |
+
margin-bottom: 20px;
|
| 116 |
}
|
| 117 |
|
| 118 |
.chatbot {
|
| 119 |
+
background: var(--surface);
|
| 120 |
+
border-radius: 16px;
|
| 121 |
+
padding: 16px;
|
| 122 |
+
height: 70vh;
|
| 123 |
+
overflow-y: auto;
|
| 124 |
+
border: 1px solid var(--border);
|
| 125 |
}
|
| 126 |
|
| 127 |
.message {
|
| 128 |
+
max-width: 72%;
|
| 129 |
+
padding: 12px 14px;
|
| 130 |
+
border-radius: 14px;
|
| 131 |
+
margin-bottom: 10px;
|
| 132 |
+
line-height: 1.6;
|
| 133 |
+
font-size: 15px;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
.message.user {
|
| 137 |
+
background: var(--user);
|
| 138 |
+
color: white;
|
| 139 |
+
margin-left: auto;
|
| 140 |
+
border-bottom-right-radius: 4px;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
.message.bot {
|
| 144 |
+
background: var(--assistant);
|
| 145 |
+
color: var(--text);
|
| 146 |
+
margin-right: auto;
|
| 147 |
+
border-bottom-left-radius: 4px;
|
| 148 |
}
|
| 149 |
|
| 150 |
textarea {
|
| 151 |
+
background: var(--surface) !important;
|
| 152 |
+
color: var(--text) !important;
|
| 153 |
+
border: 1px solid var(--border) !important;
|
| 154 |
+
border-radius: 12px !important;
|
| 155 |
+
padding: 12px !important;
|
| 156 |
+
font-size: 15px;
|
| 157 |
}
|
| 158 |
|
| 159 |
+
textarea::placeholder {
|
| 160 |
+
color: var(--muted);
|
| 161 |
}
|
| 162 |
|
| 163 |
button {
|
| 164 |
+
background: var(--user) !important;
|
| 165 |
+
color: white !important;
|
| 166 |
+
border-radius: 12px !important;
|
| 167 |
+
padding: 10px 16px !important;
|
| 168 |
+
font-weight: 600 !important;
|
| 169 |
}
|
| 170 |
|
| 171 |
button:hover {
|
| 172 |
+
opacity: 0.9;
|
|
|
|
| 173 |
}
|
| 174 |
"""
|
| 175 |
|
| 176 |
+
# =====================================================
|
| 177 |
+
# Gradio UI (HF Compatible)
|
| 178 |
+
# =====================================================
|
| 179 |
+
with gr.Blocks(css=custom_css, title="GROQ AI CHATBOT") as demo:
|
| 180 |
+
gr.Markdown("# 🤖 GROQ AI CHATBOT")
|
| 181 |
+
gr.Markdown(
|
| 182 |
+
"<div class='subtitle'>Fast, reliable AI powered by Groq & LLaMA-3.3</div>"
|
| 183 |
+
)
|
| 184 |
|
| 185 |
+
chatbot = gr.Chatbot(type="messages", elem_classes="chatbot")
|
| 186 |
state = gr.State([])
|
| 187 |
|
| 188 |
with gr.Row():
|
| 189 |
txt = gr.Textbox(
|
| 190 |
+
placeholder="Type your message…",
|
| 191 |
show_label=False,
|
| 192 |
+
scale=5,
|
| 193 |
)
|
| 194 |
+
send = gr.Button("Send", scale=1)
|
| 195 |
+
clear = gr.Button("Clear", scale=1)
|
| 196 |
|
| 197 |
+
send.click(chat_with_groq, [txt, state], [chatbot, txt])
|
| 198 |
+
txt.submit(chat_with_groq, [txt, state], [chatbot, txt])
|
| 199 |
+
clear.click(clear_chat, outputs=[chatbot, txt], show_progress=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
if __name__ == "__main__":
|
| 202 |
demo.launch()
|