test111 / app.py
seawolf2357's picture
Update app.py
b30eb63 verified
raw
history blame
1.1 kB
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()