File size: 1,101 Bytes
2d71bf6
 
 
0669fa1
2d71bf6
 
b30eb63
2d71bf6
 
 
 
30fd9a1
2d71bf6
b30eb63
 
2d71bf6
 
 
 
 
 
 
 
0669fa1
2d71bf6
0669fa1
2d71bf6
b30eb63
 
0669fa1
 
b30eb63
 
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
import gradio as gr
import PyPDF2
import io

# PDF νŒŒμΌμ—μ„œ ν…μŠ€νŠΈλ₯Ό μΆ”μΆœν•˜λŠ” ν•¨μˆ˜
def extract_text_from_pdf(pdf_file):
    reader = PyPDF2.PdfFileReader(io.BytesIO(pdf_file))
    text = ""
    for page in range(reader.numPages):
        text += reader.getPage(page).extractText()
    return text

# μΆ”μΆœλœ ν…μŠ€νŠΈλ₯Ό 기반으둜 μ§ˆλ¬Έμ— λ‹΅λ³€ν•˜λŠ” ν•¨μˆ˜
def answer_question(pdf_file, question):
    extracted_text = extract_text_from_pdf(pdf_file)
    # μ—¬κΈ°μ—μ„œλŠ” κ°„λ‹¨ν•˜κ²Œ ν…μŠ€νŠΈμ—μ„œ 질문과 μœ μ‚¬ν•œ 뢀뢄을 μ°Ύμ•„ λ°˜ν™˜ν•©λ‹ˆλ‹€.
    # 보닀 λ³΅μž‘ν•œ λ‘œμ§μ„ κ΅¬ν˜„ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.
    if question in extracted_text:
        start = extracted_text.find(question)
        end = extracted_text.find('.', start) + 1
        return extracted_text[start:end]
    else:
        return "μ§ˆλ¬Έμ— λŒ€ν•œ 닡변을 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."

# Gradio μΈν„°νŽ˜μ΄μŠ€ μ •μ˜
iface = gr.Interface(
    fn=answer_question,
    inputs=[gr.File(type="file"), gr.Textbox(label="질문")],
    outputs=gr.Textbox()
)

# Launch the interface
iface.launch()