Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,6 @@ import time
|
|
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,24 +141,6 @@ def process_document(file):
|
|
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,7 +169,7 @@ def generate_content(prompt: str, max_tokens: int = 2000) -> str:
|
|
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
|
192 |
},
|
193 |
{
|
194 |
"role": "user",
|
@@ -202,9 +183,9 @@ def generate_content(prompt: str, max_tokens: int = 2000) -> str:
|
|
202 |
result = completion.choices[0].message.content
|
203 |
print(f"API response received. Length: {len(result) if result else 0}") # Debug print
|
204 |
|
205 |
-
#
|
206 |
-
|
207 |
-
return
|
208 |
|
209 |
except Exception as e:
|
210 |
error_msg = f"β Error generating content: {str(e)}"
|
@@ -338,7 +319,6 @@ def create_download_file(content: str, filename: str) -> str:
|
|
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');
|
@@ -353,36 +333,8 @@ def create_download_file(content: str, filename: str) -> str:
|
|
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>
|
@@ -399,16 +351,7 @@ def create_download_file(content: str, filename: str) -> str:
|
|
399 |
@spaces.GPU
|
400 |
def generate_summary():
|
401 |
"""Generate comprehensive summary"""
|
402 |
-
prompt = """Create a comprehensive summary of this document with proper HTML formatting
|
403 |
-
|
404 |
-
Use these HTML tags for structure:
|
405 |
-
- <h1> for main title
|
406 |
-
- <h2> for section headers (Executive Summary, Key Points, etc.)
|
407 |
-
- <h3> for subsections
|
408 |
-
- <ul> and <li> for bullet points
|
409 |
-
- <p> for paragraphs
|
410 |
-
- <strong> for emphasis
|
411 |
-
- <div class="concept-box"> for important concepts
|
412 |
|
413 |
Structure:
|
414 |
<h1>π Document Summary</h1>
|
@@ -428,23 +371,13 @@ def generate_summary():
|
|
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 |
|
436 |
@spaces.GPU
|
437 |
def generate_study_notes():
|
438 |
"""Generate structured study notes"""
|
439 |
-
prompt = """Create comprehensive study notes from this document using proper HTML formatting
|
440 |
-
|
441 |
-
Use HTML structure with:
|
442 |
-
- <h1> for main title: Study Notes
|
443 |
-
- <h2> for major sections
|
444 |
-
- <h3> for subsections
|
445 |
-
- <ul><li> for lists
|
446 |
-
- <div class="concept-box"> for key concepts
|
447 |
-
- <strong> for important terms
|
448 |
|
449 |
Structure:
|
450 |
<h1>π Study Notes</h1>
|
@@ -468,15 +401,13 @@ def generate_study_notes():
|
|
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 |
|
476 |
@spaces.GPU
|
477 |
def generate_quiz():
|
478 |
"""Generate quiz questions"""
|
479 |
-
prompt = """Create a comprehensive quiz based on this document using HTML formatting
|
480 |
|
481 |
<h1>π Quiz Questions</h1>
|
482 |
|
@@ -510,37 +441,32 @@ def generate_quiz():
|
|
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 |
|
517 |
@spaces.GPU
|
518 |
def generate_flashcards():
|
519 |
"""Generate interactive flashcards"""
|
520 |
-
prompt = """Create 15-20 interactive flashcards based on this document.
|
521 |
|
522 |
Format each flashcard using this exact HTML structure:
|
523 |
|
524 |
<h1>π΄ Interactive Flashcards</h1>
|
525 |
-
<p><em>Click
|
526 |
|
527 |
-
<div class="flashcard"
|
528 |
-
<button class="play-btn"
|
529 |
-
<div class="flashcard-front" id="card1-front">
|
530 |
<h3>Card 1</h3>
|
531 |
<p><strong>[Question/Term]</strong></p>
|
532 |
</div>
|
533 |
-
<div class="flashcard-back" id="card1-back">
|
534 |
<h3>Answer</h3>
|
535 |
<p>[Answer/Definition/Explanation]</p>
|
536 |
</div>
|
537 |
</div>
|
538 |
|
539 |
-
Continue this pattern for each flashcard,
|
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,14 +476,13 @@ def generate_flashcards():
|
|
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 |
|
557 |
@spaces.GPU
|
558 |
def generate_mind_map():
|
559 |
"""Generate mind map structure"""
|
560 |
-
prompt = """Create a detailed mind map structure for this document using HTML
|
561 |
|
562 |
<h1>π§ Mind Map Structure</h1>
|
563 |
|
@@ -596,15 +521,13 @@ def generate_mind_map():
|
|
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 |
|
604 |
@spaces.GPU
|
605 |
def generate_lesson_plan():
|
606 |
"""Generate lesson plan"""
|
607 |
-
prompt = """Create a detailed lesson plan based on this document using HTML formatting
|
608 |
|
609 |
<h1>π Lesson Plan</h1>
|
610 |
|
@@ -663,15 +586,13 @@ def generate_lesson_plan():
|
|
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 |
|
671 |
@spaces.GPU
|
672 |
def generate_concept_explanations():
|
673 |
"""Generate detailed concept explanations"""
|
674 |
-
prompt = """Provide detailed explanations of key concepts from this document using HTML
|
675 |
|
676 |
<h1>π Concept Deep Dive</h1>
|
677 |
|
@@ -697,14 +618,13 @@ def generate_concept_explanations():
|
|
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 |
|
704 |
@spaces.GPU
|
705 |
def generate_practice_problems():
|
706 |
"""Generate practice problems"""
|
707 |
-
prompt = """Create practice problems based on this document using HTML formatting
|
708 |
|
709 |
<h1>πͺ Practice Problems</h1>
|
710 |
|
@@ -751,7 +671,6 @@ def generate_practice_problems():
|
|
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 |
|
@@ -874,7 +793,7 @@ def create_interface():
|
|
874 |
- Make sure your PDF contains selectable text (not just images)
|
875 |
- For best results, use documents with clear structure and headings
|
876 |
- The app works with academic papers, textbooks, reports, and study materials
|
877 |
-
- Interactive flashcards work best when viewed in the HTML
|
878 |
- Downloaded files can be opened in any web browser for offline use
|
879 |
|
880 |
### β‘ Performance Note:
|
|
|
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 |
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 |
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, but do not include <html>, <head>, <body>, or code block markers like ```html or ```. Start directly with the content using tags like <h1>, <h2>, <p>, etc."
|
173 |
},
|
174 |
{
|
175 |
"role": "user",
|
|
|
183 |
result = completion.choices[0].message.content
|
184 |
print(f"API response received. Length: {len(result) if result else 0}") # Debug print
|
185 |
|
186 |
+
# Strip any unwanted code block markers if they sneak in
|
187 |
+
result = result.replace("```html", "").replace("```", "").strip()
|
188 |
+
return result
|
189 |
|
190 |
except Exception as e:
|
191 |
error_msg = f"β Error generating content: {str(e)}"
|
|
|
319 |
</div>
|
320 |
<script>
|
321 |
function toggleCard(cardId) {{
|
|
|
322 |
const front = document.getElementById(cardId + '-front');
|
323 |
const back = document.getElementById(cardId + '-back');
|
324 |
const btn = document.getElementById(cardId + '-btn');
|
|
|
333 |
back.style.display = 'none';
|
334 |
btn.innerHTML = 'βΆοΈ';
|
335 |
}}
|
|
|
|
|
336 |
}}
|
337 |
}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
338 |
</script>
|
339 |
</body>
|
340 |
</html>
|
|
|
351 |
@spaces.GPU
|
352 |
def generate_summary():
|
353 |
"""Generate comprehensive summary"""
|
354 |
+
prompt = """Create a comprehensive summary of this document with proper HTML formatting. Do not include <html>, <head>, <body>, or code block markers. Start directly with the content using tags like <h1>, <h2>, <p>, etc.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
355 |
|
356 |
Structure:
|
357 |
<h1>π Document Summary</h1>
|
|
|
371 |
<div class="concept-box">
|
372 |
<p>Highlight the most crucial information students should remember.</p>
|
373 |
</div>
|
|
|
|
|
374 |
"""
|
375 |
return generate_content(prompt)
|
376 |
|
377 |
@spaces.GPU
|
378 |
def generate_study_notes():
|
379 |
"""Generate structured study notes"""
|
380 |
+
prompt = """Create comprehensive study notes from this document using proper HTML formatting. Do not include <html>, <head>, <body>, or code block markers. Start directly with the content.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
381 |
|
382 |
Structure:
|
383 |
<h1>π Study Notes</h1>
|
|
|
401 |
<ul>
|
402 |
<li>Essential facts and figures for rapid review</li>
|
403 |
</ul>
|
|
|
|
|
404 |
"""
|
405 |
return generate_content(prompt)
|
406 |
|
407 |
@spaces.GPU
|
408 |
def generate_quiz():
|
409 |
"""Generate quiz questions"""
|
410 |
+
prompt = """Create a comprehensive quiz based on this document using HTML formatting. Do not include <html>, <head>, <body>, or code block markers. Start directly with the content.
|
411 |
|
412 |
<h1>π Quiz Questions</h1>
|
413 |
|
|
|
441 |
</div>
|
442 |
|
443 |
Create 5 multiple choice, 5 short answer, and 2 essay questions.
|
|
|
444 |
"""
|
445 |
return generate_content(prompt, max_tokens=3000)
|
446 |
|
447 |
@spaces.GPU
|
448 |
def generate_flashcards():
|
449 |
"""Generate interactive flashcards"""
|
450 |
+
prompt = """Create 15-20 interactive flashcards based on this document. Do not include <html>, <head>, <body>, or code block markers. Start directly with the content.
|
451 |
|
452 |
Format each flashcard using this exact HTML structure:
|
453 |
|
454 |
<h1>π΄ Interactive Flashcards</h1>
|
455 |
+
<p><em>Click the play button (βΆοΈ) on each card to reveal the answer!</em></p>
|
456 |
|
457 |
+
<div class="flashcard" id="card1">
|
458 |
+
<button class="play-btn" id="card1-btn" onclick="toggleCard('card1')">βΆοΈ</button>
|
459 |
+
<div class="flashcard-front" id="card1-front" style="display: block;">
|
460 |
<h3>Card 1</h3>
|
461 |
<p><strong>[Question/Term]</strong></p>
|
462 |
</div>
|
463 |
+
<div class="flashcard-back" id="card1-back" style="display: none;">
|
464 |
<h3>Answer</h3>
|
465 |
<p>[Answer/Definition/Explanation]</p>
|
466 |
</div>
|
467 |
</div>
|
468 |
|
469 |
+
Continue this pattern for each flashcard, incrementing the card numbers (card2, card3, etc.).
|
|
|
|
|
|
|
|
|
470 |
|
471 |
Include flashcards for:
|
472 |
- Key terms and definitions
|
|
|
476 |
- Applications and examples
|
477 |
|
478 |
Make questions clear and answers comprehensive but concise. Use proper HTML formatting throughout.
|
|
|
479 |
"""
|
480 |
return generate_content(prompt, max_tokens=2500)
|
481 |
|
482 |
@spaces.GPU
|
483 |
def generate_mind_map():
|
484 |
"""Generate mind map structure"""
|
485 |
+
prompt = """Create a detailed mind map structure for this document using HTML. Do not include <html>, <head>, <body>, or code block markers. Start directly with the content.
|
486 |
|
487 |
<h1>π§ Mind Map Structure</h1>
|
488 |
|
|
|
521 |
<li>Symbol suggestions for different types of information</li>
|
522 |
<li>Emphasis techniques for key concepts</li>
|
523 |
</ul>
|
|
|
|
|
524 |
"""
|
525 |
return generate_content(prompt)
|
526 |
|
527 |
@spaces.GPU
|
528 |
def generate_lesson_plan():
|
529 |
"""Generate lesson plan"""
|
530 |
+
prompt = """Create a detailed lesson plan based on this document using HTML formatting. Do not include <html>, <head>, <body>, or code block markers. Start directly with the content.
|
531 |
|
532 |
<h1>π Lesson Plan</h1>
|
533 |
|
|
|
586 |
<ul>
|
587 |
<li>Additional practice opportunities</li>
|
588 |
</ul>
|
|
|
|
|
589 |
"""
|
590 |
return generate_content(prompt, max_tokens=2500)
|
591 |
|
592 |
@spaces.GPU
|
593 |
def generate_concept_explanations():
|
594 |
"""Generate detailed concept explanations"""
|
595 |
+
prompt = """Provide detailed explanations of key concepts from this document using HTML. Do not include <html>, <head>, <body>, or code block markers. Start directly with the content.
|
596 |
|
597 |
<h1>π Concept Deep Dive</h1>
|
598 |
|
|
|
618 |
<hr>
|
619 |
|
620 |
Repeat this structure for all major concepts in the document using proper HTML formatting.
|
|
|
621 |
"""
|
622 |
return generate_content(prompt, max_tokens=3000)
|
623 |
|
624 |
@spaces.GPU
|
625 |
def generate_practice_problems():
|
626 |
"""Generate practice problems"""
|
627 |
+
prompt = """Create practice problems based on this document using HTML formatting. Do not include <html>, <head>, <body>, or code block markers. Start directly with the content.
|
628 |
|
629 |
<h1>πͺ Practice Problems</h1>
|
630 |
|
|
|
671 |
- Common mistakes to avoid
|
672 |
|
673 |
Create 5 beginner, 5 intermediate, 3 advanced, and 2 challenge problems.
|
|
|
674 |
"""
|
675 |
return generate_content(prompt, max_tokens=3500)
|
676 |
|
|
|
793 |
- Make sure your PDF contains selectable text (not just images)
|
794 |
- For best results, use documents with clear structure and headings
|
795 |
- The app works with academic papers, textbooks, reports, and study materials
|
796 |
+
- Interactive flashcards work best when viewed in the downloaded HTML file
|
797 |
- Downloaded files can be opened in any web browser for offline use
|
798 |
|
799 |
### β‘ Performance Note:
|