|
import gradio as gr |
|
from transformers import pipeline |
|
import pdf2image |
|
import pytesseract |
|
|
|
|
|
qa_model = pipeline("question-answering") |
|
|
|
def process_pdf(pdf_file): |
|
|
|
text = extract_text_from_pdf(pdf_file) |
|
return text |
|
|
|
def answer_question(question, context): |
|
|
|
answer = qa_model(question=question, context=context) |
|
return answer['answer'] |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# My Study Assistant") |
|
|
|
|
|
file_input = gr.File(label="Upload PDF") |
|
|
|
|
|
text_output = gr.Textbox(label="Extracted Text") |
|
|
|
|
|
question = gr.Textbox(label="Ask a question") |
|
answer = gr.Textbox(label="Answer") |
|
|
|
|
|
upload_btn = gr.Button("Process PDF") |
|
ask_btn = gr.Button("Ask") |
|
|
|
|
|
upload_btn.click(process_pdf, inputs=file_input, outputs=text_output) |
|
ask_btn.click(answer_question, inputs=[question, text_output], outputs=answer) |
|
|
|
demo.launch() |
|
|