Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -374,17 +374,18 @@ def format_quiz_for_display(results):
|
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
-
def analyze_document(document_text: str, api_key: str) ->
|
| 378 |
os.environ["GOOGLE_API_KEY"] = api_key
|
| 379 |
try:
|
| 380 |
results = process_document_with_quiz(document_text)
|
| 381 |
formatted_output = format_quiz_for_display(results)
|
| 382 |
-
return formatted_output
|
| 383 |
except Exception as e:
|
| 384 |
-
|
|
|
|
| 385 |
|
| 386 |
-
with gr.Blocks(title="Quiz Generator
|
| 387 |
-
gr.Markdown("Quiz Generator")
|
| 388 |
|
| 389 |
with gr.Row():
|
| 390 |
with gr.Column():
|
|
@@ -393,13 +394,11 @@ with gr.Blocks(title="Quiz Generator ") as app:
|
|
| 393 |
placeholder="Paste your document text here...",
|
| 394 |
lines=10
|
| 395 |
)
|
| 396 |
-
|
| 397 |
api_key = gr.Textbox(
|
| 398 |
label="Gemini API Key",
|
| 399 |
placeholder="Enter your Gemini API key",
|
| 400 |
type="password"
|
| 401 |
)
|
| 402 |
-
|
| 403 |
analyze_btn = gr.Button("Analyze Document")
|
| 404 |
|
| 405 |
with gr.Column():
|
|
@@ -407,11 +406,49 @@ with gr.Blocks(title="Quiz Generator ") as app:
|
|
| 407 |
label="Analysis Results",
|
| 408 |
lines=20
|
| 409 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 411 |
analyze_btn.click(
|
| 412 |
-
fn=
|
| 413 |
inputs=[input_text, api_key],
|
| 414 |
-
outputs=output_results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
)
|
| 416 |
|
| 417 |
if __name__ == "__main__":
|
|
|
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
+
def analyze_document(document_text: str, api_key: str) -> tuple:
|
| 378 |
os.environ["GOOGLE_API_KEY"] = api_key
|
| 379 |
try:
|
| 380 |
results = process_document_with_quiz(document_text)
|
| 381 |
formatted_output = format_quiz_for_display(results)
|
| 382 |
+
return formatted_output, results
|
| 383 |
except Exception as e:
|
| 384 |
+
error_msg = f"Error processing document: {str(e)}"
|
| 385 |
+
return error_msg, None
|
| 386 |
|
| 387 |
+
with gr.Blocks(title="Quiz Generator") as app:
|
| 388 |
+
gr.Markdown("# Quiz Generator")
|
| 389 |
|
| 390 |
with gr.Row():
|
| 391 |
with gr.Column():
|
|
|
|
| 394 |
placeholder="Paste your document text here...",
|
| 395 |
lines=10
|
| 396 |
)
|
|
|
|
| 397 |
api_key = gr.Textbox(
|
| 398 |
label="Gemini API Key",
|
| 399 |
placeholder="Enter your Gemini API key",
|
| 400 |
type="password"
|
| 401 |
)
|
|
|
|
| 402 |
analyze_btn = gr.Button("Analyze Document")
|
| 403 |
|
| 404 |
with gr.Column():
|
|
|
|
| 406 |
label="Analysis Results",
|
| 407 |
lines=20
|
| 408 |
)
|
| 409 |
+
|
| 410 |
+
with gr.Row():
|
| 411 |
+
json_btn = gr.Button("Download JSON")
|
| 412 |
+
txt_btn = gr.Button("Download TXT")
|
| 413 |
+
|
| 414 |
+
# Create a state to store results
|
| 415 |
+
results_state = gr.State()
|
| 416 |
+
|
| 417 |
+
# Function to handle analysis and store results in state
|
| 418 |
+
def analyze_and_store(text, key):
|
| 419 |
+
formatted, results = analyze_document(text, key)
|
| 420 |
+
return formatted, results
|
| 421 |
+
|
| 422 |
+
# Function to prepare JSON for download
|
| 423 |
+
def prepare_json_download(results):
|
| 424 |
+
if results is None:
|
| 425 |
+
return None
|
| 426 |
+
return {"filename": "analysis_results.json", "content": json.dumps(results, indent=2, ensure_ascii=False)}
|
| 427 |
|
| 428 |
+
# Function to prepare TXT for download
|
| 429 |
+
def prepare_txt_download(text):
|
| 430 |
+
if not text or text.startswith("Error"):
|
| 431 |
+
return None
|
| 432 |
+
return {"filename": "analysis_results.txt", "content": text}
|
| 433 |
+
|
| 434 |
+
# Connect the analyze button
|
| 435 |
analyze_btn.click(
|
| 436 |
+
fn=analyze_and_store,
|
| 437 |
inputs=[input_text, api_key],
|
| 438 |
+
outputs=[output_results, results_state]
|
| 439 |
+
)
|
| 440 |
+
|
| 441 |
+
# Connect download buttons
|
| 442 |
+
json_btn.click(
|
| 443 |
+
fn=prepare_json_download,
|
| 444 |
+
inputs=[results_state],
|
| 445 |
+
outputs=[gr.File(label="Download JSON")]
|
| 446 |
+
)
|
| 447 |
+
|
| 448 |
+
txt_btn.click(
|
| 449 |
+
fn=prepare_txt_download,
|
| 450 |
+
inputs=[output_results],
|
| 451 |
+
outputs=[gr.File(label="Download TXT")]
|
| 452 |
)
|
| 453 |
|
| 454 |
if __name__ == "__main__":
|