Spaces:
Sleeping
Sleeping
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() |