shukdevdatta123 commited on
Commit
beaa443
·
verified ·
1 Parent(s): ca71860

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -13
app.py CHANGED
@@ -8,6 +8,7 @@ import time
8
  from typing import List, Dict, Any, Optional
9
  import spaces
10
  import os
 
11
 
12
  # Global variables to store API key and document text
13
  API_KEY = ""
@@ -141,6 +142,24 @@ def process_document(file):
141
  print(f"Error: {error_msg}") # Debug print
142
  return error_msg, "❌ No document loaded"
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  def generate_content(prompt: str, max_tokens: int = 2000) -> str:
145
  """Generate content using the AI model"""
146
  global DOCUMENT_TEXT, API_KEY
@@ -169,7 +188,7 @@ def generate_content(prompt: str, max_tokens: int = 2000) -> str:
169
  messages=[
170
  {
171
  "role": "system",
172
- "content": "You are an expert educational content creator. Create comprehensive, engaging, and pedagogically sound educational materials based on the provided document content. Format your response using proper HTML tags for better presentation."
173
  },
174
  {
175
  "role": "user",
@@ -182,7 +201,10 @@ def generate_content(prompt: str, max_tokens: int = 2000) -> str:
182
 
183
  result = completion.choices[0].message.content
184
  print(f"API response received. Length: {len(result) if result else 0}") # Debug print
185
- return result
 
 
 
186
 
187
  except Exception as e:
188
  error_msg = f"❌ Error generating content: {str(e)}"
@@ -316,20 +338,51 @@ def create_download_file(content: str, filename: str) -> str:
316
  </div>
317
  <script>
318
  function toggleCard(cardId) {{
 
319
  const front = document.getElementById(cardId + '-front');
320
  const back = document.getElementById(cardId + '-back');
321
  const btn = document.getElementById(cardId + '-btn');
322
 
323
- if (front.style.display !== 'none') {{
324
- front.style.display = 'none';
325
- back.style.display = 'block';
326
- btn.innerHTML = '🔄';
 
 
 
 
 
 
327
  }} else {{
328
- front.style.display = 'block';
329
- back.style.display = 'none';
330
- btn.innerHTML = '▶️';
331
  }}
332
  }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
  </script>
334
  </body>
335
  </html>
@@ -375,6 +428,8 @@ def generate_summary():
375
  <div class="concept-box">
376
  <p>Highlight the most crucial information students should remember.</p>
377
  </div>
 
 
378
  """
379
  return generate_content(prompt)
380
 
@@ -413,6 +468,8 @@ def generate_study_notes():
413
  <ul>
414
  <li>Essential facts and figures for rapid review</li>
415
  </ul>
 
 
416
  """
417
  return generate_content(prompt)
418
 
@@ -453,6 +510,7 @@ def generate_quiz():
453
  </div>
454
 
455
  Create 5 multiple choice, 5 short answer, and 2 essay questions.
 
456
  """
457
  return generate_content(prompt, max_tokens=3000)
458
 
@@ -464,10 +522,10 @@ def generate_flashcards():
464
  Format each flashcard using this exact HTML structure:
465
 
466
  <h1>🎴 Interactive Flashcards</h1>
467
- <p><em>Click the play button (▶️) on each card to reveal the answer!</em></p>
468
 
469
- <div class="flashcard" onclick="toggleCard('card1')">
470
- <button class="play-btn" id="card1-btn">▶️</button>
471
  <div class="flashcard-front" id="card1-front">
472
  <h3>Card 1</h3>
473
  <p><strong>[Question/Term]</strong></p>
@@ -478,7 +536,11 @@ def generate_flashcards():
478
  </div>
479
  </div>
480
 
481
- Continue this pattern for each flashcard, incrementing the card numbers (card2, card3, etc.).
 
 
 
 
482
 
483
  Include flashcards for:
484
  - Key terms and definitions
@@ -488,6 +550,7 @@ def generate_flashcards():
488
  - Applications and examples
489
 
490
  Make questions clear and answers comprehensive but concise. Use proper HTML formatting throughout.
 
491
  """
492
  return generate_content(prompt, max_tokens=2500)
493
 
@@ -533,6 +596,8 @@ def generate_mind_map():
533
  <li>Symbol suggestions for different types of information</li>
534
  <li>Emphasis techniques for key concepts</li>
535
  </ul>
 
 
536
  """
537
  return generate_content(prompt)
538
 
@@ -598,6 +663,8 @@ def generate_lesson_plan():
598
  <ul>
599
  <li>Additional practice opportunities</li>
600
  </ul>
 
 
601
  """
602
  return generate_content(prompt, max_tokens=2500)
603
 
@@ -630,6 +697,7 @@ def generate_concept_explanations():
630
  <hr>
631
 
632
  Repeat this structure for all major concepts in the document using proper HTML formatting.
 
633
  """
634
  return generate_content(prompt, max_tokens=3000)
635
 
@@ -683,6 +751,7 @@ def generate_practice_problems():
683
  - Common mistakes to avoid
684
 
685
  Create 5 beginner, 5 intermediate, 3 advanced, and 2 challenge problems.
 
686
  """
687
  return generate_content(prompt, max_tokens=3500)
688
 
 
8
  from typing import List, Dict, Any, Optional
9
  import spaces
10
  import os
11
+ import re
12
 
13
  # Global variables to store API key and document text
14
  API_KEY = ""
 
142
  print(f"Error: {error_msg}") # Debug print
143
  return error_msg, "❌ No document loaded"
144
 
145
+ def clean_html_response(content: str) -> str:
146
+ """Clean HTML response by removing markdown code blocks and fixing formatting"""
147
+ if not content:
148
+ return content
149
+
150
+ # Remove ```html and ``` blocks
151
+ content = re.sub(r'```html\s*', '', content)
152
+ content = re.sub(r'```\s*$', '', content, flags=re.MULTILINE)
153
+ content = re.sub(r'```', '', content)
154
+
155
+ # Remove any remaining markdown code block indicators
156
+ content = re.sub(r'^```.*?\n', '', content, flags=re.MULTILINE)
157
+
158
+ # Clean up extra whitespace
159
+ content = re.sub(r'\n\s*\n\s*\n', '\n\n', content)
160
+
161
+ return content.strip()
162
+
163
  def generate_content(prompt: str, max_tokens: int = 2000) -> str:
164
  """Generate content using the AI model"""
165
  global DOCUMENT_TEXT, API_KEY
 
188
  messages=[
189
  {
190
  "role": "system",
191
+ "content": "You are an expert educational content creator. Create comprehensive, engaging, and pedagogically sound educational materials based on the provided document content. Format your response using proper HTML tags for better presentation. IMPORTANT: Do not wrap your response in ```html or ``` code blocks. Return clean HTML content only."
192
  },
193
  {
194
  "role": "user",
 
201
 
202
  result = completion.choices[0].message.content
203
  print(f"API response received. Length: {len(result) if result else 0}") # Debug print
204
+
205
+ # Clean the HTML response
206
+ cleaned_result = clean_html_response(result)
207
+ return cleaned_result
208
 
209
  except Exception as e:
210
  error_msg = f"❌ Error generating content: {str(e)}"
 
338
  </div>
339
  <script>
340
  function toggleCard(cardId) {{
341
+ console.log('Toggling card:', cardId);
342
  const front = document.getElementById(cardId + '-front');
343
  const back = document.getElementById(cardId + '-back');
344
  const btn = document.getElementById(cardId + '-btn');
345
 
346
+ if (front && back && btn) {{
347
+ if (front.style.display !== 'none') {{
348
+ front.style.display = 'none';
349
+ back.style.display = 'block';
350
+ btn.innerHTML = '🔄';
351
+ }} else {{
352
+ front.style.display = 'block';
353
+ back.style.display = 'none';
354
+ btn.innerHTML = '▶️';
355
+ }}
356
  }} else {{
357
+ console.error('Could not find elements for card:', cardId);
 
 
358
  }}
359
  }}
360
+
361
+ // Alternative method - listen for clicks on flashcards
362
+ document.addEventListener('DOMContentLoaded', function() {{
363
+ const flashcards = document.querySelectorAll('.flashcard');
364
+ flashcards.forEach(function(card) {{
365
+ card.addEventListener('click', function(e) {{
366
+ e.preventDefault();
367
+ const cardId = this.getAttribute('data-card-id');
368
+ if (cardId) {{
369
+ toggleCard(cardId);
370
+ }}
371
+ }});
372
+ }});
373
+
374
+ // Also listen for button clicks
375
+ const playBtns = document.querySelectorAll('.play-btn');
376
+ playBtns.forEach(function(btn) {{
377
+ btn.addEventListener('click', function(e) {{
378
+ e.stopPropagation();
379
+ const cardId = this.getAttribute('data-card-id');
380
+ if (cardId) {{
381
+ toggleCard(cardId);
382
+ }}
383
+ }});
384
+ }});
385
+ }});
386
  </script>
387
  </body>
388
  </html>
 
428
  <div class="concept-box">
429
  <p>Highlight the most crucial information students should remember.</p>
430
  </div>
431
+
432
+ Remember: Return only clean HTML content without code block markers.
433
  """
434
  return generate_content(prompt)
435
 
 
468
  <ul>
469
  <li>Essential facts and figures for rapid review</li>
470
  </ul>
471
+
472
+ Remember: Return only clean HTML content without code block markers.
473
  """
474
  return generate_content(prompt)
475
 
 
510
  </div>
511
 
512
  Create 5 multiple choice, 5 short answer, and 2 essay questions.
513
+ Remember: Return only clean HTML content without code block markers.
514
  """
515
  return generate_content(prompt, max_tokens=3000)
516
 
 
522
  Format each flashcard using this exact HTML structure:
523
 
524
  <h1>🎴 Interactive Flashcards</h1>
525
+ <p><em>Click on each card or the play button (▶️) to reveal the answer!</em></p>
526
 
527
+ <div class="flashcard" data-card-id="card1">
528
+ <button class="play-btn" data-card-id="card1" id="card1-btn">▶️</button>
529
  <div class="flashcard-front" id="card1-front">
530
  <h3>Card 1</h3>
531
  <p><strong>[Question/Term]</strong></p>
 
536
  </div>
537
  </div>
538
 
539
+ Continue this pattern for each flashcard, ensuring:
540
+ - Each card has a unique data-card-id (card1, card2, card3, etc.)
541
+ - Button has matching data-card-id attribute
542
+ - Front and back divs have correct IDs (card1-front, card1-back, etc.)
543
+ - Button has correct ID (card1-btn, card2-btn, etc.)
544
 
545
  Include flashcards for:
546
  - Key terms and definitions
 
550
  - Applications and examples
551
 
552
  Make questions clear and answers comprehensive but concise. Use proper HTML formatting throughout.
553
+ Remember: Return only clean HTML content without code block markers.
554
  """
555
  return generate_content(prompt, max_tokens=2500)
556
 
 
596
  <li>Symbol suggestions for different types of information</li>
597
  <li>Emphasis techniques for key concepts</li>
598
  </ul>
599
+
600
+ Remember: Return only clean HTML content without code block markers.
601
  """
602
  return generate_content(prompt)
603
 
 
663
  <ul>
664
  <li>Additional practice opportunities</li>
665
  </ul>
666
+
667
+ Remember: Return only clean HTML content without code block markers.
668
  """
669
  return generate_content(prompt, max_tokens=2500)
670
 
 
697
  <hr>
698
 
699
  Repeat this structure for all major concepts in the document using proper HTML formatting.
700
+ Remember: Return only clean HTML content without code block markers.
701
  """
702
  return generate_content(prompt, max_tokens=3000)
703
 
 
751
  - Common mistakes to avoid
752
 
753
  Create 5 beginner, 5 intermediate, 3 advanced, and 2 challenge problems.
754
+ Remember: Return only clean HTML content without code block markers.
755
  """
756
  return generate_content(prompt, max_tokens=3500)
757