mohnish4246/ProblemSolver
0
1import gradio as gr2from openai import OpenAI3import pytesseract4from PIL import Image5import os6import tempfile7 8 9# --------------------------10# Setup API Key11# --------------------------12client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))13# --------------------------14# Global State15# --------------------------16math_messages = []17 18# --------------------------19# Process Uploaded/Drawn Image20# --------------------------21def process_image_with_ocr(image, shouldConvert=False):22 # Convert if needed (for Sketchpad)23 if shouldConvert:24 new_img = Image.new('RGB', size=(image.width, image.height), color=(255, 255, 255))25 new_img.paste(image, (0, 0), mask=image)26 image = new_img27 28 # Save image temporarily29 temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")30 image.save(temp_file.name)31 32 # Use Tesseract to extract text33 extracted_text = pytesseract.image_to_string(Image.open(temp_file.name))34 os.remove(temp_file.name)35 36 return extracted_text.strip()37 38# --------------------------39# Get Math Answer40# --------------------------41def get_math_response(image_description, user_question):42 if image_description:43 prompt = f"The following is a math problem extracted from an image:\n\n{image_description}\n\nUser question: {user_question}"44 else:45 prompt = f"User question: {user_question}"46 47 response = client.chat.completions.create(48 model="gpt-4",49 messages=[50 {"role": "system", "content": "You are a helpful math assistant. Solve problems carefully with step-by-step reasoning."},51 {"role": "user", "content": prompt}52 ],53 temperature=0.354 )55 56 return response.choices[0].message.content57# --------------------------58# Main Chat Function59# --------------------------60def math_chat_bot(image, sketchpad, question, state):61 current_tab_index = state["tab_index"]62 image_description = None63 64 if current_tab_index == 0:65 if image is not None:66 image_description = process_image_with_ocr(image)67 elif current_tab_index == 1:68 if sketchpad and sketchpad["composite"]:69 image_description = process_image_with_ocr(sketchpad["composite"], True)70 71 answer = get_math_response(image_description, question)72 yield answer73 74# --------------------------75# CSS for Inline LaTeX76# --------------------------77css = """78#qwen-md .katex-display { display: inline; }79#qwen-md .katex-display>.katex { display: inline; }80#qwen-md .katex-display>.katex>.katex-html { display: inline; }81"""82 83# --------------------------84# Gradio Interface85# --------------------------86with gr.Blocks(css=css) as demo:87 gr.HTML("""88 <p align="center"><img src="https://modelscope.oss-cn-beijing.aliyuncs.com/resource/qwen.png" style="height: 60px"/></p>89 <center><font size=8>📖 Qwen2.5-Math Demo</font></center>90 <center><font size=3>This WebUI uses Qwen2-VL for OCR and Qwen2.5-Math for reasoning. Input images or math problems below.</font></center>91 """)92 93 state = gr.State({"tab_index": 0})94 95 with gr.Row():96 with gr.Column():97 with gr.Tabs() as input_tabs:98 with gr.Tab("Upload"):99 input_image = gr.Image(type="pil", label="Upload")100 with gr.Tab("Sketch"):101 input_sketchpad = gr.Sketchpad(type="pil", label="Sketch", layers=False)102 103 def on_tab_select(e: gr.SelectData, s): s["tab_index"] = e.index104 input_tabs.select(fn=lambda e: {"tab_index": e.index}, inputs=None, outputs=state)105 106 input_text = gr.Textbox(label="Enter your question")107 with gr.Row():108 clear_btn = gr.ClearButton([input_image, input_sketchpad, input_text])109 submit_btn = gr.Button("Submit", variant="primary")110 111 with gr.Column():112 output_md = gr.Markdown(label="Answer", elem_id="qwen-md",113 latex_delimiters=[114 {"left": "\\(", "right": "\\)", "display": True},115 {"left": "\\[", "right": "\\]", "display": True},116 {"left": "\\begin{equation}", "right": "\\end{equation}", "display": True},117 {"left": "\\begin{align}", "right": "\\end{align}", "display": True},118 {"left": "\\begin{gather}", "right": "\\end{gather}", "display": True},119 ])120 121 submit_btn.click(122 fn=math_chat_bot,123 inputs=[input_image, input_sketchpad, input_text, state],124 outputs=output_md125 )126 127# --------------------------128# Launch129# --------------------------130if __name__ == "__main__":131 demo.launch()