Spaces:
Sleeping
Sleeping
new gradio
Browse files
app.py
CHANGED
|
@@ -373,16 +373,22 @@ def format_quiz_for_display(results):
|
|
| 373 |
return "\n".join(output)
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|
|
@@ -406,49 +412,13 @@ with gr.Blocks(title="Quiz Generator") as app:
|
|
| 406 |
label="Analysis Results",
|
| 407 |
lines=20
|
| 408 |
)
|
| 409 |
-
|
| 410 |
-
|
| 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=
|
| 437 |
inputs=[input_text, api_key],
|
| 438 |
-
outputs=[output_results,
|
| 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__":
|
|
|
|
| 373 |
return "\n".join(output)
|
| 374 |
|
| 375 |
|
|
|
|
| 376 |
def analyze_document(document_text: str, api_key: str) -> tuple:
|
| 377 |
os.environ["GOOGLE_API_KEY"] = api_key
|
| 378 |
try:
|
| 379 |
results = process_document_with_quiz(document_text)
|
| 380 |
formatted_output = format_quiz_for_display(results)
|
| 381 |
+
json_path = "analysis_results.json"
|
| 382 |
+
txt_path = "analysis_results.txt"
|
| 383 |
+
with open(json_path, "w", encoding="utf-8") as f:
|
| 384 |
+
json.dump(results, f, indent=2, ensure_ascii=False)
|
| 385 |
+
with open(txt_path, "w", encoding="utf-8") as f:
|
| 386 |
+
f.write(formatted_output)
|
| 387 |
+
|
| 388 |
+
return formatted_output, json_path, txt_path
|
| 389 |
except Exception as e:
|
| 390 |
error_msg = f"Error processing document: {str(e)}"
|
| 391 |
+
return error_msg, None, None
|
| 392 |
|
| 393 |
with gr.Blocks(title="Quiz Generator") as app:
|
| 394 |
gr.Markdown("# Quiz Generator")
|
|
|
|
| 412 |
label="Analysis Results",
|
| 413 |
lines=20
|
| 414 |
)
|
| 415 |
+
json_file_output = gr.File(label="Download JSON")
|
| 416 |
+
txt_file_output = gr.File(label="Download TXT")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
analyze_btn.click(
|
| 419 |
+
fn=analyze_document,
|
| 420 |
inputs=[input_text, api_key],
|
| 421 |
+
outputs=[output_results, json_file_output, txt_file_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
)
|
| 423 |
|
| 424 |
if __name__ == "__main__":
|