Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import fitz # PyMuPDF | |
| from transformers import pipeline | |
| # Load the Hugging Face question-answering pipeline | |
| qa_pipeline = pipeline("question-answering") | |
| def answer_question(pdf_file, question): | |
| # Read the PDF file | |
| doc = fitz.open(stream=pdf_file.read(), filetype="pdf") | |
| text = "" | |
| for page in doc: | |
| text += page.get_text() | |
| # Use the QA model to answer the question | |
| answer = qa_pipeline(question=question, context=text) | |
| return answer['answer'] | |
| # Define the Gradio interface | |
| interface = gr.Interface( | |
| fn=answer_question, | |
| inputs=[ | |
| gr.File(type="pdf"), | |
| gr.Textbox(lines=2, placeholder="Type your question here...") | |
| ], | |
| outputs="text", | |
| title="PDF Question Answering", | |
| description="Upload a PDF document and ask a question based on its content." | |
| ) | |
| interface.launch() | |