Update app.py
Browse files
app.py
CHANGED
|
@@ -3,122 +3,121 @@ import gradio as gr
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
# 1. Setup API Client
|
|
|
|
| 6 |
api_key = os.environ.get("GROQ_API_KEY")
|
| 7 |
client = Groq(api_key=api_key)
|
| 8 |
|
| 9 |
-
# 2. UI Styling
|
| 10 |
custom_css = """
|
| 11 |
-
.gradio-container { max-
|
| 12 |
-
#chatbot-window {
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
min-height: 500px;
|
| 18 |
-
}
|
| 19 |
-
.linkedin-icon:hover { transform: scale(1.1); transition: transform 0.3s ease; }
|
| 20 |
-
.sidebar-footer {
|
| 21 |
-
margin-top: 30px;
|
| 22 |
-
text-align: center;
|
| 23 |
-
border-top: 1px solid #ddd;
|
| 24 |
-
padding-top: 20px;
|
| 25 |
}
|
| 26 |
"""
|
| 27 |
|
| 28 |
# 3. Secure Chat Logic
|
| 29 |
def chat_with_groq(message, history, model, temperature, system_prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
cleaned_messages = [{"role": "system", "content": system_prompt}]
|
|
|
|
|
|
|
| 31 |
for msg in history:
|
| 32 |
-
cleaned_messages.append({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
cleaned_messages.append({"role": "user", "content": message})
|
| 34 |
-
|
| 35 |
try:
|
|
|
|
| 36 |
response = client.chat.completions.create(
|
| 37 |
model=model,
|
| 38 |
messages=cleaned_messages,
|
| 39 |
temperature=temperature,
|
| 40 |
stream=True
|
| 41 |
)
|
|
|
|
| 42 |
partial_message = ""
|
| 43 |
for chunk in response:
|
| 44 |
if chunk.choices[0].delta.content:
|
| 45 |
partial_message += chunk.choices[0].delta.content
|
| 46 |
yield partial_message
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
yield f"⚠️ API Error: {str(e)}"
|
| 49 |
|
| 50 |
-
# 4.
|
| 51 |
-
def user_turn(msg, hist):
|
| 52 |
-
if not msg:
|
| 53 |
-
return "", hist
|
| 54 |
-
hist.append({"role": "user", "content": msg})
|
| 55 |
-
return "", hist
|
| 56 |
-
|
| 57 |
-
def bot_turn(hist, m, t, s):
|
| 58 |
-
if not hist or hist[-1]["role"] != "user":
|
| 59 |
-
return hist
|
| 60 |
-
user_msg = hist[-1]["content"]
|
| 61 |
-
gen = chat_with_groq(user_msg, hist[:-1], m, t, s)
|
| 62 |
-
hist.append({"role": "assistant", "content": ""})
|
| 63 |
-
for chunk in gen:
|
| 64 |
-
hist[-1]["content"] = chunk
|
| 65 |
-
yield hist
|
| 66 |
-
|
| 67 |
-
# 5. Build the Interface
|
| 68 |
with gr.Blocks() as demo:
|
| 69 |
gr.Markdown("# ⚡ Groq Professional Studio")
|
| 70 |
-
|
|
|
|
| 71 |
with gr.Row():
|
| 72 |
-
#
|
| 73 |
with gr.Column(scale=4):
|
| 74 |
-
chatbot = gr.Chatbot(elem_id="chatbot-window"
|
| 75 |
with gr.Row():
|
| 76 |
-
msg_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
submit_btn = gr.Button("Send", variant="primary", scale=1)
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
with gr.Column(scale=1
|
| 81 |
gr.Markdown("### ⚙️ Settings")
|
| 82 |
-
|
| 83 |
model_choice = gr.Dropdown(
|
| 84 |
choices=["llama-3.3-70b-versatile", "llama-3.1-8b-instant"],
|
| 85 |
value="llama-3.3-70b-versatile",
|
| 86 |
label="Select Model"
|
| 87 |
)
|
| 88 |
-
|
| 89 |
temp = gr.Slider(0.0, 1.0, 0.7, label="Temperature")
|
| 90 |
-
|
| 91 |
sys_msg = gr.Textbox(
|
| 92 |
value="You are a professional assistant.",
|
| 93 |
label="System Prompt",
|
| 94 |
-
lines=
|
| 95 |
)
|
| 96 |
-
|
| 97 |
-
# Moved from below input bar to here
|
| 98 |
-
clear = gr.Button("Clear Chat", variant="secondary")
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
#
|
| 114 |
msg_input.submit(user_turn, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
|
| 115 |
bot_turn, [chatbot, model_choice, temp, sys_msg], chatbot
|
| 116 |
)
|
| 117 |
submit_btn.click(user_turn, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
|
| 118 |
bot_turn, [chatbot, model_choice, temp, sys_msg], chatbot
|
| 119 |
)
|
|
|
|
|
|
|
| 120 |
clear.click(lambda: [], None, chatbot, queue=False)
|
| 121 |
|
|
|
|
| 122 |
if __name__ == "__main__":
|
| 123 |
# Settings moved to launch() for Gradio 6 compatibility
|
| 124 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
# 1. Setup API Client
|
| 6 |
+
# Ensure GROQ_API_KEY is set in your Space Settings > Secrets
|
| 7 |
api_key = os.environ.get("GROQ_API_KEY")
|
| 8 |
client = Groq(api_key=api_key)
|
| 9 |
|
| 10 |
+
# 2. Responsive UI Styling
|
| 11 |
custom_css = """
|
| 12 |
+
.gradio-container { max-height: 100vh !important; overflow: hidden !important; }
|
| 13 |
+
#chatbot-window {
|
| 14 |
+
height: 50vh !important;
|
| 15 |
+
border-radius: 12px;
|
| 16 |
+
border: 1px solid #e0e0e0;
|
| 17 |
+
background: white;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
}
|
| 19 |
"""
|
| 20 |
|
| 21 |
# 3. Secure Chat Logic
|
| 22 |
def chat_with_groq(message, history, model, temperature, system_prompt):
|
| 23 |
+
"""
|
| 24 |
+
Cleans history to remove Gradio-specific metadata that causes 400 errors.
|
| 25 |
+
"""
|
| 26 |
+
# Initialize with System Prompt
|
| 27 |
cleaned_messages = [{"role": "system", "content": system_prompt}]
|
| 28 |
+
|
| 29 |
+
# Scrub history for clean role/content pairs
|
| 30 |
for msg in history:
|
| 31 |
+
cleaned_messages.append({
|
| 32 |
+
"role": msg["role"],
|
| 33 |
+
"content": msg["content"]
|
| 34 |
+
})
|
| 35 |
+
|
| 36 |
+
# Add current user prompt
|
| 37 |
cleaned_messages.append({"role": "user", "content": message})
|
| 38 |
+
|
| 39 |
try:
|
| 40 |
+
# High-speed inference via Groq
|
| 41 |
response = client.chat.completions.create(
|
| 42 |
model=model,
|
| 43 |
messages=cleaned_messages,
|
| 44 |
temperature=temperature,
|
| 45 |
stream=True
|
| 46 |
)
|
| 47 |
+
|
| 48 |
partial_message = ""
|
| 49 |
for chunk in response:
|
| 50 |
if chunk.choices[0].delta.content:
|
| 51 |
partial_message += chunk.choices[0].delta.content
|
| 52 |
yield partial_message
|
| 53 |
+
|
| 54 |
except Exception as e:
|
| 55 |
yield f"⚠️ API Error: {str(e)}"
|
| 56 |
|
| 57 |
+
# 4. Build the Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
with gr.Blocks() as demo:
|
| 59 |
gr.Markdown("# ⚡ Groq Professional Studio")
|
| 60 |
+
gr.Markdown("Everything is running smoothly on my end. Please let me know if you encounter any errors!")
|
| 61 |
+
|
| 62 |
with gr.Row():
|
| 63 |
+
# Main Interaction Area
|
| 64 |
with gr.Column(scale=4):
|
| 65 |
+
chatbot = gr.Chatbot(elem_id="chatbot-window")
|
| 66 |
with gr.Row():
|
| 67 |
+
msg_input = gr.Textbox(
|
| 68 |
+
placeholder="Ask me anything...",
|
| 69 |
+
container=False,
|
| 70 |
+
scale=9
|
| 71 |
+
)
|
| 72 |
submit_btn = gr.Button("Send", variant="primary", scale=1)
|
| 73 |
|
| 74 |
+
# Settings Sidebar
|
| 75 |
+
with gr.Column(scale=1):
|
| 76 |
gr.Markdown("### ⚙️ Settings")
|
|
|
|
| 77 |
model_choice = gr.Dropdown(
|
| 78 |
choices=["llama-3.3-70b-versatile", "llama-3.1-8b-instant"],
|
| 79 |
value="llama-3.3-70b-versatile",
|
| 80 |
label="Select Model"
|
| 81 |
)
|
|
|
|
| 82 |
temp = gr.Slider(0.0, 1.0, 0.7, label="Temperature")
|
|
|
|
| 83 |
sys_msg = gr.Textbox(
|
| 84 |
value="You are a professional assistant.",
|
| 85 |
label="System Prompt",
|
| 86 |
+
lines=3
|
| 87 |
)
|
| 88 |
+
clear = gr.Button("Clear Chat", variant="stop")
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
# --- UI Event Flow ---
|
| 91 |
+
def user_turn(msg, hist):
|
| 92 |
+
# Update UI state with user message
|
| 93 |
+
hist.append({"role": "user", "content": msg})
|
| 94 |
+
return "", hist
|
| 95 |
+
|
| 96 |
+
def bot_turn(hist, m, t, s):
|
| 97 |
+
# Get response for the last user message
|
| 98 |
+
user_msg = hist[-1]["content"]
|
| 99 |
+
gen = chat_with_groq(user_msg, hist[:-1], m, t, s)
|
| 100 |
+
|
| 101 |
+
hist.append({"role": "assistant", "content": ""})
|
| 102 |
+
for chunk in gen:
|
| 103 |
+
hist[-1]["content"] = chunk
|
| 104 |
+
yield hist
|
| 105 |
|
| 106 |
+
# Link Input Actions
|
| 107 |
msg_input.submit(user_turn, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
|
| 108 |
bot_turn, [chatbot, model_choice, temp, sys_msg], chatbot
|
| 109 |
)
|
| 110 |
submit_btn.click(user_turn, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
|
| 111 |
bot_turn, [chatbot, model_choice, temp, sys_msg], chatbot
|
| 112 |
)
|
| 113 |
+
|
| 114 |
+
# Clear Workspace
|
| 115 |
clear.click(lambda: [], None, chatbot, queue=False)
|
| 116 |
|
| 117 |
+
# 5. Production Launch
|
| 118 |
if __name__ == "__main__":
|
| 119 |
# Settings moved to launch() for Gradio 6 compatibility
|
| 120 |
+
demo.launch(
|
| 121 |
+
theme=gr.themes.Soft(primary_hue="blue"),
|
| 122 |
+
css=custom_css
|
| 123 |
+
)
|