| import gradio as gr |
| from transformers import pipeline |
|
|
| |
| ocr = pipeline("image-to-text", model="microsoft/trocr-base-printed") |
|
|
| |
| qa = pipeline("question-answering", model="deepset/roberta-base-squad2") |
|
|
| def process(image, question): |
| try: |
| |
| extracted_text = ocr(image)[0]["generated_text"] |
|
|
| |
| if not question: |
| return extracted_text, "Please enter a question." |
|
|
| |
| answer = qa(question=question, context=extracted_text) |
|
|
| return extracted_text, answer.get("answer", "No answer found.") |
|
|
| except Exception as e: |
| return "Error during processing.", str(e) |
|
|
| |
| demo = gr.Interface( |
| fn=process, |
| inputs=[ |
| gr.Image(type="pil", label="Upload an image"), |
| gr.Textbox(label="Ask a question about the document") |
| ], |
| outputs=[ |
| gr.Textbox(label="Extracted Text"), |
| gr.Textbox(label="Answer") |
| ], |
| title="OCR + Question Answering", |
| description="Upload a document image, extract text, and ask questions about it." |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |
|
|
|
|