import gradio as gr from transformers import pipeline # Load the models ocr_model = pipeline("image-to-text", model="pragnakalp/OCR-image-to-text") math_model = pipeline("text-generation", model="Qwen/Qwen2.5-Math") # Define functions for OCR and math reasoning def process_image(image): # Extract text from image using OCR ocr_text = ocr_model(image)[0]['generated_text'] return ocr_text def process_math(text): # Use Qwen2.5-Math model to interpret the text math_result = math_model(text, max_length=50)[0]['generated_text'] return math_result # Create a Gradio interface def combined_process(image=None, text=None): history = [] # If an image is uploaded, process it with OCR if image: extracted_text = process_image(image) history.append(("OCR Text", extracted_text)) else: extracted_text = text # Pass extracted or entered text to the math model if extracted_text: result = process_math(extracted_text) history.append(("Math Result", result)) return history # Gradio Interface with text and image inputs iface = gr.Interface( fn=combined_process, inputs=[ gr.inputs.Image(label="Upload an Image for OCR"), gr.inputs.Textbox(label="Or Enter Text for Math Processing") ], outputs=gr.outputs.JSON(label="Interaction History"), title="OCR and Math Assistant", description="Upload an image or enter text for OCR or math processing." ) iface.launch()