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