Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from huggingface_hub import InferenceClient
|
|
| 6 |
from dataclasses import dataclass
|
| 7 |
import pytesseract
|
| 8 |
from PIL import Image
|
|
|
|
| 9 |
|
| 10 |
@dataclass
|
| 11 |
class ChatMessage:
|
|
@@ -40,8 +41,7 @@ class XylariaChat:
|
|
| 40 |
|
| 41 |
# System prompt with more detailed instructions
|
| 42 |
self.system_prompt = """You are a helpful and harmless assistant. You are Xylaria developed by Sk Md Saad Amin. You should think step-by-step. You should respond to image questions"""
|
| 43 |
-
|
| 44 |
-
def store_information(self, key, value):
|
| 45 |
"""Store important information in persistent memory"""
|
| 46 |
self.persistent_memory[key] = value
|
| 47 |
return f"Stored: {key} = {value}"
|
|
@@ -112,7 +112,7 @@ class XylariaChat:
|
|
| 112 |
|
| 113 |
def perform_math_ocr(self, image_path):
|
| 114 |
"""
|
| 115 |
-
Perform OCR on an image and return the extracted text.
|
| 116 |
Args:
|
| 117 |
image_path (str): Path to the image file.
|
| 118 |
Returns:
|
|
@@ -122,11 +122,14 @@ class XylariaChat:
|
|
| 122 |
# Open the image using Pillow library
|
| 123 |
img = Image.open(image_path)
|
| 124 |
|
| 125 |
-
# Use
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
# Remove leading/trailing whitespace and return
|
| 129 |
-
return
|
| 130 |
|
| 131 |
except Exception as e:
|
| 132 |
return f"Error during Math OCR: {e}"
|
|
@@ -222,7 +225,7 @@ class XylariaChat:
|
|
| 222 |
|
| 223 |
def create_interface(self):
|
| 224 |
def streaming_response(message, chat_history, image_filepath, math_ocr_image_path):
|
| 225 |
-
|
| 226 |
ocr_text = ""
|
| 227 |
if math_ocr_image_path:
|
| 228 |
ocr_text = self.perform_math_ocr(math_ocr_image_path)
|
|
@@ -232,7 +235,7 @@ class XylariaChat:
|
|
| 232 |
yield "", updated_history, None, None
|
| 233 |
return
|
| 234 |
else:
|
| 235 |
-
message = f"Math OCR Result: {ocr_text}\n\nUser's message: {message}
|
| 236 |
|
| 237 |
# Check if an image was actually uploaded
|
| 238 |
if image_filepath:
|
|
|
|
| 6 |
from dataclasses import dataclass
|
| 7 |
import pytesseract
|
| 8 |
from PIL import Image
|
| 9 |
+
import easyocr
|
| 10 |
|
| 11 |
@dataclass
|
| 12 |
class ChatMessage:
|
|
|
|
| 41 |
|
| 42 |
# System prompt with more detailed instructions
|
| 43 |
self.system_prompt = """You are a helpful and harmless assistant. You are Xylaria developed by Sk Md Saad Amin. You should think step-by-step. You should respond to image questions"""
|
| 44 |
+
self.reader = easyocr.Reader(['en'])
|
|
|
|
| 45 |
"""Store important information in persistent memory"""
|
| 46 |
self.persistent_memory[key] = value
|
| 47 |
return f"Stored: {key} = {value}"
|
|
|
|
| 112 |
|
| 113 |
def perform_math_ocr(self, image_path):
|
| 114 |
"""
|
| 115 |
+
Perform OCR on an image using EasyOCR and return the extracted text.
|
| 116 |
Args:
|
| 117 |
image_path (str): Path to the image file.
|
| 118 |
Returns:
|
|
|
|
| 122 |
# Open the image using Pillow library
|
| 123 |
img = Image.open(image_path)
|
| 124 |
|
| 125 |
+
# Use EasyOCR to do OCR on the image
|
| 126 |
+
results = self.reader.readtext(image_path)
|
| 127 |
+
|
| 128 |
+
# Extract text from results (combining text from multiple detections)
|
| 129 |
+
extracted_text = ' '.join([result[1] for result in results])
|
| 130 |
|
| 131 |
# Remove leading/trailing whitespace and return
|
| 132 |
+
return extracted_text.strip()
|
| 133 |
|
| 134 |
except Exception as e:
|
| 135 |
return f"Error during Math OCR: {e}"
|
|
|
|
| 225 |
|
| 226 |
def create_interface(self):
|
| 227 |
def streaming_response(message, chat_history, image_filepath, math_ocr_image_path):
|
| 228 |
+
|
| 229 |
ocr_text = ""
|
| 230 |
if math_ocr_image_path:
|
| 231 |
ocr_text = self.perform_math_ocr(math_ocr_image_path)
|
|
|
|
| 235 |
yield "", updated_history, None, None
|
| 236 |
return
|
| 237 |
else:
|
| 238 |
+
message = f"Math OCR Result: {ocr_text}\n\nUser's message: {message}
|
| 239 |
|
| 240 |
# Check if an image was actually uploaded
|
| 241 |
if image_filepath:
|