jdesiree commited on
Commit
1057a95
·
verified ·
1 Parent(s): f5d14de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -317
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  from langchain.prompts import ChatPromptTemplate
3
  from langchain_huggingface import HuggingFaceEndpoint
4
  from langchain.schema import HumanMessage, SystemMessage
5
- from langchain.callbacks.base import BaseCallbackHandler
6
  import os
7
  import time
8
  import logging
@@ -115,7 +114,7 @@ def smart_truncate(text, max_length=3000):
115
 
116
  def respond_with_enhanced_streaming(
117
  message,
118
- history: list[tuple[str, str]],
119
  system_message,
120
  max_tokens,
121
  temperature,
@@ -155,7 +154,7 @@ def respond_with_enhanced_streaming(
155
  # Update every 4 words for smooth streaming effect
156
  if i % 4 == 0:
157
  yield partial_response
158
- time.sleep(0.03) # Slightly faster streaming
159
 
160
  # Final complete response
161
  final_response = f"*{mode}*\n\n{response}"
@@ -166,7 +165,7 @@ def respond_with_enhanced_streaming(
166
  logger.exception("Error in LangChain response generation")
167
  yield f"Sorry, I encountered an error: {str(e)[:150]}"
168
 
169
- # Create enhanced Gradio interface (simplified for compatibility)
170
  demo = gr.ChatInterface(
171
  respond_with_enhanced_streaming,
172
  title="🎓 EduBot | AI Learning Assistant",
@@ -182,316 +181,4 @@ demo = gr.ChatInterface(
182
  📚 **Educational Modes:**
183
  • 🧮 **Math Mode** - Step-by-step problem solving with detailed explanations
184
  • 🔍 **Research Mode** - Source finding, evaluation, and citation guidance
185
- • 📚 **Study Mode** - Learning strategies and exam preparation techniques
186
- • 🎓 **General Mode** - Comprehensive educational support
187
-
188
- 💡 **Tip:** Try asking detailed questions for thorough explanations!
189
- """,
190
- examples=[
191
- ["Solve the quadratic equation x² + 5x + 6 = 0 with complete step-by-step explanations"],
192
- ["How do I conduct a comprehensive literature review for my psychology research paper?"],
193
- ["Create a detailed study schedule for my calculus and chemistry final exams"],
194
- ["Explain derivatives in calculus with real-world applications and examples"],
195
- ["How do I properly format citations in APA style with detailed guidelines?"]
196
- ],
197
- additional_inputs=[
198
- gr.Textbox(
199
- value="You are EduBot, an expert AI learning assistant. Provide comprehensive, educational responses that help students truly understand concepts.",
200
- label="Custom System Message",
201
- placeholder="Customize how EduBot behaves...",
202
- lines=2
203
- ),
204
- gr.Slider(
205
- minimum=1,
206
- maximum=1024,
207
- value=600,
208
- step=1,
209
- label="Max Tokens"
210
- ),
211
- gr.Slider(
212
- minimum=0.1,
213
- maximum=2.0,
214
- value=0.7,
215
- step=0.1,
216
- label="Temperature"
217
- ),
218
- gr.Slider(
219
- minimum=0.1,
220
- maximum=1.0,
221
- value=0.9,
222
- step=0.05,
223
- label="Top-p"
224
- ),
225
- ],
226
- theme=gr.themes.Soft(
227
- primary_hue="blue",
228
- secondary_hue="green"
229
- )
230
- )
231
-
232
- if __name__ == "__main__":
233
- logger.info("Starting EduBot application...")
234
- demo.launch() def on_llm_end(self, response, **kwargs):
235
- """Called when LLM finishes generating"""
236
- self.is_streaming = False
237
- logger.info(f"LLM generation completed. Total tokens: {len(self.tokens)}")
238
-
239
- def on_llm_error(self, error, **kwargs):
240
- """Called when LLM encounters an error"""
241
- self.is_streaming = False
242
- logger.error(f"LLM error: {error}")
243
- self.text += f"\n[Error: {str(error)[:100]}]"
244
-
245
- # Set up LangChain model with conservative settings
246
- llm = HuggingFaceEndpoint(
247
- repo_id="HuggingFaceH4/zephyr-7b-beta",
248
- temperature=0.7,
249
- top_p=0.9,
250
- repetition_penalty=1.1,
251
- model_kwargs={"max_length": 1024}, # More conservative
252
- huggingfacehub_api_token=os.getenv("HUGGINGFACEHUB_API_TOKEN")
253
- )
254
-
255
- # Enhanced prompt templates that use system_message parameter
256
- math_template = ChatPromptTemplate.from_messages([
257
- ("system", """{system_message}
258
-
259
- You are an expert math tutor. For every math problem:
260
- 1. Break it down step-by-step with detailed explanations
261
- 2. Explain the reasoning behind each step thoroughly
262
- 3. Show all work clearly with proper mathematical notation
263
- 4. Check your answer and explain why it's correct
264
- 5. Provide additional examples if helpful
265
- 6. Explain the underlying mathematical concepts
266
-
267
- Be comprehensive and educational. Structure your response clearly."""),
268
- ("human", "{question}")
269
- ])
270
-
271
- research_template = ChatPromptTemplate.from_messages([
272
- ("system", """{system_message}
273
-
274
- You are a research skills mentor. Help students with:
275
- - Finding reliable and credible sources
276
- - Evaluating source credibility and bias
277
- - Proper citation formats (APA, MLA, Chicago, etc.)
278
- - Research strategies and methodologies
279
- - Academic writing techniques and structure
280
- - Database navigation and search strategies
281
-
282
- Provide detailed, actionable advice with specific examples."""),
283
- ("human", "{question}")
284
- ])
285
-
286
- study_template = ChatPromptTemplate.from_messages([
287
- ("system", """{system_message}
288
-
289
- You are a study skills coach. Help students with:
290
- - Effective study methods for different learning styles
291
- - Time management and scheduling techniques
292
- - Memory techniques and retention strategies
293
- - Test preparation and exam strategies
294
- - Note-taking methods and organization
295
- - Learning style optimization
296
-
297
- Provide comprehensive, personalized advice with practical examples."""),
298
- ("human", "{question}")
299
- ])
300
-
301
- general_template = ChatPromptTemplate.from_messages([
302
- ("system", """{system_message}
303
-
304
- You are EduBot, a comprehensive AI learning assistant. You help students with:
305
- 📐 Mathematics (detailed step-by-step solutions and concept explanations)
306
- 🔍 Research skills (source finding, evaluation, and citation)
307
- 📚 Study strategies (effective learning techniques and exam preparation)
308
- 🛠️ Educational tools (guidance on learning resources and technologies)
309
-
310
- Always be encouraging, patient, thorough, and comprehensive."""),
311
- ("human", "{question}")
312
- ])
313
-
314
- def detect_subject(message):
315
- """Determine which prompt template to use based on the message"""
316
- message_lower = message.lower()
317
-
318
- math_keywords = ['math', 'solve', 'calculate', 'equation', 'formula', 'algebra', 'geometry', 'calculus', 'derivative', 'integral', 'theorem', 'proof']
319
- research_keywords = ['research', 'source', 'citation', 'bibliography', 'reference', 'academic', 'paper', 'essay', 'thesis', 'database', 'journal']
320
- study_keywords = ['study', 'memorize', 'exam', 'test', 'quiz', 'review', 'learn', 'remember', 'focus', 'motivation', 'notes']
321
-
322
- if any(keyword in message_lower for keyword in math_keywords):
323
- return math_template, "🧮 Math Mode"
324
- elif any(keyword in message_lower for keyword in research_keywords):
325
- return research_template, "🔍 Research Mode"
326
- elif any(keyword in message_lower for keyword in study_keywords):
327
- return study_template, "📚 Study Mode"
328
- else:
329
- return general_template, "🎓 General Mode"
330
-
331
- def smart_truncate(text, max_length=3000):
332
- """Intelligently truncate text at sentence boundaries"""
333
- if len(text) <= max_length:
334
- return text
335
-
336
- # Try to cut at last complete sentence
337
- sentences = re.split(r'(?<=[.!?]) +', text[:max_length])
338
- if len(sentences) > 1:
339
- # Remove the last incomplete sentence
340
- return ' '.join(sentences[:-1]) + "... [Response truncated - ask for continuation]"
341
- else:
342
- # Fallback to word boundary
343
- words = text[:max_length].split()
344
- return ' '.join(words[:-1]) + "... [Response truncated - ask for continuation]"
345
-
346
- def respond_with_enhanced_streaming(
347
- message,
348
- history: list[tuple[str, str]],
349
- system_message,
350
- max_tokens,
351
- temperature,
352
- top_p,
353
- ):
354
- """Enhanced LangChain implementation with proper system message handling"""
355
-
356
- try:
357
- # Select template and get mode
358
- template, mode = detect_subject(message)
359
-
360
- # Create LangChain chain
361
- chain = template | llm
362
-
363
- # Show initial mode
364
- yield f"*{mode}*\n\nGenerating response..."
365
-
366
- # Get complete response from LangChain with system message
367
- logger.info(f"Processing {mode} query: {message[:50]}...")
368
-
369
- response = chain.invoke({
370
- "question": message,
371
- "system_message": system_message or "You are EduBot, an AI learning assistant."
372
- })
373
-
374
- # Smart truncation at sentence boundaries
375
- response = smart_truncate(response, max_length=3000)
376
-
377
- # Simulate streaming by chunking the response
378
- words = response.split()
379
- partial_response = f"*{mode}*\n\n"
380
-
381
- # Stream word by word for better UX
382
- for i, word in enumerate(words):
383
- partial_response += word + " "
384
-
385
- # Update every 4 words for smooth streaming effect
386
- if i % 4 == 0:
387
- yield partial_response
388
- time.sleep(0.03) # Slightly faster streaming
389
-
390
- # Final complete response
391
- final_response = f"*{mode}*\n\n{response}"
392
- logger.info(f"Response completed. Length: {len(response)} characters")
393
- yield final_response
394
-
395
- except Exception as e:
396
- logger.exception("Error in LangChain response generation")
397
- yield f"Sorry, I encountered an error: {str(e)[:150]}"
398
-
399
- # Create enhanced Gradio interface with custom theme
400
- custom_theme = gr.themes.Soft(
401
- primary_hue="blue",
402
- secondary_hue="green",
403
- neutral_hue="slate",
404
- ).set(
405
- body_background_fill="linear-gradient(45deg, #f0f9ff, #ecfdf5)",
406
- button_primary_background_fill="#2563eb",
407
- button_primary_text_color="white",
408
- )
409
-
410
- # Create the main interface
411
- demo = gr.ChatInterface(
412
- respond_with_enhanced_streaming,
413
- title="🎓 EduBot | AI Learning Assistant",
414
- description="""
415
- **Your comprehensive AI tutor powered by LangChain!**
416
-
417
- 🔧 **Technical Features:**
418
- • Dynamic prompt templates based on question type
419
- • LangChain chain composition with `|` operator
420
- • Smart response truncation at sentence boundaries
421
- • Enhanced error handling and logging
422
-
423
- 📚 **Educational Modes:**
424
- • 🧮 **Math Mode** - Step-by-step problem solving with detailed explanations
425
- • 🔍 **Research Mode** - Source finding, evaluation, and citation guidance
426
- • 📚 **Study Mode** - Learning strategies and exam preparation techniques
427
- • 🎓 **General Mode** - Comprehensive educational support
428
-
429
- 💡 **Tip:** Try asking detailed questions for thorough explanations!
430
- """,
431
- examples=[
432
- ["Solve the quadratic equation x² + 5x + 6 = 0 with complete step-by-step explanations"],
433
- ["How do I conduct a comprehensive literature review for my psychology research paper?"],
434
- ["Create a detailed study schedule for my calculus and chemistry final exams"],
435
- ["Explain derivatives in calculus with real-world applications and examples"],
436
- ["How do I properly format citations in APA style with detailed guidelines?"]
437
- ],
438
- additional_inputs=[
439
- gr.Textbox(
440
- value="You are EduBot, an expert AI learning assistant. Provide comprehensive, educational responses that help students truly understand concepts.",
441
- label="Custom System Message",
442
- placeholder="Customize how EduBot behaves...",
443
- lines=2,
444
- visible=True # Make it visible so users can customize
445
- ),
446
- gr.Slider(
447
- minimum=1,
448
- maximum=1024,
449
- value=600,
450
- step=1,
451
- label="Max Tokens",
452
- info="Higher values = longer responses"
453
- ),
454
- gr.Slider(
455
- minimum=0.1,
456
- maximum=2.0,
457
- value=0.7,
458
- step=0.1,
459
- label="Temperature",
460
- info="Higher values = more creative responses"
461
- ),
462
- gr.Slider(
463
- minimum=0.1,
464
- maximum=1.0,
465
- value=0.9,
466
- step=0.05,
467
- label="Top-p",
468
- info="Controls response diversity"
469
- ),
470
- ],
471
- theme=custom_theme,
472
- chatbot=gr.Chatbot(
473
- height=500,
474
- show_copy_button=True,
475
- avatar_images=["👤", "🎓"],
476
- bubble_full_width=False,
477
- show_share_button=True,
478
- placeholder="Hi! I'm EduBot. What would you like to learn today? 📚"
479
- ),
480
- textbox=gr.Textbox(
481
- placeholder="Ask me about math, research, study strategies, or any educational topic...",
482
- container=False,
483
- scale=7
484
- ),
485
- retry_btn="🔄 Retry",
486
- undo_btn="↩️ Undo",
487
- clear_btn="🗑️ Clear",
488
- )
489
-
490
- if __name__ == "__main__":
491
- logger.info("Starting EduBot application...")
492
- demo.launch(
493
- share=False, # Set to True if you want a public link
494
- show_error=True,
495
- favicon_path=None,
496
- show_api=False
497
- )
 
2
  from langchain.prompts import ChatPromptTemplate
3
  from langchain_huggingface import HuggingFaceEndpoint
4
  from langchain.schema import HumanMessage, SystemMessage
 
5
  import os
6
  import time
7
  import logging
 
114
 
115
  def respond_with_enhanced_streaming(
116
  message,
117
+ history,
118
  system_message,
119
  max_tokens,
120
  temperature,
 
154
  # Update every 4 words for smooth streaming effect
155
  if i % 4 == 0:
156
  yield partial_response
157
+ time.sleep(0.03)
158
 
159
  # Final complete response
160
  final_response = f"*{mode}*\n\n{response}"
 
165
  logger.exception("Error in LangChain response generation")
166
  yield f"Sorry, I encountered an error: {str(e)[:150]}"
167
 
168
+ # Create enhanced Gradio interface
169
  demo = gr.ChatInterface(
170
  respond_with_enhanced_streaming,
171
  title="🎓 EduBot | AI Learning Assistant",
 
181
  📚 **Educational Modes:**
182
  • 🧮 **Math Mode** - Step-by-step problem solving with detailed explanations
183
  • 🔍 **Research Mode** - Source finding, evaluation, and citation guidance
184
+ • 📚 **Study Mode** - Learning strategies and exam preparation techniques