Spaces:
Running
on
Zero
Running
on
Zero
Delete app.py
Browse files
app.py
DELETED
@@ -1,859 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import PyPDF2
|
3 |
-
import docx
|
4 |
-
from openai import OpenAI
|
5 |
-
import io
|
6 |
-
import json
|
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 = ""
|
14 |
-
DOCUMENT_TEXT = ""
|
15 |
-
MODEL = "google/gemma-3-27b-it:free"
|
16 |
-
|
17 |
-
def setup_client(api_key: str):
|
18 |
-
"""Initialize and test API key"""
|
19 |
-
global API_KEY
|
20 |
-
try:
|
21 |
-
if not api_key or api_key.strip() == "":
|
22 |
-
return "❌ Please enter a valid API key"
|
23 |
-
|
24 |
-
client = OpenAI(
|
25 |
-
base_url="https://openrouter.ai/api/v1",
|
26 |
-
api_key=api_key.strip(),
|
27 |
-
)
|
28 |
-
API_KEY = api_key.strip()
|
29 |
-
return "✅ API Key configured successfully!"
|
30 |
-
except Exception as e:
|
31 |
-
return f"❌ Error configuring API: {str(e)}"
|
32 |
-
|
33 |
-
def create_client() -> Optional[OpenAI]:
|
34 |
-
"""Create OpenAI client with stored API key"""
|
35 |
-
if not API_KEY:
|
36 |
-
return None
|
37 |
-
return OpenAI(
|
38 |
-
base_url="https://openrouter.ai/api/v1",
|
39 |
-
api_key=API_KEY,
|
40 |
-
)
|
41 |
-
|
42 |
-
def extract_text_from_pdf(file_path: str) -> str:
|
43 |
-
"""Extract text from PDF file"""
|
44 |
-
try:
|
45 |
-
with open(file_path, 'rb') as file:
|
46 |
-
pdf_reader = PyPDF2.PdfReader(file)
|
47 |
-
text = ""
|
48 |
-
for page_num, page in enumerate(pdf_reader.pages):
|
49 |
-
try:
|
50 |
-
page_text = page.extract_text()
|
51 |
-
if page_text:
|
52 |
-
text += page_text + "\n"
|
53 |
-
except Exception as e:
|
54 |
-
print(f"Error extracting text from page {page_num}: {e}")
|
55 |
-
continue
|
56 |
-
return text.strip()
|
57 |
-
except Exception as e:
|
58 |
-
return f"Error reading PDF: {str(e)}"
|
59 |
-
|
60 |
-
def extract_text_from_docx(file_path: str) -> str:
|
61 |
-
"""Extract text from DOCX file"""
|
62 |
-
try:
|
63 |
-
doc = docx.Document(file_path)
|
64 |
-
text = ""
|
65 |
-
for paragraph in doc.paragraphs:
|
66 |
-
if paragraph.text.strip():
|
67 |
-
text += paragraph.text + "\n"
|
68 |
-
|
69 |
-
for table in doc.tables:
|
70 |
-
for row in table.rows:
|
71 |
-
for cell in row.cells:
|
72 |
-
if cell.text.strip():
|
73 |
-
text += cell.text + "\n"
|
74 |
-
|
75 |
-
return text.strip()
|
76 |
-
except Exception as e:
|
77 |
-
return f"Error reading DOCX: {str(e)}"
|
78 |
-
|
79 |
-
def process_document(file):
|
80 |
-
"""Process uploaded document and extract text"""
|
81 |
-
global DOCUMENT_TEXT
|
82 |
-
|
83 |
-
if file is None:
|
84 |
-
DOCUMENT_TEXT = ""
|
85 |
-
return "❌ No file uploaded", "❌ No document loaded"
|
86 |
-
|
87 |
-
try:
|
88 |
-
file_path = file.name if hasattr(file, 'name') else str(file)
|
89 |
-
|
90 |
-
if not os.path.exists(file_path):
|
91 |
-
DOCUMENT_TEXT = ""
|
92 |
-
return "❌ File not found", "❌ No document loaded"
|
93 |
-
|
94 |
-
file_extension = file_path.lower().split('.')[-1]
|
95 |
-
|
96 |
-
if file_extension == 'pdf':
|
97 |
-
extracted_text = extract_text_from_pdf(file_path)
|
98 |
-
elif file_extension in ['docx', 'doc']:
|
99 |
-
extracted_text = extract_text_from_docx(file_path)
|
100 |
-
else:
|
101 |
-
DOCUMENT_TEXT = ""
|
102 |
-
return "❌ Unsupported file format. Please upload PDF or DOCX files.", "❌ No document loaded"
|
103 |
-
|
104 |
-
if extracted_text.startswith("Error"):
|
105 |
-
DOCUMENT_TEXT = ""
|
106 |
-
return extracted_text, "❌ No document loaded"
|
107 |
-
|
108 |
-
DOCUMENT_TEXT = extracted_text.strip()
|
109 |
-
|
110 |
-
if DOCUMENT_TEXT and len(DOCUMENT_TEXT) > 10:
|
111 |
-
word_count = len(DOCUMENT_TEXT.split())
|
112 |
-
char_count = len(DOCUMENT_TEXT)
|
113 |
-
preview = DOCUMENT_TEXT[:300] + "..." if len(DOCUMENT_TEXT) > 300 else DOCUMENT_TEXT
|
114 |
-
|
115 |
-
status_msg = f"✅ Document loaded ({word_count} words, {char_count} characters)"
|
116 |
-
process_msg = f"✅ Document processed successfully!\n📄 Word count: {word_count}\n📝 Character count: {char_count}\n\n📖 Preview:\n{preview}"
|
117 |
-
return process_msg, status_msg
|
118 |
-
else:
|
119 |
-
DOCUMENT_TEXT = ""
|
120 |
-
return "❌ Could not extract meaningful text from the document.", "❌ No document loaded"
|
121 |
-
|
122 |
-
except Exception as e:
|
123 |
-
DOCUMENT_TEXT = ""
|
124 |
-
return f"❌ Error processing document: {str(e)}", "❌ No document loaded"
|
125 |
-
|
126 |
-
def generate_content(prompt: str, max_tokens: int = 2000) -> str:
|
127 |
-
"""Generate content using the AI model"""
|
128 |
-
global DOCUMENT_TEXT, API_KEY
|
129 |
-
|
130 |
-
if not API_KEY or API_KEY.strip() == "":
|
131 |
-
return "❌ Please configure your API key first"
|
132 |
-
|
133 |
-
if not DOCUMENT_TEXT or len(DOCUMENT_TEXT.strip()) < 10:
|
134 |
-
return "❌ Please upload and process a document first."
|
135 |
-
|
136 |
-
try:
|
137 |
-
client = create_client()
|
138 |
-
if not client:
|
139 |
-
return "❌ Failed to create API client"
|
140 |
-
|
141 |
-
completion = client.chat.completions.create(
|
142 |
-
extra_headers={
|
143 |
-
"HTTP-Referer": "https://educational-assistant.app",
|
144 |
-
"X-Title": "Educational Content Creator",
|
145 |
-
},
|
146 |
-
model=MODEL,
|
147 |
-
messages=[
|
148 |
-
{
|
149 |
-
"role": "system",
|
150 |
-
"content": "You are an expert educational content creator. Create engaging educational materials based on the provided document content. Format your response using proper HTML tags, but do not include <html>, <head>, <body>, or code block markers. Start directly with the content using tags like <h1>, <h2>, <p>, etc."
|
151 |
-
},
|
152 |
-
{
|
153 |
-
"role": "user",
|
154 |
-
"content": f"Document Content:\n{DOCUMENT_TEXT[:4000]}\n\n{prompt}"
|
155 |
-
}
|
156 |
-
],
|
157 |
-
max_tokens=max_tokens,
|
158 |
-
temperature=0.7
|
159 |
-
)
|
160 |
-
|
161 |
-
result = completion.choices[0].message.content
|
162 |
-
result = result.replace("```html", "").replace("```", "").strip()
|
163 |
-
return result
|
164 |
-
|
165 |
-
except Exception as e:
|
166 |
-
return f"❌ Error generating content: {str(e)}"
|
167 |
-
|
168 |
-
def create_download_file(content: str, filename: str) -> str:
|
169 |
-
"""Create downloadable HTML file with full structure and interactivity"""
|
170 |
-
html_template = f"""
|
171 |
-
<!DOCTYPE html>
|
172 |
-
<html lang="en">
|
173 |
-
<head>
|
174 |
-
<meta charset="UTF-8">
|
175 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
176 |
-
<title>Educational Content - {filename}</title>
|
177 |
-
<style>
|
178 |
-
body {{
|
179 |
-
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
180 |
-
line-height: 1.6;
|
181 |
-
margin: 0;
|
182 |
-
padding: 20px;
|
183 |
-
background-color: #f5f5f5;
|
184 |
-
}}
|
185 |
-
.container {{
|
186 |
-
max-width: 1200px;
|
187 |
-
margin: 0 auto;
|
188 |
-
background: white;
|
189 |
-
padding: 30px;
|
190 |
-
border-radius: 10px;
|
191 |
-
box-shadow: 0 0 20px rgba(0,0,0,0.1);
|
192 |
-
}}
|
193 |
-
h1, h2, h3, h4, h5, h6 {{
|
194 |
-
color: #2c3e50;
|
195 |
-
margin-top: 30px;
|
196 |
-
margin-bottom: 15px;
|
197 |
-
}}
|
198 |
-
.flashcard-slider {{
|
199 |
-
width: 100%;
|
200 |
-
max-width: 400px;
|
201 |
-
margin: 20px auto;
|
202 |
-
overflow: hidden;
|
203 |
-
position: relative;
|
204 |
-
cursor: pointer;
|
205 |
-
}}
|
206 |
-
.slides {{
|
207 |
-
display: flex;
|
208 |
-
width: 200%;
|
209 |
-
transition: transform 0.3s ease;
|
210 |
-
}}
|
211 |
-
.slide {{
|
212 |
-
width: 50%;
|
213 |
-
box-sizing: border-box;
|
214 |
-
padding: 30px;
|
215 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
216 |
-
color: white;
|
217 |
-
border-radius: 15px;
|
218 |
-
min-height: 200px;
|
219 |
-
display: flex;
|
220 |
-
flex-direction: column;
|
221 |
-
justify-content: center;
|
222 |
-
text-align: center;
|
223 |
-
}}
|
224 |
-
.showing-answer .slides {{
|
225 |
-
transform: translateX(-50%);
|
226 |
-
}}
|
227 |
-
.quiz-question {{
|
228 |
-
background: #f8f9fa;
|
229 |
-
border-left: 4px solid #007bff;
|
230 |
-
padding: 15px;
|
231 |
-
margin: 15px 0;
|
232 |
-
border-radius: 5px;
|
233 |
-
}}
|
234 |
-
.answer-key {{
|
235 |
-
background: #d4edda;
|
236 |
-
border: 1px solid #c3e6cb;
|
237 |
-
padding: 15px;
|
238 |
-
border-radius: 5px;
|
239 |
-
margin: 10px 0;
|
240 |
-
}}
|
241 |
-
.concept-box {{
|
242 |
-
background: #fff3cd;
|
243 |
-
border: 1px solid #ffeaa7;
|
244 |
-
border-radius: 8px;
|
245 |
-
padding: 20px;
|
246 |
-
margin: 15px 0;
|
247 |
-
}}
|
248 |
-
.mind-map-branch {{
|
249 |
-
background: #e3f2fd;
|
250 |
-
border-left: 4px solid #2196f3;
|
251 |
-
padding: 15px;
|
252 |
-
margin: 10px 0;
|
253 |
-
border-radius: 5px;
|
254 |
-
}}
|
255 |
-
.print-btn {{
|
256 |
-
position: fixed;
|
257 |
-
top: 20px;
|
258 |
-
right: 20px;
|
259 |
-
background: #007bff;
|
260 |
-
color: white;
|
261 |
-
border: none;
|
262 |
-
padding: 10px 20px;
|
263 |
-
border-radius: 5px;
|
264 |
-
cursor: pointer;
|
265 |
-
font-size: 14px;
|
266 |
-
}}
|
267 |
-
@media print {{
|
268 |
-
.print-btn {{ display: none; }}
|
269 |
-
body {{ background: white; }}
|
270 |
-
.container {{ box-shadow: none; }}
|
271 |
-
}}
|
272 |
-
</style>
|
273 |
-
</head>
|
274 |
-
<body>
|
275 |
-
<button class="print-btn" onclick="window.print()">🖨️ Print</button>
|
276 |
-
<div class="container">
|
277 |
-
{content}
|
278 |
-
</div>
|
279 |
-
<script>
|
280 |
-
function toggleFlashcard(id) {{
|
281 |
-
const flashcard = document.getElementById(id);
|
282 |
-
if (flashcard) {{
|
283 |
-
flashcard.classList.toggle('showing-answer');
|
284 |
-
}}
|
285 |
-
}}
|
286 |
-
</script>
|
287 |
-
</body>
|
288 |
-
</html>
|
289 |
-
"""
|
290 |
-
|
291 |
-
temp_filename = f"temp_{filename}.html"
|
292 |
-
with open(temp_filename, 'w', encoding='utf-8') as f:
|
293 |
-
f.write(html_template)
|
294 |
-
|
295 |
-
return temp_filename
|
296 |
-
|
297 |
-
@spaces.GPU
|
298 |
-
def generate_summary():
|
299 |
-
"""Generate comprehensive summary"""
|
300 |
-
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.
|
301 |
-
|
302 |
-
Structure:
|
303 |
-
<h1>📋 Document Summary</h1>
|
304 |
-
|
305 |
-
<h2>🎯 Executive Summary</h2>
|
306 |
-
<p>Provide a brief overview in 2-3 sentences.</p>
|
307 |
-
|
308 |
-
<h2>🔑 Key Points</h2>
|
309 |
-
<ul>
|
310 |
-
<li>List the main concepts, ideas, or arguments presented</li>
|
311 |
-
</ul>
|
312 |
-
|
313 |
-
<h2>📚 Detailed Summary</h2>
|
314 |
-
<p>Provide a thorough summary organized by topics or sections.</p>
|
315 |
-
|
316 |
-
<h2>💡 Important Takeaways</h2>
|
317 |
-
<div class="concept-box">
|
318 |
-
<p>Highlight the most crucial information students should remember.</p>
|
319 |
-
</div>
|
320 |
-
"""
|
321 |
-
return generate_content(prompt)
|
322 |
-
|
323 |
-
@spaces.GPU
|
324 |
-
def generate_study_notes():
|
325 |
-
"""Generate structured study notes"""
|
326 |
-
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.
|
327 |
-
|
328 |
-
Structure:
|
329 |
-
<h1>📖 Study Notes</h1>
|
330 |
-
|
331 |
-
<h2>🔑 Key Concepts</h2>
|
332 |
-
<div class="concept-box">
|
333 |
-
Define important terms and concepts with their significance
|
334 |
-
</div>
|
335 |
-
|
336 |
-
<h2>📊 Main Topics</h2>
|
337 |
-
Organize content into clear sections with topic headings, key points, and supporting details
|
338 |
-
|
339 |
-
<h2>🧠 Memory Aids</h2>
|
340 |
-
<ul>
|
341 |
-
<li>Mnemonics for complex information</li>
|
342 |
-
<li>Visualization techniques</li>
|
343 |
-
<li>Connection points between concepts</li>
|
344 |
-
</ul>
|
345 |
-
|
346 |
-
<h2>⚡ Quick Review Points</h2>
|
347 |
-
<ul>
|
348 |
-
<li>Essential facts and figures for rapid review</li>
|
349 |
-
</ul>
|
350 |
-
"""
|
351 |
-
return generate_content(prompt)
|
352 |
-
|
353 |
-
@spaces.GPU
|
354 |
-
def generate_quiz():
|
355 |
-
"""Generate quiz questions"""
|
356 |
-
prompt = """
|
357 |
-
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.
|
358 |
-
|
359 |
-
<h1>📝 Quiz Questions</h1>
|
360 |
-
|
361 |
-
<h2>Multiple Choice Questions</h2>
|
362 |
-
For each question, use this format:
|
363 |
-
<div class="quiz-question">
|
364 |
-
<h3>Question 1:</h3>
|
365 |
-
<p>[Question text]</p>
|
366 |
-
<ol type="A">
|
367 |
-
<li>Option A</li>
|
368 |
-
<li>Option B</li>
|
369 |
-
<li>Option C</li>
|
370 |
-
<li>Option D</li>
|
371 |
-
</ol>
|
372 |
-
</div>
|
373 |
-
|
374 |
-
<h2>Short Answer Questions</h2>
|
375 |
-
<div class="quiz-question">
|
376 |
-
Questions requiring 2-3 sentence answers covering key concepts and applications.
|
377 |
-
</div>
|
378 |
-
|
379 |
-
<h2>Essay Questions</h2>
|
380 |
-
<div class="quiz-question">
|
381 |
-
Thought-provoking questions requiring detailed responses. Focus on analysis, synthesis, or evaluation.
|
382 |
-
</div>
|
383 |
-
|
384 |
-
<h2>📚 Answer Key</h2>
|
385 |
-
<div class="answer-key">
|
386 |
-
<h3>Multiple Choice Answers:</h3>
|
387 |
-
<p>For each MCQ, provide the correct option and a brief explanation.</p>
|
388 |
-
|
389 |
-
<h3>Short Answer Responses:</h3>
|
390 |
-
<p>For each short answer question, provide a concise model answer.</p>
|
391 |
-
|
392 |
-
<h3>Essay Question Guidelines:</h3>
|
393 |
-
<p>For each essay question, offer a structured outline or key points to include in the response.</p>
|
394 |
-
</div>
|
395 |
-
|
396 |
-
Create 5 multiple choice, 5 short answer, and 2 essay questions.
|
397 |
-
|
398 |
-
Ensure that the Answer Key includes detailed responses for all question types: correct options and explanations for MCQs, model answers for short answer questions, and outlines or key points for essay questions.
|
399 |
-
"""
|
400 |
-
return generate_content(prompt, max_tokens=4000)
|
401 |
-
|
402 |
-
@spaces.GPU
|
403 |
-
def generate_flashcards():
|
404 |
-
"""Generate interactive flashcards with slider functionality"""
|
405 |
-
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.
|
406 |
-
|
407 |
-
Format each flashcard using this exact HTML structure:
|
408 |
-
|
409 |
-
<h1>🎴 Interactive Flashcards</h1>
|
410 |
-
<p><em>Click on a card to slide between question and answer!</em></p>
|
411 |
-
|
412 |
-
<div class="flashcard-slider" id="card1" onclick="toggleFlashcard('card1')">
|
413 |
-
<div class="slides">
|
414 |
-
<div class="slide question">
|
415 |
-
<h3>Card 1</h3>
|
416 |
-
<p><strong>[Question/Term]</strong></p>
|
417 |
-
</div>
|
418 |
-
<div class="slide answer">
|
419 |
-
<h3>Answer</h3>
|
420 |
-
<p>[Answer/Definition/Explanation]</p>
|
421 |
-
</div>
|
422 |
-
</div>
|
423 |
-
</div>
|
424 |
-
|
425 |
-
Continue this pattern for each flashcard, incrementing the card numbers (card2, card3, etc.).
|
426 |
-
|
427 |
-
Include flashcards for:
|
428 |
-
- Key terms and definitions
|
429 |
-
- Important concepts
|
430 |
-
- Facts and figures
|
431 |
-
- Cause and effect relationships
|
432 |
-
- Applications and examples
|
433 |
-
|
434 |
-
Make questions clear and answers comprehensive but concise. Use proper HTML formatting throughout.
|
435 |
-
"""
|
436 |
-
return generate_content(prompt, max_tokens=2500)
|
437 |
-
|
438 |
-
@spaces.GPU
|
439 |
-
def generate_mind_map():
|
440 |
-
"""Generate mind map structure"""
|
441 |
-
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.
|
442 |
-
|
443 |
-
<h1>🧠 Mind Map Structure</h1>
|
444 |
-
|
445 |
-
<div class="concept-box">
|
446 |
-
<h2>Central Topic:</h2>
|
447 |
-
<p><strong>[Main subject of the document]</strong></p>
|
448 |
-
</div>
|
449 |
-
|
450 |
-
<h2>Primary Branches:</h2>
|
451 |
-
|
452 |
-
For each main topic, create branches using this structure:
|
453 |
-
<div class="mind-map-branch">
|
454 |
-
<h3>Branch 1: [Topic Name]</h3>
|
455 |
-
<ul>
|
456 |
-
<li><strong>Sub-branch 1.1:</strong> [Subtopic]
|
457 |
-
<ul>
|
458 |
-
<li>Detail 1.1.1</li>
|
459 |
-
<li>Detail 1.1.2</li>
|
460 |
-
</ul>
|
461 |
-
</li>
|
462 |
-
</ul>
|
463 |
-
</div>
|
464 |
-
|
465 |
-
<h2>🔗 Connections</h2>
|
466 |
-
<div class="concept-box">
|
467 |
-
<ul>
|
468 |
-
<li>Relationships between different branches</li>
|
469 |
-
<li>Cross-references and dependencies</li>
|
470 |
-
<li>Cause-effect relationships</li>
|
471 |
-
</ul>
|
472 |
-
</div>
|
473 |
-
|
474 |
-
<h2>🎨 Visual Elements Suggestions</h2>
|
475 |
-
<ul>
|
476 |
-
<li>Color coding recommendations</li>
|
477 |
-
<li>Symbol suggestions for different types of information</li>
|
478 |
-
<li>Emphasis techniques for key concepts</li>
|
479 |
-
</ul>
|
480 |
-
"""
|
481 |
-
return generate_content(prompt)
|
482 |
-
|
483 |
-
@spaces.GPU
|
484 |
-
def generate_lesson_plan():
|
485 |
-
"""Generate lesson plan"""
|
486 |
-
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.
|
487 |
-
|
488 |
-
<h1>📚 Lesson Plan</h1>
|
489 |
-
|
490 |
-
<h2>🎯 Learning Objectives</h2>
|
491 |
-
<p>By the end of this lesson, students will be able to:</p>
|
492 |
-
<ul>
|
493 |
-
<li>[Specific, measurable objectives]</li>
|
494 |
-
</ul>
|
495 |
-
|
496 |
-
<h2>📋 Prerequisites</h2>
|
497 |
-
<div class="concept-box">
|
498 |
-
<ul>
|
499 |
-
<li>Required background knowledge</li>
|
500 |
-
<li>Recommended prior reading</li>
|
501 |
-
</ul>
|
502 |
-
</div>
|
503 |
-
|
504 |
-
<h2>⏰ Lesson Structure (60 minutes)</h2>
|
505 |
-
|
506 |
-
<h3>Introduction (10 minutes)</h3>
|
507 |
-
<ul>
|
508 |
-
<li>Hook/attention grabber</li>
|
509 |
-
<li>Learning objectives overview</li>
|
510 |
-
</ul>
|
511 |
-
|
512 |
-
<h3>Main Content (35 minutes)</h3>
|
513 |
-
<ul>
|
514 |
-
<li>Key concepts presentation</li>
|
515 |
-
<li>Activities and examples</li>
|
516 |
-
<li>Discussion points</li>
|
517 |
-
</ul>
|
518 |
-
|
519 |
-
<h3>Practice & Application (10 minutes)</h3>
|
520 |
-
<ul>
|
521 |
-
<li>Practice exercises</li>
|
522 |
-
<li>Real-world applications</li>
|
523 |
-
</ul>
|
524 |
-
|
525 |
-
<h3>Wrap-up & Assessment (5 minutes)</h3>
|
526 |
-
<ul>
|
527 |
-
<li>Summary of key points</li>
|
528 |
-
<li>Quick assessment questions</li>
|
529 |
-
</ul>
|
530 |
-
|
531 |
-
<h2>📦 Materials Needed</h2>
|
532 |
-
<ul>
|
533 |
-
<li>List of required resources</li>
|
534 |
-
</ul>
|
535 |
-
|
536 |
-
<h2>📊 Assessment Methods</h2>
|
537 |
-
<div class="answer-key">
|
538 |
-
<p>How to evaluate student understanding</p>
|
539 |
-
</div>
|
540 |
-
|
541 |
-
<h2>📝 Homework/Extension Activities</h2>
|
542 |
-
<ul>
|
543 |
-
<li>Additional practice opportunities</li>
|
544 |
-
</ul>
|
545 |
-
"""
|
546 |
-
return generate_content(prompt, max_tokens=2500)
|
547 |
-
|
548 |
-
@spaces.GPU
|
549 |
-
def generate_concept_explanations():
|
550 |
-
"""Generate detailed concept explanations"""
|
551 |
-
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.
|
552 |
-
|
553 |
-
<h1>🔍 Concept Deep Dive</h1>
|
554 |
-
|
555 |
-
For each major concept, use this structure:
|
556 |
-
|
557 |
-
<div class="concept-box">
|
558 |
-
<h2>[Concept Name]</h2>
|
559 |
-
<p><strong>Definition:</strong> Clear, precise definition</p>
|
560 |
-
|
561 |
-
<p><strong>Explanation:</strong> Detailed explanation in simple terms</p>
|
562 |
-
|
563 |
-
<p><strong>Examples:</strong> Real-world examples and applications</p>
|
564 |
-
|
565 |
-
<p><strong>Analogies:</strong> Helpful comparisons to familiar concepts</p>
|
566 |
-
|
567 |
-
<p><strong>Common Misconceptions:</strong> What students often get wrong</p>
|
568 |
-
|
569 |
-
<p><strong>Connection to Other Concepts:</strong> How it relates to other topics</p>
|
570 |
-
|
571 |
-
<p><strong>Practice Application:</strong> Simple exercise or question</p>
|
572 |
-
</div>
|
573 |
-
|
574 |
-
<hr>
|
575 |
-
|
576 |
-
Repeat this structure for all major concepts in the document using proper HTML formatting.
|
577 |
-
"""
|
578 |
-
return generate_content(prompt, max_tokens=3000)
|
579 |
-
|
580 |
-
@spaces.GPU
|
581 |
-
def generate_practice_problems():
|
582 |
-
"""Generate practice problems"""
|
583 |
-
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.
|
584 |
-
|
585 |
-
<h1>💪 Practice Problems</h1>
|
586 |
-
|
587 |
-
<h2>🟢 Beginner Level</h2>
|
588 |
-
<div class="quiz-question">
|
589 |
-
<h3>Problem 1:</h3>
|
590 |
-
<p>[Problem statement]</p>
|
591 |
-
<div class="answer-key">
|
592 |
-
<p><strong>Solution:</strong> Step-by-step solution provided</p>
|
593 |
-
</div>
|
594 |
-
</div>
|
595 |
-
|
596 |
-
<h2>🟡 Intermediate Level</h2>
|
597 |
-
<div class="quiz-question">
|
598 |
-
<h3>Problem 1:</h3>
|
599 |
-
<p>[Multi-step problem requiring understanding of relationships]</p>
|
600 |
-
<div class="answer-key">
|
601 |
-
<p><strong>Solution:</strong> Guided solutions with explanations</p>
|
602 |
-
</div>
|
603 |
-
</div>
|
604 |
-
|
605 |
-
<h2>🔴 Advanced Level</h2>
|
606 |
-
<div class="quiz-question">
|
607 |
-
<h3>Problem 1:</h3>
|
608 |
-
<p>[Complex scenarios requiring analysis and synthesis]</p>
|
609 |
-
<div class="answer-key">
|
610 |
-
<p><strong>Solution:</strong> Detailed solution strategies</p>
|
611 |
-
</div>
|
612 |
-
</div>
|
613 |
-
|
614 |
-
<h2>🏆 Challenge Problems</h2>
|
615 |
-
<div class="concept-box">
|
616 |
-
<h3>Challenge 1:</h3>
|
617 |
-
<p>[Extension beyond document content with creative application]</p>
|
618 |
-
<div class="answer-key">
|
619 |
-
<p><strong>Multiple Solution Approaches:</strong> Various ways to solve</p>
|
620 |
-
</div>
|
621 |
-
</div>
|
622 |
-
|
623 |
-
For each problem, include:
|
624 |
-
- Clear problem statement with HTML formatting
|
625 |
-
- Required formulas/concepts in <strong> tags
|
626 |
-
- Step-by-step solution in answer-key divs
|
627 |
-
- Common mistakes to avoid
|
628 |
-
|
629 |
-
Create 5 beginner, 5 intermediate, 3 advanced, and 2 challenge problems.
|
630 |
-
"""
|
631 |
-
return generate_content(prompt, max_tokens=3500)
|
632 |
-
|
633 |
-
def get_document_status():
|
634 |
-
"""Get current document status"""
|
635 |
-
global DOCUMENT_TEXT
|
636 |
-
if DOCUMENT_TEXT and len(DOCUMENT_TEXT.strip()) > 10:
|
637 |
-
word_count = len(DOCUMENT_TEXT.split())
|
638 |
-
char_count = len(DOCUMENT_TEXT)
|
639 |
-
return f"✅ Document loaded ({word_count} words, {char_count} characters)"
|
640 |
-
else:
|
641 |
-
return "❌ No document loaded"
|
642 |
-
|
643 |
-
def get_api_status():
|
644 |
-
"""Get current API status"""
|
645 |
-
global API_KEY
|
646 |
-
if API_KEY and API_KEY.strip():
|
647 |
-
return "✅ API Key configured"
|
648 |
-
else:
|
649 |
-
return "❌ API Key not configured"
|
650 |
-
|
651 |
-
def create_interface():
|
652 |
-
with gr.Blocks(title="📚 Educational Content Creator Assistant", theme=gr.themes.Soft()) as app:
|
653 |
-
gr.Markdown("""
|
654 |
-
# 📚 Educational Content Creator Assistant
|
655 |
-
Transform your documents into comprehensive educational materials using AI!
|
656 |
-
|
657 |
-
**New Feature:**
|
658 |
-
- 🎴 Interactive Flashcards now work like an image slider—click to slide between question and answer!
|
659 |
-
|
660 |
-
*Powered by ZeroGPU for enhanced performance*
|
661 |
-
""")
|
662 |
-
|
663 |
-
with gr.Row():
|
664 |
-
with gr.Column(scale=1):
|
665 |
-
gr.Markdown("### 🔑 Setup")
|
666 |
-
api_key = gr.Textbox(
|
667 |
-
label="OpenRouter API Key",
|
668 |
-
type="password",
|
669 |
-
placeholder="Enter your OpenRouter API key...",
|
670 |
-
info="Get your API key from https://openrouter.ai/"
|
671 |
-
)
|
672 |
-
setup_btn = gr.Button("🔧 Configure API", variant="primary")
|
673 |
-
setup_status = gr.Textbox(label="API Status", value=get_api_status(), interactive=False)
|
674 |
-
|
675 |
-
gr.Markdown("### 📄 Document Upload")
|
676 |
-
file_upload = gr.File(
|
677 |
-
label="Upload Document (PDF or DOCX)",
|
678 |
-
file_types=[".pdf", ".docx", ".doc"],
|
679 |
-
type="filepath"
|
680 |
-
)
|
681 |
-
process_btn = gr.Button("🔄 Process Document", variant="secondary")
|
682 |
-
process_status = gr.Textbox(label="Processing Status", interactive=False, lines=4)
|
683 |
-
|
684 |
-
doc_status = gr.Textbox(
|
685 |
-
label="Document Status",
|
686 |
-
value=get_document_status(),
|
687 |
-
interactive=False
|
688 |
-
)
|
689 |
-
|
690 |
-
with gr.Column(scale=2):
|
691 |
-
gr.Markdown("### 🎯 Generate Educational Content")
|
692 |
-
|
693 |
-
with gr.Row():
|
694 |
-
summary_btn = gr.Button("📋 Generate Summary", variant="primary")
|
695 |
-
notes_btn = gr.Button("📖 Study Notes", variant="primary")
|
696 |
-
quiz_btn = gr.Button("📝 Create Quiz", variant="primary")
|
697 |
-
|
698 |
-
with gr.Row():
|
699 |
-
flashcards_btn = gr.Button("🎴 Interactive Flashcards", variant="secondary")
|
700 |
-
mindmap_btn = gr.Button("🧠 Mind Map", variant="secondary")
|
701 |
-
lesson_btn = gr.Button("📚 Lesson Plan", variant="secondary")
|
702 |
-
|
703 |
-
with gr.Row():
|
704 |
-
concepts_btn = gr.Button("🔍 Concept Explanations", variant="secondary")
|
705 |
-
problems_btn = gr.Button("💪 Practice Problems", variant="secondary")
|
706 |
-
|
707 |
-
output = gr.HTML(
|
708 |
-
label="Generated Content",
|
709 |
-
value="<p>Generated content will appear here...</p>"
|
710 |
-
)
|
711 |
-
|
712 |
-
with gr.Row():
|
713 |
-
download_file = gr.File(label="📥 Download Content", visible=False)
|
714 |
-
download_btn = gr.Button("💾 Prepare Download", variant="secondary", visible=False)
|
715 |
-
|
716 |
-
gr.Markdown("""
|
717 |
-
### ��� How to Use:
|
718 |
-
1. **Get API Key:** Sign up at [OpenRouter](https://openrouter.ai/)
|
719 |
-
2. **Configure:** Enter your API key and click "Configure API"
|
720 |
-
3. **Upload:** Upload a PDF or DOCX document
|
721 |
-
4. **Process:** Click "Process Document" to extract text
|
722 |
-
5. **Generate:** Choose a content type to generate
|
723 |
-
6. **Download:** Download the HTML file to see interactive flashcards
|
724 |
-
|
725 |
-
### 💡 Tips:
|
726 |
-
- Flashcards slide between question and answer when clicked (in the downloaded file).
|
727 |
-
- Use the downloaded HTML file in a browser for full interactivity.
|
728 |
-
""")
|
729 |
-
|
730 |
-
last_content = gr.State("")
|
731 |
-
last_content_type = gr.State("")
|
732 |
-
|
733 |
-
def setup_api_and_update_status(api_key):
|
734 |
-
result = setup_client(api_key)
|
735 |
-
status = get_api_status()
|
736 |
-
return result, status
|
737 |
-
|
738 |
-
setup_btn.click(
|
739 |
-
setup_api_and_update_status,
|
740 |
-
inputs=[api_key],
|
741 |
-
outputs=[setup_status, setup_status]
|
742 |
-
)
|
743 |
-
|
744 |
-
def process_and_update_all_status(file):
|
745 |
-
process_result, doc_status_result = process_document(file)
|
746 |
-
return process_result, doc_status_result
|
747 |
-
|
748 |
-
process_btn.click(
|
749 |
-
process_and_update_all_status,
|
750 |
-
inputs=[file_upload],
|
751 |
-
outputs=[process_status, doc_status]
|
752 |
-
)
|
753 |
-
|
754 |
-
def generate_and_display_summary():
|
755 |
-
content = generate_summary()
|
756 |
-
return content, content, "summary", gr.update(visible=True)
|
757 |
-
|
758 |
-
def generate_and_display_notes():
|
759 |
-
content = generate_study_notes()
|
760 |
-
return content, content, "study_notes", gr.update(visible=True)
|
761 |
-
|
762 |
-
def generate_and_display_quiz():
|
763 |
-
content = generate_quiz()
|
764 |
-
return content, content, "quiz", gr.update(visible=True)
|
765 |
-
|
766 |
-
def generate_and_display_flashcards():
|
767 |
-
content = generate_flashcards()
|
768 |
-
return content, content, "flashcards", gr.update(visible=True)
|
769 |
-
|
770 |
-
def generate_and_display_mindmap():
|
771 |
-
content = generate_mind_map()
|
772 |
-
return content, content, "mind_map", gr.update(visible=True)
|
773 |
-
|
774 |
-
def generate_and_display_lesson():
|
775 |
-
content = generate_lesson_plan()
|
776 |
-
return content, content, "lesson_plan", gr.update(visible=True)
|
777 |
-
|
778 |
-
def generate_and_display_concepts():
|
779 |
-
content = generate_concept_explanations()
|
780 |
-
return content, content, "concept_explanations", gr.update(visible=True)
|
781 |
-
|
782 |
-
def generate_and_display_problems():
|
783 |
-
content = generate_practice_problems()
|
784 |
-
return content, content, "practice_problems", gr.update(visible=True)
|
785 |
-
|
786 |
-
def prepare_download(content, content_type):
|
787 |
-
if not content or content.startswith("❌"):
|
788 |
-
return None
|
789 |
-
temp_file = create_download_file(content, content_type)
|
790 |
-
return temp_file
|
791 |
-
|
792 |
-
summary_btn.click(
|
793 |
-
generate_and_display_summary,
|
794 |
-
outputs=[output, last_content, last_content_type, download_btn]
|
795 |
-
)
|
796 |
-
|
797 |
-
notes_btn.click(
|
798 |
-
generate_and_display_notes,
|
799 |
-
outputs=[output, last_content, last_content_type, download_btn]
|
800 |
-
)
|
801 |
-
|
802 |
-
quiz_btn.click(
|
803 |
-
generate_and_display_quiz,
|
804 |
-
outputs=[output, last_content, last_content_type, download_btn]
|
805 |
-
)
|
806 |
-
|
807 |
-
flashcards_btn.click(
|
808 |
-
generate_and_display_flashcards,
|
809 |
-
outputs=[output, last_content, last_content_type, download_btn]
|
810 |
-
)
|
811 |
-
|
812 |
-
mindmap_btn.click(
|
813 |
-
generate_and_display_mindmap,
|
814 |
-
outputs=[output, last_content, last_content_type, download_btn]
|
815 |
-
)
|
816 |
-
|
817 |
-
lesson_btn.click(
|
818 |
-
generate_and_display_lesson,
|
819 |
-
outputs=[output, last_content, last_content_type, download_btn]
|
820 |
-
)
|
821 |
-
|
822 |
-
concepts_btn.click(
|
823 |
-
generate_and_display_concepts,
|
824 |
-
outputs=[output, last_content, last_content_type, download_btn]
|
825 |
-
)
|
826 |
-
|
827 |
-
problems_btn.click(
|
828 |
-
generate_and_display_problems,
|
829 |
-
outputs=[output, last_content, last_content_type, download_btn]
|
830 |
-
)
|
831 |
-
|
832 |
-
def handle_download(content, content_type):
|
833 |
-
if content and not content.startswith("❌"):
|
834 |
-
temp_file = prepare_download(content, content_type)
|
835 |
-
return temp_file, gr.update(visible=True)
|
836 |
-
return None, gr.update(visible=False)
|
837 |
-
|
838 |
-
download_btn.click(
|
839 |
-
handle_download,
|
840 |
-
inputs=[last_content, last_content_type],
|
841 |
-
outputs=[download_file, download_file]
|
842 |
-
)
|
843 |
-
|
844 |
-
def update_initial_status():
|
845 |
-
return get_api_status(), get_document_status()
|
846 |
-
|
847 |
-
app.load(
|
848 |
-
update_initial_status,
|
849 |
-
outputs=[setup_status, doc_status]
|
850 |
-
)
|
851 |
-
|
852 |
-
return app
|
853 |
-
|
854 |
-
if __name__ == "__main__":
|
855 |
-
app = create_interface()
|
856 |
-
app.launch(
|
857 |
-
debug=True,
|
858 |
-
share=False
|
859 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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