File size: 1,651 Bytes
7b2e5db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import gradio as gr
from fpdf import FPDF
from mistletoe import markdown

from research_assistant.app_logging import app_logger
from research_assistant.pipeline.articleSummarization import ArticleSummarization


def process_file(file):
    try:
        app_logger.info(f"Processing file: {file}")
        summary_pipeline = ArticleSummarization(file)
        summary = summary_pipeline.get_summary()
        word_count = len(summary.split())
    except Exception as e:
        summary = f"An error occurred: {e}"
        word_count = 0
    return summary, word_count


def generate_pdf(summary):
    pdf = FPDF()
    pdf.add_page()
    pdf.set_auto_page_break(auto=True, margin=15)
    pdf.set_font("Helvetica", size=12)
    try:
        html_content = markdown(summary)
        pdf.write_html(html_content)
    except Exception as e:
        app_logger.error(f"Error generating PDF: {e}")
        pdf.write(5, "Error generating PDF content.")

    pdf_output_path = "summary.pdf"
    pdf.output(name=pdf_output_path)
    return pdf_output_path


def process_and_generate_pdf(file):
    summary, wordcount = process_file(file)
    pdf_output_path = generate_pdf(summary)
    return summary, wordcount, pdf_output_path


iface = gr.Interface(
    fn=process_and_generate_pdf,
    inputs=gr.File(label="Upload PDF", type="filepath"),
    outputs=[
        gr.Textbox(label="Summary"),
        gr.Number(label="Word Count"),
        gr.File(label="Download PDF"),
    ],
    title="Research Assistant Summarizer",
    description="Summarize your research paper.",
    theme=gr.themes.Default(),
)

if __name__ == "__main__":
    iface.launch(share=True)