Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from groq import Groq
|
|
| 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 {
|
| 12 |
max-width: 100% !important;
|
|
@@ -22,6 +22,12 @@ custom_css = """
|
|
| 22 |
background: white;
|
| 23 |
}
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
#system-prompt-container textarea, .scroll-input textarea {
|
| 26 |
height: 45px !important;
|
| 27 |
max-height: 45px !important;
|
|
@@ -53,9 +59,15 @@ custom_css = """
|
|
| 53 |
border: 1px solid #ddd;
|
| 54 |
min-width: 300px !important;
|
| 55 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
"""
|
| 57 |
|
| 58 |
-
# 3.
|
| 59 |
def chat_with_groq(message, history, model, temperature, system_prompt):
|
| 60 |
cleaned_messages = [{"role": "system", "content": system_prompt}]
|
| 61 |
for msg in history:
|
|
@@ -77,9 +89,18 @@ def chat_with_groq(message, history, model, temperature, system_prompt):
|
|
| 77 |
except Exception as e:
|
| 78 |
yield f"⚠️ API Error: {str(e)}"
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
# 4. Interface Setup
|
| 81 |
-
with gr.Blocks() as demo:
|
| 82 |
sidebar_state = gr.State(False)
|
|
|
|
| 83 |
|
| 84 |
# Header Section
|
| 85 |
with gr.Row():
|
|
@@ -88,12 +109,10 @@ with gr.Blocks() as demo:
|
|
| 88 |
with gr.Column(scale=1, min_width=50):
|
| 89 |
settings_btn = gr.Button("⚙️", elem_id="settings-btn")
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
with gr.Row():
|
| 94 |
# Main Chat Area
|
| 95 |
with gr.Column(scale=4) as chat_col:
|
| 96 |
-
chatbot = gr.Chatbot(elem_id="chatbot-window")
|
| 97 |
with gr.Row():
|
| 98 |
msg_input = gr.Textbox(
|
| 99 |
placeholder="Ask me anything...",
|
|
@@ -106,6 +125,10 @@ with gr.Blocks() as demo:
|
|
| 106 |
# Sidebar Area
|
| 107 |
with gr.Column(scale=1, visible=False, elem_classes="sidebar-panel") as sidebar_col:
|
| 108 |
gr.Markdown("### ⚙️ Global Settings")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
model_choice = gr.Dropdown(
|
| 110 |
choices=["llama-3.3-70b-versatile", "llama-3.1-8b-instant"],
|
| 111 |
value="llama-3.3-70b-versatile", label="Select Model"
|
|
@@ -118,43 +141,35 @@ with gr.Blocks() as demo:
|
|
| 118 |
elem_id="system-prompt-container"
|
| 119 |
)
|
| 120 |
|
| 121 |
-
#
|
| 122 |
-
# Support Section with LinkedIn, Email, and WhatsApp
|
| 123 |
gr.HTML("""
|
| 124 |
<div style="text-align: center; margin-top: 20px; padding-top: 15px; border-top: 1px solid #ddd;">
|
| 125 |
<p style="font-size: 0.85em; color: #555; margin-bottom: 12px;">Connect with the developer</p>
|
| 126 |
<div style="display: flex; justify-content: center; gap: 15px; align-items: center;">
|
| 127 |
-
|
| 128 |
-
<a href="https://
|
| 129 |
-
|
| 130 |
-
<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"/>
|
| 131 |
-
</svg>
|
| 132 |
-
</a>
|
| 133 |
-
|
| 134 |
-
<a href="https://wa.me/923099702809" target="_blank" style="text-decoration: none;">
|
| 135 |
-
<svg width="55" height="55" viewBox="0 0 24 24" fill="#25D366">
|
| 136 |
-
<path d="M12.031 6.172c-3.181 0-5.767 2.586-5.768 5.766-.001 1.298.38 2.27 1.019 3.287l-.582 2.128 2.182-.573c.978.58 1.911.928 3.145.929 3.178 0 5.767-2.587 5.768-5.766 0-3.18-2.587-5.771-5.764-5.771zm3.392 8.244c-.144.405-.837.774-1.17.824-.299.045-.677.063-1.092-.069-.252-.08-.575-.187-.988-.365-1.739-.751-2.874-2.502-2.961-2.617-.087-.116-.708-.94-.708-1.793 0-.852.448-1.271.607-1.442.159-.171.348-.214.464-.214.116 0 .232.001.333.006.101.005.237-.038.37.284.144.35.493 1.203.536 1.29.043.087.072.188.014.304-.058.116-.087.188-.174.289-.087.101-.184.225-.261.304-.093.097-.19.202-.082.387.108.185.479.791 1.029 1.282.709.633 1.308.83 1.493.922.185.093.294.077.404-.051.11-.128.471-.548.598-.736.127-.188.253-.159.426-.096.173.063 1.096.516 1.284.609.188.093.312.139.356.215.044.076.044.439-.1.844z"/>
|
| 137 |
-
</svg>
|
| 138 |
-
</a>
|
| 139 |
-
|
| 140 |
-
<a href="mailto:hk4960498@gmail.com" target="_blank" title="Email: hk4960498@gmail.com" style="text-decoration: none; cursor: pointer;">
|
| 141 |
-
<svg width="30" height="30" viewBox="0 0 24 24" fill="#555">
|
| 142 |
-
<path d="M0 3v18h24v-18h-24zm21.518 2l-9.518 7.713-9.518-7.713h19.036zm-19.518 14v-11.817l10 8.104 10-8.104v11.817h-20z"/>
|
| 143 |
-
</svg>
|
| 144 |
-
</a>
|
| 145 |
-
|
| 146 |
</div>
|
| 147 |
</div>
|
| 148 |
""")
|
| 149 |
|
| 150 |
-
#
|
|
|
|
|
|
|
| 151 |
def toggle_sidebar(current_state):
|
| 152 |
new_state = not current_state
|
| 153 |
return gr.update(visible=new_state), new_state
|
| 154 |
|
| 155 |
settings_btn.click(toggle_sidebar, [sidebar_state], [sidebar_col, sidebar_state])
|
| 156 |
|
| 157 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
def user_turn(msg, hist):
|
| 159 |
if not msg: return "", hist
|
| 160 |
hist.append({"role": "user", "content": msg})
|
|
@@ -178,4 +193,4 @@ with gr.Blocks() as demo:
|
|
| 178 |
|
| 179 |
# Final Launch
|
| 180 |
if __name__ == "__main__":
|
| 181 |
-
demo.launch(
|
|
|
|
| 6 |
api_key = os.environ.get("GROQ_API_KEY")
|
| 7 |
client = Groq(api_key=api_key)
|
| 8 |
|
| 9 |
+
# 2. UI Styling (Added .dark mode support)
|
| 10 |
custom_css = """
|
| 11 |
.gradio-container {
|
| 12 |
max-width: 100% !important;
|
|
|
|
| 22 |
background: white;
|
| 23 |
}
|
| 24 |
|
| 25 |
+
/* Dark Mode Overrides */
|
| 26 |
+
.dark #chatbot-window {
|
| 27 |
+
background: #1a1a1a !important;
|
| 28 |
+
border-color: #444 !important;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
#system-prompt-container textarea, .scroll-input textarea {
|
| 32 |
height: 45px !important;
|
| 33 |
max-height: 45px !important;
|
|
|
|
| 59 |
border: 1px solid #ddd;
|
| 60 |
min-width: 300px !important;
|
| 61 |
}
|
| 62 |
+
|
| 63 |
+
/* Ensure sidebar looks good in dark mode */
|
| 64 |
+
.dark .sidebar-panel {
|
| 65 |
+
background-color: #2d2d2d !important;
|
| 66 |
+
border-color: #444 !important;
|
| 67 |
+
}
|
| 68 |
"""
|
| 69 |
|
| 70 |
+
# 3. Logic Functions
|
| 71 |
def chat_with_groq(message, history, model, temperature, system_prompt):
|
| 72 |
cleaned_messages = [{"role": "system", "content": system_prompt}]
|
| 73 |
for msg in history:
|
|
|
|
| 89 |
except Exception as e:
|
| 90 |
yield f"⚠️ API Error: {str(e)}"
|
| 91 |
|
| 92 |
+
# --- DARK MODE TOGGLE LOGIC ---
|
| 93 |
+
def toggle_dark_mode(current_is_dark):
|
| 94 |
+
new_state = not current_is_dark
|
| 95 |
+
new_label = "☀️ Light Mode" if new_state else "🌙 Dark Mode"
|
| 96 |
+
return new_label, new_state
|
| 97 |
+
|
| 98 |
+
toggle_dark_js = """() => { document.body.classList.toggle('dark'); }"""
|
| 99 |
+
|
| 100 |
# 4. Interface Setup
|
| 101 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 102 |
sidebar_state = gr.State(False)
|
| 103 |
+
dark_state = gr.State(False) # Track dark mode state
|
| 104 |
|
| 105 |
# Header Section
|
| 106 |
with gr.Row():
|
|
|
|
| 109 |
with gr.Column(scale=1, min_width=50):
|
| 110 |
settings_btn = gr.Button("⚙️", elem_id="settings-btn")
|
| 111 |
|
|
|
|
|
|
|
| 112 |
with gr.Row():
|
| 113 |
# Main Chat Area
|
| 114 |
with gr.Column(scale=4) as chat_col:
|
| 115 |
+
chatbot = gr.Chatbot(elem_id="chatbot-window", type="messages")
|
| 116 |
with gr.Row():
|
| 117 |
msg_input = gr.Textbox(
|
| 118 |
placeholder="Ask me anything...",
|
|
|
|
| 125 |
# Sidebar Area
|
| 126 |
with gr.Column(scale=1, visible=False, elem_classes="sidebar-panel") as sidebar_col:
|
| 127 |
gr.Markdown("### ⚙️ Global Settings")
|
| 128 |
+
|
| 129 |
+
# --- THE DARK MODE BUTTON ---
|
| 130 |
+
dark_btn = gr.Button("🌙 Dark Mode", variant="secondary")
|
| 131 |
+
|
| 132 |
model_choice = gr.Dropdown(
|
| 133 |
choices=["llama-3.3-70b-versatile", "llama-3.1-8b-instant"],
|
| 134 |
value="llama-3.3-70b-versatile", label="Select Model"
|
|
|
|
| 141 |
elem_id="system-prompt-container"
|
| 142 |
)
|
| 143 |
|
| 144 |
+
# Support Section
|
|
|
|
| 145 |
gr.HTML("""
|
| 146 |
<div style="text-align: center; margin-top: 20px; padding-top: 15px; border-top: 1px solid #ddd;">
|
| 147 |
<p style="font-size: 0.85em; color: #555; margin-bottom: 12px;">Connect with the developer</p>
|
| 148 |
<div style="display: flex; justify-content: center; gap: 15px; align-items: center;">
|
| 149 |
+
<a href="https://www.linkedin.com/in/hassan-naseer-web-developer" target="_blank"><svg width="30" height="30" viewBox="0 0 24 24" fill="#0077b5"><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"/></svg></a>
|
| 150 |
+
<a href="https://wa.me/923099702809" target="_blank"><svg width="55" height="55" viewBox="0 0 24 24" fill="#25D366"><path d="M12.031 6.172c-3.181 0-5.767 2.586-5.768 5.766-.001 1.298.38 2.27 1.019 3.287l-.582 2.128 2.182-.573c.978.58 1.911.928 3.145.929 3.178 0 5.767-2.587 5.768-5.766 0-3.18-2.587-5.771-5.764-5.771zm3.392 8.244c-.144.405-.837.774-1.17.824-.299.045-.677.063-1.092-.069-.252-.08-.575-.187-.988-.365-1.739-.751-2.874-2.502-2.961-2.617-.087-.116-.708-.94-.708-1.793 0-.852.448-1.271.607-1.442.159-.171.348-.214.464-.214.116 0 .232.001.333.006.101.005.237-.038.37.284.144.35.493 1.203.536 1.29.043.087.072.188.014.304-.058.116-.087.188-.174.289-.087.101-.184.225-.261.304-.093.097-.19.202-.082.387.108.185.479.791 1.029 1.282.709.633 1.308.83 1.493.922.185.093.294.077.404-.051.11-.128.471-.548.598-.736.127-.188.253-.159.426-.096.173.063 1.096.516 1.284.609.188.093.312.139.356.215.044.076.044.439-.1.844z"/></svg></a>
|
| 151 |
+
<a href="mailto:hk4960498@gmail.com" target="_blank"><svg width="30" height="30" viewBox="0 0 24 24" fill="#555"><path d="M0 3v18h24v-18h-24zm21.518 2l-9.518 7.713-9.518-7.713h19.036zm-19.518 14v-11.817l10 8.104 10-8.104v11.817h-20z"/></svg></a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
</div>
|
| 153 |
</div>
|
| 154 |
""")
|
| 155 |
|
| 156 |
+
# --- UI Event Flow ---
|
| 157 |
+
|
| 158 |
+
# Sidebar Toggle
|
| 159 |
def toggle_sidebar(current_state):
|
| 160 |
new_state = not current_state
|
| 161 |
return gr.update(visible=new_state), new_state
|
| 162 |
|
| 163 |
settings_btn.click(toggle_sidebar, [sidebar_state], [sidebar_col, sidebar_state])
|
| 164 |
|
| 165 |
+
# Dark Mode Click
|
| 166 |
+
dark_btn.click(
|
| 167 |
+
toggle_dark_mode,
|
| 168 |
+
inputs=[dark_state],
|
| 169 |
+
outputs=[dark_btn, dark_state],
|
| 170 |
+
js=toggle_dark_js
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
def user_turn(msg, hist):
|
| 174 |
if not msg: return "", hist
|
| 175 |
hist.append({"role": "user", "content": msg})
|
|
|
|
| 193 |
|
| 194 |
# Final Launch
|
| 195 |
if __name__ == "__main__":
|
| 196 |
+
demo.launch()
|