Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,9 @@ import openai
|
|
3 |
import fitz # PyMuPDF for PDF processing
|
4 |
import os
|
5 |
import tempfile
|
|
|
|
|
|
|
6 |
|
7 |
# Variable to store API key
|
8 |
api_key = ""
|
@@ -283,6 +286,57 @@ def save_uploaded_files(files):
|
|
283 |
|
284 |
return saved_paths
|
285 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
286 |
# Add CSS styling for the Gradio interface
|
287 |
custom_css = """
|
288 |
<style>
|
@@ -374,6 +428,35 @@ custom_css = """
|
|
374 |
margin-bottom: 20px;
|
375 |
}
|
376 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
377 |
/* Responsive adjustments */
|
378 |
@media screen and (max-width: 768px) {
|
379 |
.gradio-container {
|
@@ -395,6 +478,7 @@ with gr.Blocks(css=custom_css) as demo:
|
|
395 |
3. Enter your review question or topic
|
396 |
4. Check the "Include Tables" option if you want the review to include comparison tables
|
397 |
5. Click "Generate Systematic Review" to start the process
|
|
|
398 |
|
399 |
### Tips for Best Results:
|
400 |
- Upload papers that are related to the same research topic or field
|
@@ -425,6 +509,9 @@ with gr.Blocks(css=custom_css) as demo:
|
|
425 |
include_tables = gr.Checkbox(label="Include Comparison Tables", value=True)
|
426 |
generate_button = gr.Button("Generate Systematic Review", elem_id="generate_button", size="large")
|
427 |
|
|
|
|
|
|
|
428 |
# Output with improved styling
|
429 |
with gr.Row(elem_classes="output-container"):
|
430 |
review_output = gr.HTML(label="Systematic Review")
|
@@ -435,18 +522,18 @@ with gr.Blocks(css=custom_css) as demo:
|
|
435 |
# Generate systematic review
|
436 |
def process_files_and_generate_review(files, question, include_tables):
|
437 |
if not files:
|
438 |
-
return """
|
439 |
<div style="padding: 20px; border: 1px solid #e0e0e0; border-radius: 5px; background-color: #f9f9f9;">
|
440 |
<h3 style="color: #666;">Please upload at least one PDF file.</h3>
|
441 |
<p>To generate a systematic review, upload one or more research papers in PDF format.</p>
|
442 |
</div>
|
443 |
-
"""
|
444 |
|
445 |
# Save uploaded files
|
446 |
saved_paths = save_uploaded_files(files)
|
447 |
|
448 |
# Show loading message
|
449 |
-
|
450 |
<div style="padding: 20px; text-align: center;">
|
451 |
<h3>Generating Systematic Review...</h3>
|
452 |
<p>This may take a few minutes depending on the number and size of papers.</p>
|
@@ -462,9 +549,26 @@ with gr.Blocks(css=custom_css) as demo:
|
|
462 |
</div>
|
463 |
"""
|
464 |
|
|
|
|
|
465 |
# Generate review
|
466 |
review = generate_systematic_review(saved_paths, question, include_tables)
|
467 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
468 |
# Clean up temporary files
|
469 |
for path in saved_paths:
|
470 |
try:
|
@@ -472,12 +576,12 @@ with gr.Blocks(css=custom_css) as demo:
|
|
472 |
except:
|
473 |
pass
|
474 |
|
475 |
-
yield review
|
476 |
|
477 |
generate_button.click(
|
478 |
process_files_and_generate_review,
|
479 |
inputs=[pdf_files, review_question, include_tables],
|
480 |
-
outputs=[review_output]
|
481 |
)
|
482 |
|
483 |
# Launch the app
|
|
|
3 |
import fitz # PyMuPDF for PDF processing
|
4 |
import os
|
5 |
import tempfile
|
6 |
+
import base64
|
7 |
+
import pdfkit
|
8 |
+
from datetime import datetime
|
9 |
|
10 |
# Variable to store API key
|
11 |
api_key = ""
|
|
|
286 |
|
287 |
return saved_paths
|
288 |
|
289 |
+
# Function to create a downloadable HTML file
|
290 |
+
def create_html_download_link(html_content):
|
291 |
+
if not html_content or "<div style=\"color: red; padding: 20px;" in html_content or "Please upload at least one PDF file" in html_content:
|
292 |
+
return None
|
293 |
+
|
294 |
+
# Create timestamp for the filename
|
295 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
296 |
+
filename = f"systematic_review_{timestamp}.html"
|
297 |
+
|
298 |
+
# Encode the HTML content for download
|
299 |
+
b64_html = base64.b64encode(html_content.encode()).decode()
|
300 |
+
download_link = f'<a href="data:text/html;base64,{b64_html}" download="{filename}" class="download-button">Download HTML</a>'
|
301 |
+
|
302 |
+
return download_link
|
303 |
+
|
304 |
+
# Function to create a downloadable PDF file
|
305 |
+
def create_pdf_download_link(html_content):
|
306 |
+
if not html_content or "<div style=\"color: red; padding: 20px;" in html_content or "Please upload at least one PDF file" in html_content:
|
307 |
+
return None
|
308 |
+
|
309 |
+
try:
|
310 |
+
# Create timestamp for the filename
|
311 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
312 |
+
pdf_filename = f"systematic_review_{timestamp}.pdf"
|
313 |
+
|
314 |
+
# Create a temporary file for the PDF
|
315 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_pdf:
|
316 |
+
pdf_path = tmp_pdf.name
|
317 |
+
|
318 |
+
# Convert HTML to PDF
|
319 |
+
pdfkit.from_string(html_content, pdf_path)
|
320 |
+
|
321 |
+
# Read the PDF file and encode it
|
322 |
+
with open(pdf_path, "rb") as pdf_file:
|
323 |
+
pdf_data = pdf_file.read()
|
324 |
+
|
325 |
+
# Encode the PDF content for download
|
326 |
+
b64_pdf = base64.b64encode(pdf_data).decode()
|
327 |
+
download_link = f'<a href="data:application/pdf;base64,{b64_pdf}" download="{pdf_filename}" class="download-button">Download PDF</a>'
|
328 |
+
|
329 |
+
# Clean up the temporary file
|
330 |
+
try:
|
331 |
+
os.remove(pdf_path)
|
332 |
+
except:
|
333 |
+
pass
|
334 |
+
|
335 |
+
return download_link
|
336 |
+
|
337 |
+
except Exception as e:
|
338 |
+
return f'<div style="color: red; margin-top: 10px;">PDF generation error: {str(e)}</div>'
|
339 |
+
|
340 |
# Add CSS styling for the Gradio interface
|
341 |
custom_css = """
|
342 |
<style>
|
|
|
428 |
margin-bottom: 20px;
|
429 |
}
|
430 |
|
431 |
+
/* Download Buttons */
|
432 |
+
.download-button {
|
433 |
+
display: inline-block;
|
434 |
+
background: linear-gradient(135deg, #4a00e0 0%, #8e2de2 100%);
|
435 |
+
color: white;
|
436 |
+
font-weight: bold;
|
437 |
+
padding: 8px 16px;
|
438 |
+
border-radius: 4px;
|
439 |
+
text-decoration: none;
|
440 |
+
margin-right: 10px;
|
441 |
+
margin-bottom: 10px;
|
442 |
+
transition: all 0.3s ease;
|
443 |
+
}
|
444 |
+
.download-button:hover {
|
445 |
+
background: linear-gradient(135deg, #5b10f1 0%, #9f3ef3 100%);
|
446 |
+
transform: translateY(-2px);
|
447 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
|
448 |
+
}
|
449 |
+
|
450 |
+
/* Download Container */
|
451 |
+
#download-container {
|
452 |
+
display: flex;
|
453 |
+
justify-content: center;
|
454 |
+
margin: 20px 0;
|
455 |
+
padding: 15px;
|
456 |
+
background-color: #f5f5f5;
|
457 |
+
border-radius: 8px;
|
458 |
+
}
|
459 |
+
|
460 |
/* Responsive adjustments */
|
461 |
@media screen and (max-width: 768px) {
|
462 |
.gradio-container {
|
|
|
478 |
3. Enter your review question or topic
|
479 |
4. Check the "Include Tables" option if you want the review to include comparison tables
|
480 |
5. Click "Generate Systematic Review" to start the process
|
481 |
+
6. After generation, you can download the review as HTML or PDF
|
482 |
|
483 |
### Tips for Best Results:
|
484 |
- Upload papers that are related to the same research topic or field
|
|
|
509 |
include_tables = gr.Checkbox(label="Include Comparison Tables", value=True)
|
510 |
generate_button = gr.Button("Generate Systematic Review", elem_id="generate_button", size="large")
|
511 |
|
512 |
+
# Download links container
|
513 |
+
download_html_output = gr.HTML(label="Download Options")
|
514 |
+
|
515 |
# Output with improved styling
|
516 |
with gr.Row(elem_classes="output-container"):
|
517 |
review_output = gr.HTML(label="Systematic Review")
|
|
|
522 |
# Generate systematic review
|
523 |
def process_files_and_generate_review(files, question, include_tables):
|
524 |
if not files:
|
525 |
+
return ("""
|
526 |
<div style="padding: 20px; border: 1px solid #e0e0e0; border-radius: 5px; background-color: #f9f9f9;">
|
527 |
<h3 style="color: #666;">Please upload at least one PDF file.</h3>
|
528 |
<p>To generate a systematic review, upload one or more research papers in PDF format.</p>
|
529 |
</div>
|
530 |
+
""", "")
|
531 |
|
532 |
# Save uploaded files
|
533 |
saved_paths = save_uploaded_files(files)
|
534 |
|
535 |
# Show loading message
|
536 |
+
loading_message = """
|
537 |
<div style="padding: 20px; text-align: center;">
|
538 |
<h3>Generating Systematic Review...</h3>
|
539 |
<p>This may take a few minutes depending on the number and size of papers.</p>
|
|
|
549 |
</div>
|
550 |
"""
|
551 |
|
552 |
+
yield loading_message, ""
|
553 |
+
|
554 |
# Generate review
|
555 |
review = generate_systematic_review(saved_paths, question, include_tables)
|
556 |
|
557 |
+
# Create download links
|
558 |
+
html_link = create_html_download_link(review)
|
559 |
+
pdf_link = create_pdf_download_link(review)
|
560 |
+
|
561 |
+
# Create combined download links HTML
|
562 |
+
download_links = f"""
|
563 |
+
<div id="download-container">
|
564 |
+
<div>
|
565 |
+
<h3>Download Options:</h3>
|
566 |
+
{html_link or ""}
|
567 |
+
{pdf_link or ""}
|
568 |
+
</div>
|
569 |
+
</div>
|
570 |
+
"""
|
571 |
+
|
572 |
# Clean up temporary files
|
573 |
for path in saved_paths:
|
574 |
try:
|
|
|
576 |
except:
|
577 |
pass
|
578 |
|
579 |
+
yield review, download_links
|
580 |
|
581 |
generate_button.click(
|
582 |
process_files_and_generate_review,
|
583 |
inputs=[pdf_files, review_question, include_tables],
|
584 |
+
outputs=[review_output, download_html_output]
|
585 |
)
|
586 |
|
587 |
# Launch the app
|