Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,8 @@ import requests
|
|
| 4 |
import gradio as gr
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
from dataclasses import dataclass
|
|
|
|
|
|
|
| 7 |
|
| 8 |
@dataclass
|
| 9 |
class ChatMessage:
|
|
@@ -71,10 +73,8 @@ class XylariaChat:
|
|
| 71 |
def caption_image(self, image):
|
| 72 |
"""
|
| 73 |
Caption an uploaded image using Hugging Face API
|
| 74 |
-
|
| 75 |
Args:
|
| 76 |
image (str): Base64 encoded image or file path
|
| 77 |
-
|
| 78 |
Returns:
|
| 79 |
str: Image caption or error message
|
| 80 |
"""
|
|
@@ -110,14 +110,35 @@ class XylariaChat:
|
|
| 110 |
except Exception as e:
|
| 111 |
return f"Error processing image: {str(e)}"
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
def get_response(self, user_input, image=None):
|
| 114 |
"""
|
| 115 |
Generate a response using chat completions with improved error handling
|
| 116 |
-
|
| 117 |
Args:
|
| 118 |
user_input (str): User's message
|
| 119 |
image (optional): Uploaded image
|
| 120 |
-
|
| 121 |
Returns:
|
| 122 |
Stream of chat completions or error message
|
| 123 |
"""
|
|
@@ -202,18 +223,31 @@ class XylariaChat:
|
|
| 202 |
|
| 203 |
|
| 204 |
def create_interface(self):
|
| 205 |
-
def streaming_response(message, chat_history, image_filepath):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
# Check if an image was actually uploaded
|
| 207 |
if image_filepath:
|
| 208 |
response_stream = self.get_response(message, image_filepath)
|
| 209 |
else:
|
| 210 |
response_stream = self.get_response(message)
|
|
|
|
| 211 |
|
| 212 |
# Handle errors in get_response
|
| 213 |
if isinstance(response_stream, str):
|
| 214 |
# Return immediately with the error message
|
| 215 |
updated_history = chat_history + [[message, response_stream]]
|
| 216 |
-
yield "", updated_history, None
|
| 217 |
return
|
| 218 |
|
| 219 |
# Prepare for streaming response
|
|
@@ -229,12 +263,12 @@ class XylariaChat:
|
|
| 229 |
|
| 230 |
# Update the last message in chat history with partial response
|
| 231 |
updated_history[-1][1] = full_response
|
| 232 |
-
yield "", updated_history, None
|
| 233 |
except Exception as e:
|
| 234 |
print(f"Streaming error: {e}")
|
| 235 |
# Display error in the chat interface
|
| 236 |
updated_history[-1][1] = f"Error during response: {e}"
|
| 237 |
-
yield "", updated_history, None
|
| 238 |
return
|
| 239 |
|
| 240 |
# Update conversation history
|
|
@@ -317,6 +351,17 @@ class XylariaChat:
|
|
| 317 |
)
|
| 318 |
with gr.Row():
|
| 319 |
clear_image_btn = gr.Button("Clear Image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
# Input row with improved layout
|
| 322 |
with gr.Row():
|
|
@@ -341,16 +386,24 @@ class XylariaChat:
|
|
| 341 |
queue=False
|
| 342 |
)
|
| 343 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
# Submit functionality with streaming and image support
|
| 345 |
btn.click(
|
| 346 |
fn=streaming_response,
|
| 347 |
-
inputs=[txt, chatbot, img],
|
| 348 |
-
outputs=[txt, chatbot, img]
|
| 349 |
)
|
| 350 |
txt.submit(
|
| 351 |
fn=streaming_response,
|
| 352 |
-
inputs=[txt, chatbot, img],
|
| 353 |
-
outputs=[txt, chatbot, img]
|
| 354 |
)
|
| 355 |
|
| 356 |
# Clear conversation history
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
from dataclasses import dataclass
|
| 7 |
+
import pytesseract
|
| 8 |
+
from PIL import Image
|
| 9 |
|
| 10 |
@dataclass
|
| 11 |
class ChatMessage:
|
|
|
|
| 73 |
def caption_image(self, image):
|
| 74 |
"""
|
| 75 |
Caption an uploaded image using Hugging Face API
|
|
|
|
| 76 |
Args:
|
| 77 |
image (str): Base64 encoded image or file path
|
|
|
|
| 78 |
Returns:
|
| 79 |
str: Image caption or error message
|
| 80 |
"""
|
|
|
|
| 110 |
except Exception as e:
|
| 111 |
return f"Error processing image: {str(e)}"
|
| 112 |
|
| 113 |
+
def perform_math_ocr(self, image_path):
|
| 114 |
+
"""
|
| 115 |
+
Perform OCR on an image and return the extracted text.
|
| 116 |
+
|
| 117 |
+
Args:
|
| 118 |
+
image_path (str): Path to the image file.
|
| 119 |
+
|
| 120 |
+
Returns:
|
| 121 |
+
str: Extracted text from the image, or an error message.
|
| 122 |
+
"""
|
| 123 |
+
try:
|
| 124 |
+
# Open the image using Pillow library
|
| 125 |
+
img = Image.open(image_path)
|
| 126 |
+
|
| 127 |
+
# Use Tesseract to do OCR on the image
|
| 128 |
+
text = pytesseract.image_to_string(img)
|
| 129 |
+
|
| 130 |
+
# Remove leading/trailing whitespace and return
|
| 131 |
+
return text.strip()
|
| 132 |
+
|
| 133 |
+
except Exception as e:
|
| 134 |
+
return f"Error during Math OCR: {e}"
|
| 135 |
+
|
| 136 |
def get_response(self, user_input, image=None):
|
| 137 |
"""
|
| 138 |
Generate a response using chat completions with improved error handling
|
|
|
|
| 139 |
Args:
|
| 140 |
user_input (str): User's message
|
| 141 |
image (optional): Uploaded image
|
|
|
|
| 142 |
Returns:
|
| 143 |
Stream of chat completions or error message
|
| 144 |
"""
|
|
|
|
| 223 |
|
| 224 |
|
| 225 |
def create_interface(self):
|
| 226 |
+
def streaming_response(message, chat_history, image_filepath, math_ocr_image_path):
|
| 227 |
+
|
| 228 |
+
ocr_text = ""
|
| 229 |
+
if math_ocr_image_path:
|
| 230 |
+
ocr_text = self.perform_math_ocr(math_ocr_image_path)
|
| 231 |
+
if ocr_text.startswith("Error"):
|
| 232 |
+
# Handle OCR error
|
| 233 |
+
updated_history = chat_history + [[message, ocr_text]]
|
| 234 |
+
yield "", updated_history, None, None
|
| 235 |
+
return
|
| 236 |
+
else:
|
| 237 |
+
message = f"Math OCR Result: {ocr_text}\n\nUser's message: {message}"
|
| 238 |
+
|
| 239 |
# Check if an image was actually uploaded
|
| 240 |
if image_filepath:
|
| 241 |
response_stream = self.get_response(message, image_filepath)
|
| 242 |
else:
|
| 243 |
response_stream = self.get_response(message)
|
| 244 |
+
|
| 245 |
|
| 246 |
# Handle errors in get_response
|
| 247 |
if isinstance(response_stream, str):
|
| 248 |
# Return immediately with the error message
|
| 249 |
updated_history = chat_history + [[message, response_stream]]
|
| 250 |
+
yield "", updated_history, None, None
|
| 251 |
return
|
| 252 |
|
| 253 |
# Prepare for streaming response
|
|
|
|
| 263 |
|
| 264 |
# Update the last message in chat history with partial response
|
| 265 |
updated_history[-1][1] = full_response
|
| 266 |
+
yield "", updated_history, None, None
|
| 267 |
except Exception as e:
|
| 268 |
print(f"Streaming error: {e}")
|
| 269 |
# Display error in the chat interface
|
| 270 |
updated_history[-1][1] = f"Error during response: {e}"
|
| 271 |
+
yield "", updated_history, None, None
|
| 272 |
return
|
| 273 |
|
| 274 |
# Update conversation history
|
|
|
|
| 351 |
)
|
| 352 |
with gr.Row():
|
| 353 |
clear_image_btn = gr.Button("Clear Image")
|
| 354 |
+
|
| 355 |
+
with gr.Accordion("Math Input", open=False):
|
| 356 |
+
with gr.Column():
|
| 357 |
+
math_ocr_img = gr.Image(
|
| 358 |
+
sources=["upload", "webcam"],
|
| 359 |
+
type="filepath",
|
| 360 |
+
label="Upload Image for math",
|
| 361 |
+
elem_classes="image-preview"
|
| 362 |
+
)
|
| 363 |
+
with gr.Row():
|
| 364 |
+
clear_math_ocr_btn = gr.Button("Clear Math Image")
|
| 365 |
|
| 366 |
# Input row with improved layout
|
| 367 |
with gr.Row():
|
|
|
|
| 386 |
queue=False
|
| 387 |
)
|
| 388 |
|
| 389 |
+
# Clear Math OCR image functionality
|
| 390 |
+
clear_math_ocr_btn.click(
|
| 391 |
+
fn=lambda: None,
|
| 392 |
+
inputs=None,
|
| 393 |
+
outputs=[math_ocr_img],
|
| 394 |
+
queue=False
|
| 395 |
+
)
|
| 396 |
+
|
| 397 |
# Submit functionality with streaming and image support
|
| 398 |
btn.click(
|
| 399 |
fn=streaming_response,
|
| 400 |
+
inputs=[txt, chatbot, img, math_ocr_img],
|
| 401 |
+
outputs=[txt, chatbot, img, math_ocr_img]
|
| 402 |
)
|
| 403 |
txt.submit(
|
| 404 |
fn=streaming_response,
|
| 405 |
+
inputs=[txt, chatbot, img, math_ocr_img],
|
| 406 |
+
outputs=[txt, chatbot, img, math_ocr_img]
|
| 407 |
)
|
| 408 |
|
| 409 |
# Clear conversation history
|