Update app.py
Browse files
app.py
CHANGED
|
@@ -3,124 +3,115 @@ import gradio as gr
|
|
| 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.
|
| 11 |
custom_css = """
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
#chatbot-window {
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 58 |
with gr.Blocks() as demo:
|
| 59 |
gr.Markdown("# ⚡ Groq Professional Studio")
|
| 60 |
-
|
| 61 |
-
|
| 62 |
with gr.Row():
|
| 63 |
-
#
|
| 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 |
-
#
|
| 75 |
-
|
| 76 |
-
with gr.Column(scale=1):
|
| 77 |
gr.Markdown("### ⚙️ Settings")
|
| 78 |
model_choice = gr.Dropdown(
|
| 79 |
choices=["llama-3.3-70b-versatile", "llama-3.1-8b-instant"],
|
| 80 |
-
value="llama-3.3-70b-versatile",
|
| 81 |
-
label="Select Model"
|
| 82 |
)
|
| 83 |
temp = gr.Slider(0.0, 1.0, 0.7, label="Temperature")
|
| 84 |
-
sys_msg = gr.Textbox(
|
| 85 |
-
value="You are a professional assistant.",
|
| 86 |
-
label="System Prompt",
|
| 87 |
-
lines=3
|
| 88 |
-
)
|
| 89 |
clear = gr.Button("Clear Chat", variant="stop")
|
| 90 |
-
# Footer Section with LinkedIn Icon
|
| 91 |
-
|
| 92 |
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
def user_turn(msg, hist):
|
| 95 |
-
# Update UI state with user message
|
| 96 |
hist.append({"role": "user", "content": msg})
|
| 97 |
return "", hist
|
| 98 |
|
| 99 |
def bot_turn(hist, m, t, s):
|
| 100 |
-
# Get response for the last user message
|
| 101 |
user_msg = hist[-1]["content"]
|
| 102 |
gen = chat_with_groq(user_msg, hist[:-1], m, t, s)
|
| 103 |
-
|
| 104 |
hist.append({"role": "assistant", "content": ""})
|
| 105 |
for chunk in gen:
|
| 106 |
hist[-1]["content"] = chunk
|
| 107 |
yield hist
|
| 108 |
|
| 109 |
-
# Link Input Actions
|
| 110 |
msg_input.submit(user_turn, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
|
| 111 |
bot_turn, [chatbot, model_choice, temp, sys_msg], chatbot
|
| 112 |
)
|
| 113 |
submit_btn.click(user_turn, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
|
| 114 |
bot_turn, [chatbot, model_choice, temp, sys_msg], chatbot
|
| 115 |
)
|
| 116 |
-
|
| 117 |
-
# Clear Workspace
|
| 118 |
clear.click(lambda: [], None, chatbot, queue=False)
|
| 119 |
|
| 120 |
-
# 5. Production Launch
|
| 121 |
if __name__ == "__main__":
|
| 122 |
-
|
| 123 |
-
demo.launch(
|
| 124 |
-
theme=gr.themes.Soft(primary_hue="blue"),
|
| 125 |
-
css=custom_css
|
| 126 |
-
)
|
|
|
|
| 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. SEPARATED CSS: Distinct styles for Chatbot and Sidebar
|
| 10 |
custom_css = """
|
| 11 |
+
/* Container & Layout */
|
| 12 |
+
.gradio-container { max-width: 1200px; margin: auto; }
|
| 13 |
+
|
| 14 |
+
/* Chatbot Panel Styles */
|
| 15 |
#chatbot-window {
|
| 16 |
+
height: 600px !important;
|
| 17 |
+
border-radius: 12px;
|
| 18 |
+
border: 1px solid #e0e0e0;
|
| 19 |
+
background-color: #ffffff;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
/* Sidebar Specific Styles */
|
| 23 |
+
.sidebar-panel {
|
| 24 |
+
background-color: rgba(128, 128, 128, 0.05);
|
| 25 |
+
padding: 20px !important;
|
| 26 |
+
border-radius: 12px;
|
| 27 |
+
border: 1px solid #eee;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
/* LinkedIn Icon Animation */
|
| 31 |
+
.linkedin-icon:hover {
|
| 32 |
+
transform: scale(1.1) rotate(5deg);
|
| 33 |
+
transition: transform 0.3s ease;
|
| 34 |
}
|
| 35 |
"""
|
| 36 |
|
| 37 |
# 3. Secure Chat Logic
|
| 38 |
def chat_with_groq(message, history, model, temperature, system_prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
cleaned_messages = [{"role": "system", "content": system_prompt}]
|
|
|
|
|
|
|
| 40 |
for msg in history:
|
| 41 |
+
cleaned_messages.append({"role": msg["role"], "content": msg["content"]})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
cleaned_messages.append({"role": "user", "content": message})
|
| 43 |
+
|
| 44 |
try:
|
|
|
|
| 45 |
response = client.chat.completions.create(
|
| 46 |
model=model,
|
| 47 |
messages=cleaned_messages,
|
| 48 |
temperature=temperature,
|
| 49 |
stream=True
|
| 50 |
)
|
|
|
|
| 51 |
partial_message = ""
|
| 52 |
for chunk in response:
|
| 53 |
if chunk.choices[0].delta.content:
|
| 54 |
partial_message += chunk.choices[0].delta.content
|
| 55 |
yield partial_message
|
|
|
|
| 56 |
except Exception as e:
|
| 57 |
yield f"⚠️ API Error: {str(e)}"
|
| 58 |
|
| 59 |
+
# 4. Interface Setup
|
| 60 |
with gr.Blocks() as demo:
|
| 61 |
gr.Markdown("# ⚡ Groq Professional Studio")
|
| 62 |
+
|
|
|
|
| 63 |
with gr.Row():
|
| 64 |
+
# --- MAIN CHATBOT COLUMN ---
|
| 65 |
with gr.Column(scale=4):
|
| 66 |
chatbot = gr.Chatbot(elem_id="chatbot-window")
|
| 67 |
with gr.Row():
|
| 68 |
+
msg_input = gr.Textbox(placeholder="Ask me anything...", container=False, scale=9)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
submit_btn = gr.Button("Send", variant="primary", scale=1)
|
| 70 |
|
| 71 |
+
# --- SETTINGS SIDEBAR COLUMN ---
|
| 72 |
+
with gr.Column(scale=1, elem_classes="sidebar-panel"):
|
|
|
|
| 73 |
gr.Markdown("### ⚙️ Settings")
|
| 74 |
model_choice = gr.Dropdown(
|
| 75 |
choices=["llama-3.3-70b-versatile", "llama-3.1-8b-instant"],
|
| 76 |
+
value="llama-3.3-70b-versatile", label="Select Model"
|
|
|
|
| 77 |
)
|
| 78 |
temp = gr.Slider(0.0, 1.0, 0.7, label="Temperature")
|
| 79 |
+
sys_msg = gr.Textbox(value="You are a professional assistant.", label="System Prompt", lines=3)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
clear = gr.Button("Clear Chat", variant="stop")
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
# LINKEDIN OPTION: Added directly below Clear Chat inside the Sidebar
|
| 83 |
+
gr.HTML("""
|
| 84 |
+
<div style="text-align: center; margin-top: 25px; padding-top: 15px; border-top: 1px solid #ddd;">
|
| 85 |
+
<p style="font-size: 0.85em; color: #666; margin-bottom: 10px;"><b>Developer Support</b></p>
|
| 86 |
+
<a href="https://www.linkedin.com/in/YOUR_LINKEDIN_USERNAME" target="_blank" style="text-decoration: none;">
|
| 87 |
+
<svg class="linkedin-icon" xmlns="http://www.w3.org/2000/svg" width="35" height="35" viewBox="0 0 24 24" fill="#0077b5">
|
| 88 |
+
<path d="M19 0h-14c-2.761 0-5 2.239-5 5v14c0 2.761 2.239 5 5 5h14c2.762 0 5-2.239 5-5v-14c0-2.761-2.239-5-5-5zm-11 19h-3v-11h3v11zm-1.5-12.268c-.966 0-1.75-.79-1.75-1.764s.784-1.764 1.75-1.764 1.75.79 1.75 1.764-.783 1.764-1.75 1.764zm13.5 12.268h-3v-5.604c0-3.368-4-3.113-4 0v5.604h-3v-11h3v1.765c1.396-2.586 7-2.777 7 2.476v6.759z"/>
|
| 89 |
+
</svg>
|
| 90 |
+
</a>
|
| 91 |
+
<p style="font-size: 0.75em; color: #888; margin-top: 5px;">Hassan Naseer | NUTECH</p>
|
| 92 |
+
</div>
|
| 93 |
+
""")
|
| 94 |
+
|
| 95 |
+
# --- Event Handling ---
|
| 96 |
def user_turn(msg, hist):
|
|
|
|
| 97 |
hist.append({"role": "user", "content": msg})
|
| 98 |
return "", hist
|
| 99 |
|
| 100 |
def bot_turn(hist, m, t, s):
|
|
|
|
| 101 |
user_msg = hist[-1]["content"]
|
| 102 |
gen = chat_with_groq(user_msg, hist[:-1], m, t, s)
|
|
|
|
| 103 |
hist.append({"role": "assistant", "content": ""})
|
| 104 |
for chunk in gen:
|
| 105 |
hist[-1]["content"] = chunk
|
| 106 |
yield hist
|
| 107 |
|
|
|
|
| 108 |
msg_input.submit(user_turn, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
|
| 109 |
bot_turn, [chatbot, model_choice, temp, sys_msg], chatbot
|
| 110 |
)
|
| 111 |
submit_btn.click(user_turn, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
|
| 112 |
bot_turn, [chatbot, model_choice, temp, sys_msg], chatbot
|
| 113 |
)
|
|
|
|
|
|
|
| 114 |
clear.click(lambda: [], None, chatbot, queue=False)
|
| 115 |
|
|
|
|
| 116 |
if __name__ == "__main__":
|
| 117 |
+
demo.launch(theme=gr.themes.Soft(primary_hue="blue"), css=custom_css)
|
|
|
|
|
|
|
|
|
|
|
|