Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -165,69 +165,151 @@ def respond_with_enhanced_streaming(
|
|
| 165 |
logger.exception("Error in LangChain response generation")
|
| 166 |
yield f"Sorry, I encountered an error: {str(e)[:150]}"
|
| 167 |
|
| 168 |
-
#
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
|
|
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
gr.Slider(
|
| 211 |
-
minimum=0.1,
|
| 212 |
-
maximum=2.0,
|
| 213 |
-
value=0.7,
|
| 214 |
-
step=0.1,
|
| 215 |
-
label="Temperature"
|
| 216 |
-
),
|
| 217 |
-
gr.Slider(
|
| 218 |
-
minimum=0.1,
|
| 219 |
-
maximum=1.0,
|
| 220 |
-
value=0.9,
|
| 221 |
-
step=0.05,
|
| 222 |
-
label="Top-p"
|
| 223 |
-
),
|
| 224 |
-
],
|
| 225 |
-
theme=gr.themes.Soft(
|
| 226 |
-
primary_hue="blue",
|
| 227 |
-
secondary_hue="green"
|
| 228 |
)
|
| 229 |
-
)
|
| 230 |
|
| 231 |
if __name__ == "__main__":
|
| 232 |
-
logger.info("Starting EduBot application...")
|
| 233 |
demo.launch()
|
|
|
|
| 165 |
logger.exception("Error in LangChain response generation")
|
| 166 |
yield f"Sorry, I encountered an error: {str(e)[:150]}"
|
| 167 |
|
| 168 |
+
# Your custom Glass theme with amber accent
|
| 169 |
+
theme = gr.themes.Glass(
|
| 170 |
+
primary_hue="amber",
|
| 171 |
+
text_size="md",
|
| 172 |
+
radius_size="md",
|
| 173 |
+
).set(
|
| 174 |
+
shadow_drop='*shadow_drop_lg',
|
| 175 |
+
shadow_drop_lg='*shadow_drop',
|
| 176 |
+
shadow_spread='*shadow_drop_lg',
|
| 177 |
+
block_info_text_weight='500',
|
| 178 |
+
button_border_width='*block_border_width'
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
# Create enhanced Gradio interface with your custom theme
|
| 182 |
+
with gr.Blocks(theme=theme) as demo:
|
| 183 |
+
|
| 184 |
+
gr.HTML("""
|
| 185 |
+
<div style='text-align: center; margin-bottom: 2rem;'>
|
| 186 |
+
<h1 style='font-size: 2.5rem; font-weight: bold; color: #d97706; margin-bottom: 0.5rem;'>
|
| 187 |
+
๐ EduBot | AI Learning Assistant
|
| 188 |
+
</h1>
|
| 189 |
+
<p style='font-size: 1.2rem; color: #6b7280; margin-bottom: 1rem;'>
|
| 190 |
+
Your comprehensive AI tutor powered by LangChain!
|
| 191 |
+
</p>
|
| 192 |
+
<div style='background: rgba(217, 119, 6, 0.1); padding: 1rem; border-radius: 10px; margin: 1rem auto; max-width: 800px;'>
|
| 193 |
+
<p style='margin: 0; font-weight: 500;'>
|
| 194 |
+
๐ง <strong>LangChain Features:</strong> Dynamic prompt templates โข Chain composition โข Smart truncation โข Enhanced error handling
|
| 195 |
+
</p>
|
| 196 |
+
</div>
|
| 197 |
+
</div>
|
| 198 |
+
""")
|
| 199 |
+
|
| 200 |
+
with gr.Row():
|
| 201 |
+
with gr.Column(scale=3):
|
| 202 |
+
# Main chat interface
|
| 203 |
+
chatbot = gr.Chatbot(
|
| 204 |
+
height=500,
|
| 205 |
+
show_copy_button=True,
|
| 206 |
+
avatar_images=["๐ค", "๐"],
|
| 207 |
+
placeholder="Hi! I'm EduBot. What would you like to learn today? ๐"
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
msg = gr.Textbox(
|
| 211 |
+
placeholder="Ask me about math, research, study strategies, or any educational topic...",
|
| 212 |
+
container=False,
|
| 213 |
+
scale=7,
|
| 214 |
+
show_label=False
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
with gr.Row():
|
| 218 |
+
clear_btn = gr.Button("๐๏ธ Clear Chat", variant="secondary", size="sm")
|
| 219 |
+
send_btn = gr.Button("๐ค Send Message", variant="primary", size="sm")
|
| 220 |
+
|
| 221 |
+
with gr.Column(scale=1):
|
| 222 |
+
gr.HTML("""
|
| 223 |
+
<div style='background: rgba(217, 119, 6, 0.1); padding: 1rem; border-radius: 10px; margin-bottom: 1rem;'>
|
| 224 |
+
<h3 style='margin-top: 0; color: #d97706;'>๐ Educational Modes</h3>
|
| 225 |
+
<ul style='margin: 0; padding-left: 1.2rem;'>
|
| 226 |
+
<li><strong>๐งฎ Math Mode</strong> - Step-by-step solutions</li>
|
| 227 |
+
<li><strong>๐ Research Mode</strong> - Source finding & citations</li>
|
| 228 |
+
<li><strong>๐ Study Mode</strong> - Learning strategies</li>
|
| 229 |
+
<li><strong>๐ General Mode</strong> - Comprehensive support</li>
|
| 230 |
+
</ul>
|
| 231 |
+
</div>
|
| 232 |
+
""")
|
| 233 |
+
|
| 234 |
+
system_message = gr.Textbox(
|
| 235 |
+
value="You are EduBot, an expert AI learning assistant. Provide comprehensive, educational responses that help students truly understand concepts.",
|
| 236 |
+
label="Custom System Message",
|
| 237 |
+
placeholder="Customize how EduBot behaves...",
|
| 238 |
+
lines=3
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 242 |
+
max_tokens = gr.Slider(
|
| 243 |
+
minimum=1,
|
| 244 |
+
maximum=1024,
|
| 245 |
+
value=600,
|
| 246 |
+
step=1,
|
| 247 |
+
label="Max Tokens",
|
| 248 |
+
info="Higher = longer responses"
|
| 249 |
+
)
|
| 250 |
+
|
| 251 |
+
temperature = gr.Slider(
|
| 252 |
+
minimum=0.1,
|
| 253 |
+
maximum=2.0,
|
| 254 |
+
value=0.7,
|
| 255 |
+
step=0.1,
|
| 256 |
+
label="Temperature",
|
| 257 |
+
info="Higher = more creative"
|
| 258 |
+
)
|
| 259 |
+
|
| 260 |
+
top_p = gr.Slider(
|
| 261 |
+
minimum=0.1,
|
| 262 |
+
maximum=1.0,
|
| 263 |
+
value=0.9,
|
| 264 |
+
step=0.05,
|
| 265 |
+
label="Top-p",
|
| 266 |
+
info="Controls diversity"
|
| 267 |
+
)
|
| 268 |
|
| 269 |
+
# Example questions section
|
| 270 |
+
gr.HTML("""
|
| 271 |
+
<div style='background: rgba(217, 119, 6, 0.05); padding: 1.5rem; border-radius: 10px; margin-top: 1rem;'>
|
| 272 |
+
<h3 style='margin-top: 0; color: #d97706; text-align: center;'>๐ก Try These Example Questions</h3>
|
| 273 |
+
</div>
|
| 274 |
+
""")
|
| 275 |
|
| 276 |
+
examples = gr.Examples(
|
| 277 |
+
examples=[
|
| 278 |
+
["Solve the quadratic equation xยฒ + 5x + 6 = 0 with complete step-by-step explanations"],
|
| 279 |
+
["How do I conduct a comprehensive literature review for my psychology research paper?"],
|
| 280 |
+
["Create a detailed study schedule for my calculus and chemistry final exams"],
|
| 281 |
+
["Explain derivatives in calculus with real-world applications and examples"],
|
| 282 |
+
["How do I properly format citations in APA style with detailed guidelines?"]
|
| 283 |
+
],
|
| 284 |
+
inputs=msg,
|
| 285 |
+
examples_per_page=5
|
| 286 |
+
)
|
| 287 |
|
| 288 |
+
# Enhanced response function for Blocks
|
| 289 |
+
def respond_and_update(message, history, system_msg, max_tok, temp, top_p):
|
| 290 |
+
# Get the streaming response
|
| 291 |
+
for response in respond_with_enhanced_streaming(message, history, system_msg, max_tok, temp, top_p):
|
| 292 |
+
history.append([message, response])
|
| 293 |
+
yield history, ""
|
| 294 |
+
|
| 295 |
+
# Event handlers
|
| 296 |
+
msg.submit(
|
| 297 |
+
respond_and_update,
|
| 298 |
+
[msg, chatbot, system_message, max_tokens, temperature, top_p],
|
| 299 |
+
[chatbot, msg]
|
| 300 |
+
)
|
| 301 |
+
|
| 302 |
+
send_btn.click(
|
| 303 |
+
respond_and_update,
|
| 304 |
+
[msg, chatbot, system_message, max_tokens, temperature, top_p],
|
| 305 |
+
[chatbot, msg]
|
| 306 |
+
)
|
| 307 |
+
|
| 308 |
+
clear_btn.click(
|
| 309 |
+
lambda: ([], ""),
|
| 310 |
+
outputs=[chatbot, msg]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
)
|
|
|
|
| 312 |
|
| 313 |
if __name__ == "__main__":
|
| 314 |
+
logger.info("Starting EduBot application with custom Glass theme...")
|
| 315 |
demo.launch()
|