Spaces:
Build error
Build error
Upload 2 files
Browse files- app.py +140 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import tempfile
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
import secrets
|
| 8 |
+
|
| 9 |
+
# Initialising huggingface pipelines
|
| 10 |
+
image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 11 |
+
math_reasoning = pipeline("text2text-generation", model="google/flan-t5-large")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Helper function to process images
|
| 15 |
+
def process_image(image, should_convert=False):
|
| 16 |
+
'''
|
| 17 |
+
Saves an uploaded image and utilises image-to-text pipeline for math-related descriptions
|
| 18 |
+
:param image:
|
| 19 |
+
:param should_convert:
|
| 20 |
+
:return: pipeline's output
|
| 21 |
+
'''
|
| 22 |
+
# creating a temporary directory for saving images
|
| 23 |
+
uploaded_file_dir = os.environ.get("GRADIO_TEMP_DIR") or str(Path(tempfile.gettempdir()) / "gradio")
|
| 24 |
+
os.makedirs(uploaded_file_dir, exist_ok=True)
|
| 25 |
+
# Save the uploaded image as a temporary file
|
| 26 |
+
name = f"tmp{secrets.token_hex(8)}.jpg"
|
| 27 |
+
filename = os.path.join(uploaded_file_dir, name)
|
| 28 |
+
|
| 29 |
+
if should_convert:
|
| 30 |
+
# Converts image into RGB format
|
| 31 |
+
new_img = Image.new("RGB", size=(image.height, image.width), color=(255, 255, 255))
|
| 32 |
+
new_img.paste(image, (0, 0), mask=image)
|
| 33 |
+
image = new_img
|
| 34 |
+
image.save(filename)
|
| 35 |
+
|
| 36 |
+
# Generate text description of the image
|
| 37 |
+
description = image_to_text(Image.open(filename))[0]['generated_text']
|
| 38 |
+
|
| 39 |
+
# Clean up file
|
| 40 |
+
os.remove(filename)
|
| 41 |
+
return description
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def get_math_response(image_description, user_question):
|
| 45 |
+
'''
|
| 46 |
+
Generates a math related response based upon image description and user's question
|
| 47 |
+
:param image_description:
|
| 48 |
+
:param user_question:
|
| 49 |
+
'''
|
| 50 |
+
prompt = ""
|
| 51 |
+
if image_description:
|
| 52 |
+
prompt += f"Image Description :{image_description}\n"
|
| 53 |
+
if user_question:
|
| 54 |
+
prompt += f"User question :{user_question}\n"
|
| 55 |
+
else:
|
| 56 |
+
return "Please provide a valid description."
|
| 57 |
+
# Generate the response using the math_reasoning pipeline
|
| 58 |
+
response = math_reasoning(prompt, max_length=512)[0]['generated_text']
|
| 59 |
+
return response
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# Combined chatbot logic
|
| 63 |
+
def math_chatbot(image, sketchpad, question, state):
|
| 64 |
+
current_tab_index = state['tab_index']
|
| 65 |
+
image_description = None
|
| 66 |
+
|
| 67 |
+
# Handle image upload
|
| 68 |
+
if current_tab_index == 0:
|
| 69 |
+
if image is not None:
|
| 70 |
+
image_description = process_image(image, )
|
| 71 |
+
# Handle sketchpad input
|
| 72 |
+
elif current_tab_index == 1:
|
| 73 |
+
if sketchpad and sketchpad['composite']:
|
| 74 |
+
image_description = process_image(sketchpad['composite'], should_convert=True)
|
| 75 |
+
|
| 76 |
+
return get_math_response(image_description, question)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
css = """
|
| 80 |
+
#qwen-md .katex-display { display: inline; }
|
| 81 |
+
#qwen-md .katex-display>.katex { display: inline; }
|
| 82 |
+
#qwen-md .katex-display>.katex>.katex-html { display: inline; }
|
| 83 |
+
"""
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# Tab selection callback
|
| 87 |
+
def tabs_select(e: gr.SelectData, _state):
|
| 88 |
+
_state["tab_index"] = e.index
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
# Gradio interface
|
| 92 |
+
with gr.Blocks(css=css) as demo:
|
| 93 |
+
gr.HTML("""\
|
| 94 |
+
<p align="center"><img src="https://huggingface.co/front/assets/huggingface_logo.svg" style="height: 60px"/><p>"""
|
| 95 |
+
"""<center><font size=8>📖 Math Reasoning Chatbot</center>"""
|
| 96 |
+
"""\
|
| 97 |
+
<center><font size=3>This demo uses Hugging Face models for OCR and mathematical reasoning. You can input images or text-based questions.</center>"""
|
| 98 |
+
)
|
| 99 |
+
state = gr.State({"tab_index": 0})
|
| 100 |
+
|
| 101 |
+
with gr.Row():
|
| 102 |
+
with gr.Column():
|
| 103 |
+
with gr.Tabs() as input_tabs:
|
| 104 |
+
with gr.Tab("Upload"):
|
| 105 |
+
input_image = gr.Image(type="pil", label="Upload")
|
| 106 |
+
with gr.Tab("Sketch"):
|
| 107 |
+
input_sketchpad = gr.Sketchpad(label="Sketch", layers=False)
|
| 108 |
+
input_tabs.select(fn=tabs_select, inputs=[state])
|
| 109 |
+
input_text = gr.Textbox(label="Input your question")
|
| 110 |
+
with gr.Row():
|
| 111 |
+
with gr.Column():
|
| 112 |
+
clear_btn = gr.ClearButton([input_image, input_sketchpad, input_text])
|
| 113 |
+
with gr.Column():
|
| 114 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
| 115 |
+
|
| 116 |
+
with gr.Column():
|
| 117 |
+
output_md = gr.Markdown(label="Answer",
|
| 118 |
+
latex_delimiters=[{
|
| 119 |
+
"left": "\\(",
|
| 120 |
+
"right": "\\)",
|
| 121 |
+
"display": True
|
| 122 |
+
}, {
|
| 123 |
+
"left": "\\begin\{equation\}",
|
| 124 |
+
"right": "\\end\{equation\}",
|
| 125 |
+
"display": True
|
| 126 |
+
}, {
|
| 127 |
+
"left": "\\[",
|
| 128 |
+
"right": "\\]",
|
| 129 |
+
"display": True
|
| 130 |
+
}],
|
| 131 |
+
elem_id="qwen-md")
|
| 132 |
+
|
| 133 |
+
submit_btn.click(
|
| 134 |
+
fn=math_chatbot,
|
| 135 |
+
inputs=[input_image, input_sketchpad, input_text, state],
|
| 136 |
+
outputs=output_md
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
# Launch Gradio app
|
| 140 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
pillow
|
| 3 |
+
gradio
|