File size: 1,057 Bytes
978720b |
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 34 35 36 37 38 39 40 41 42 |
import gradio as gr
from transformers import pipeline
import pdf2image
import pytesseract
# Initialize model
qa_model = pipeline("question-answering")
def process_pdf(pdf_file):
# Convert PDF to text
text = extract_text_from_pdf(pdf_file)
return text
def answer_question(question, context):
# Get answer from model
answer = qa_model(question=question, context=context)
return answer['answer']
# Create interface
with gr.Blocks() as demo:
gr.Markdown("# My Study Assistant")
# File upload
file_input = gr.File(label="Upload PDF")
# Text display
text_output = gr.Textbox(label="Extracted Text")
# Chat interface
question = gr.Textbox(label="Ask a question")
answer = gr.Textbox(label="Answer")
# Buttons
upload_btn = gr.Button("Process PDF")
ask_btn = gr.Button("Ask")
# Set up actions
upload_btn.click(process_pdf, inputs=file_input, outputs=text_output)
ask_btn.click(answer_question, inputs=[question, text_output], outputs=answer)
demo.launch()
|