Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ from huggingface_hub import InferenceClient
|
|
| 6 |
from dataclasses import dataclass
|
| 7 |
import pytesseract
|
| 8 |
from PIL import Image
|
| 9 |
-
import easyocr
|
| 10 |
|
| 11 |
@dataclass
|
| 12 |
class ChatMessage:
|
|
@@ -41,7 +40,8 @@ class XylariaChat:
|
|
| 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 |
-
|
|
|
|
| 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
|
| 116 |
Args:
|
| 117 |
image_path (str): Path to the image file.
|
| 118 |
Returns:
|
|
@@ -122,14 +122,11 @@ class XylariaChat:
|
|
| 122 |
# Open the image using Pillow library
|
| 123 |
img = Image.open(image_path)
|
| 124 |
|
| 125 |
-
# Use
|
| 126 |
-
|
| 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
|
| 133 |
|
| 134 |
except Exception as e:
|
| 135 |
return f"Error during Math OCR: {e}"
|
|
@@ -225,7 +222,7 @@ class XylariaChat:
|
|
| 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)
|
|
|
|
| 6 |
from dataclasses import dataclass
|
| 7 |
import pytesseract
|
| 8 |
from PIL import Image
|
|
|
|
| 9 |
|
| 10 |
@dataclass
|
| 11 |
class ChatMessage:
|
|
|
|
| 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 |
|
| 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 |
# Open the image using Pillow library
|
| 123 |
img = Image.open(image_path)
|
| 124 |
|
| 125 |
+
# Use Tesseract to do OCR on the image
|
| 126 |
+
text = pytesseract.image_to_string(img)
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
# Remove leading/trailing whitespace and return
|
| 129 |
+
return text.strip()
|
| 130 |
|
| 131 |
except Exception as e:
|
| 132 |
return f"Error during Math OCR: {e}"
|
|
|
|
| 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)
|