CoolFace
Apppublic

Maxi0811/pid-interactive-demo

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
app.py52 linesDownload Raw Back to root
1import gradio as gr2import pytesseract3from PIL import Image4from PyPDF2 import PdfReader5import os6import tempfile7from transformers import pipeline8 9# Load a small language model for Q&A10qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")11 12def extract_text_from_image(image):13    if image is None:14        return ""15    text = pytesseract.image_to_string(image)16    return text17 18def extract_text_from_pdf(pdf_file):19    if pdf_file is None:20        return ""21    reader = PdfReader(pdf_file)22    text = ""23    for page in reader.pages:24        text += page.extract_text() or ""25    return text26 27def combine_text(image, doc):28    image_text = extract_text_from_image(image)29    doc_text = extract_text_from_pdf(doc) if doc.name.endswith('.pdf') else doc.read().decode('utf-8')30    return f"{image_text}\n\n{doc_text}"31 32def answer_question(image, doc, question):33    context = combine_text(image, doc)34    if not context.strip():35        return "No valid text found in the image or manual."36    result = qa_pipeline(question=question, context=context)37    return result['answer']38 39iface = gr.Interface(40    fn=answer_question,41    inputs=[42        gr.Image(type="pil", label="Upload P&ID Image"),43        gr.File(label="Upload Working Manual (.txt or .pdf)"),44        gr.Textbox(label="Ask a Question")45    ],46    outputs="text",47    title="P&ID + Manual Interactive QA",48    description="Upload a P&ID image and an operational manual, and ask questions about them."49)50 51iface.launch()52