File size: 2,122 Bytes
2a9930b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
import fitz  # PyMuPDF
import gradio as gr
from transformers import pipeline
import re

# μš”μ•½μ„ μœ„ν•œ λͺ¨λΈ λ‘œλ“œ
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")

def extract_text_from_pdf(pdf_path):
    doc = fitz.open(pdf_path)
    text = ""
    for page_num in range(doc.page_count):
        page = doc.load_page(page_num)
        text += page.get_text("text") + "\n"
    return text

def find_section(text, section_title):
    # μ •κ·œ ν‘œν˜„μ‹μ„ μ‚¬μš©ν•˜μ—¬ μ„Ήμ…˜ 제λͺ©μ„ μ°ΎμŠ΅λ‹ˆλ‹€.
    pattern = re.compile(r'(?i)^.*{}.*$'.format(section_title), re.MULTILINE)
    matches = list(pattern.finditer(text))
    if not matches:
        return None
    start_idx = matches[0].start()
    end_idx = text.find('\n\n', start_idx)
    if end_idx == -1:
        end_idx = len(text)
    section_text = text[start_idx:end_idx].strip()
    return section_text

def summarize_section(text, section_title, max_length=150):
    try:
        section_text = find_section(text, section_title)
        if section_text:
            summary = summarizer(section_text, max_length=max_length, min_length=30, do_sample=False)
            return summary[0]['summary_text']
        return f"Section '{section_title}' not found."
    except Exception as e:
        return f"Error processing section '{section_title}': {str(e)}"

def process_pdf(file):
    try:
        text = extract_text_from_pdf(file.name)
    except Exception as e:
        return [f"Error extracting text from PDF: {str(e)}"] * 3

    abstract_summary = summarize_section(text, "abstract")
    research_question_summary = summarize_section(text, "research question")
    results_summary = summarize_section(text, "results")
    
    return [abstract_summary, research_question_summary, results_summary]

# Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •
interface = gr.Interface(
    fn=process_pdf,
    inputs=gr.File(label="Upload PDF"),
    outputs=[
        gr.Textbox(label="Abstract Summary"),
        gr.Textbox(label="Research Question Summary"),
        gr.Textbox(label="Results Summary")
    ]
)

# μΈν„°νŽ˜μ΄μŠ€ μ‹€ν–‰
interface.launch()