Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -66,32 +66,51 @@ def process_document(file):
|
|
66 |
global DOCUMENT_TEXT
|
67 |
|
68 |
if file is None:
|
|
|
69 |
return "β No file uploaded"
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
@spaces.GPU
|
88 |
def generate_content(prompt: str, max_tokens: int = 2000) -> str:
|
89 |
"""Generate content using the AI model"""
|
|
|
|
|
90 |
if not API_KEY:
|
91 |
return "β Please configure your API key first"
|
92 |
|
93 |
-
if not DOCUMENT_TEXT:
|
94 |
-
return "β Please upload and process a document first"
|
95 |
|
96 |
try:
|
97 |
client = create_client()
|
@@ -359,6 +378,15 @@ def generate_practice_problems():
|
|
359 |
"""
|
360 |
return generate_content(prompt, max_tokens=3500)
|
361 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
362 |
# Create Gradio interface
|
363 |
def create_interface():
|
364 |
with gr.Blocks(title="π Educational Content Creator Assistant", theme=gr.themes.Soft()) as app:
|
@@ -389,6 +417,9 @@ def create_interface():
|
|
389 |
)
|
390 |
process_btn = gr.Button("π Process Document", variant="secondary")
|
391 |
process_status = gr.Textbox(label="Processing Status", interactive=False)
|
|
|
|
|
|
|
392 |
|
393 |
with gr.Column(scale=2):
|
394 |
gr.Markdown("### π― Generate Educational Content")
|
@@ -444,10 +475,15 @@ def create_interface():
|
|
444 |
outputs=[setup_status]
|
445 |
)
|
446 |
|
|
|
|
|
|
|
|
|
|
|
447 |
process_btn.click(
|
448 |
-
|
449 |
inputs=[file_upload],
|
450 |
-
outputs=[process_status]
|
451 |
)
|
452 |
|
453 |
summary_btn.click(
|
@@ -489,6 +525,12 @@ def create_interface():
|
|
489 |
generate_practice_problems,
|
490 |
outputs=[output]
|
491 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
492 |
|
493 |
return app
|
494 |
|
|
|
66 |
global DOCUMENT_TEXT
|
67 |
|
68 |
if file is None:
|
69 |
+
DOCUMENT_TEXT = "" # Reset if no file
|
70 |
return "β No file uploaded"
|
71 |
|
72 |
+
try:
|
73 |
+
file_path = file.name
|
74 |
+
file_extension = file_path.lower().split('.')[-1]
|
75 |
+
|
76 |
+
if file_extension == 'pdf':
|
77 |
+
extracted_text = extract_text_from_pdf(file_path)
|
78 |
+
elif file_extension in ['docx', 'doc']:
|
79 |
+
extracted_text = extract_text_from_docx(file_path)
|
80 |
+
else:
|
81 |
+
DOCUMENT_TEXT = "" # Reset on unsupported format
|
82 |
+
return "β Unsupported file format. Please upload PDF or DOCX files."
|
83 |
+
|
84 |
+
# Check if extraction was successful
|
85 |
+
if extracted_text.startswith("Error"):
|
86 |
+
DOCUMENT_TEXT = "" # Reset on error
|
87 |
+
return extracted_text
|
88 |
+
|
89 |
+
# Set the global variable
|
90 |
+
DOCUMENT_TEXT = extracted_text.strip()
|
91 |
+
|
92 |
+
if DOCUMENT_TEXT and len(DOCUMENT_TEXT) > 0:
|
93 |
+
word_count = len(DOCUMENT_TEXT.split())
|
94 |
+
preview = DOCUMENT_TEXT[:200] + "..." if len(DOCUMENT_TEXT) > 200 else DOCUMENT_TEXT
|
95 |
+
return f"β
Document processed successfully!\nπ Word count: {word_count}\nπ Preview: {preview}"
|
96 |
+
else:
|
97 |
+
DOCUMENT_TEXT = "" # Reset if no text extracted
|
98 |
+
return "β Could not extract text from the document. The document might be empty or contain only images."
|
99 |
+
|
100 |
+
except Exception as e:
|
101 |
+
DOCUMENT_TEXT = "" # Reset on any error
|
102 |
+
return f"β Error processing document: {str(e)}"
|
103 |
|
104 |
@spaces.GPU
|
105 |
def generate_content(prompt: str, max_tokens: int = 2000) -> str:
|
106 |
"""Generate content using the AI model"""
|
107 |
+
global DOCUMENT_TEXT
|
108 |
+
|
109 |
if not API_KEY:
|
110 |
return "β Please configure your API key first"
|
111 |
|
112 |
+
if not DOCUMENT_TEXT or len(DOCUMENT_TEXT.strip()) == 0:
|
113 |
+
return "β Please upload and process a document first. Make sure the document contains readable text."
|
114 |
|
115 |
try:
|
116 |
client = create_client()
|
|
|
378 |
"""
|
379 |
return generate_content(prompt, max_tokens=3500)
|
380 |
|
381 |
+
def check_document_status():
|
382 |
+
"""Check if document is loaded"""
|
383 |
+
global DOCUMENT_TEXT
|
384 |
+
if DOCUMENT_TEXT and len(DOCUMENT_TEXT.strip()) > 0:
|
385 |
+
word_count = len(DOCUMENT_TEXT.split())
|
386 |
+
return f"β
Document loaded ({word_count} words)"
|
387 |
+
else:
|
388 |
+
return "β No document loaded"
|
389 |
+
|
390 |
# Create Gradio interface
|
391 |
def create_interface():
|
392 |
with gr.Blocks(title="π Educational Content Creator Assistant", theme=gr.themes.Soft()) as app:
|
|
|
417 |
)
|
418 |
process_btn = gr.Button("π Process Document", variant="secondary")
|
419 |
process_status = gr.Textbox(label="Processing Status", interactive=False)
|
420 |
+
|
421 |
+
# Add document status indicator
|
422 |
+
doc_status = gr.Textbox(label="Document Status", value="β No document loaded", interactive=False)
|
423 |
|
424 |
with gr.Column(scale=2):
|
425 |
gr.Markdown("### π― Generate Educational Content")
|
|
|
475 |
outputs=[setup_status]
|
476 |
)
|
477 |
|
478 |
+
def process_and_update_status(file):
|
479 |
+
result = process_document(file)
|
480 |
+
status = check_document_status()
|
481 |
+
return result, status
|
482 |
+
|
483 |
process_btn.click(
|
484 |
+
process_and_update_status,
|
485 |
inputs=[file_upload],
|
486 |
+
outputs=[process_status, doc_status]
|
487 |
)
|
488 |
|
489 |
summary_btn.click(
|
|
|
525 |
generate_practice_problems,
|
526 |
outputs=[output]
|
527 |
)
|
528 |
+
|
529 |
+
# Update document status when page loads
|
530 |
+
app.load(
|
531 |
+
check_document_status,
|
532 |
+
outputs=[doc_status]
|
533 |
+
)
|
534 |
|
535 |
return app
|
536 |
|