fix(effi-ocr): improve file search and normalize language codes
- Better file finding (startswith + contains) - Normalize language codes (French->fra, English->eng, etc.) - List all files in directory when searching
This commit is contained in:
@@ -24,6 +24,16 @@ class Tools:
|
||||
self.uploads_dir = "/app/backend/data/uploads"
|
||||
self.host_uploads_dir = "/home/effi/effi/data/uploads"
|
||||
|
||||
def _normalize_language(self, lang):
|
||||
lang_map = {
|
||||
"french": "fra", "francais": "fra", "fr": "fra",
|
||||
"english": "eng", "eng": "eng",
|
||||
"german": "deu", "de": "deu",
|
||||
"spanish": "spa", "es": "spa",
|
||||
"italian": "ita", "it": "ita"
|
||||
}
|
||||
return lang_map.get(lang.lower(), lang)
|
||||
|
||||
def extract_text_from_image(self, image_path, language="fra"):
|
||||
try:
|
||||
file_path = self._find_file(image_path)
|
||||
@@ -32,8 +42,8 @@ class Tools:
|
||||
return "[ERROR] File not found: " + image_path + ". Use list_recent_files() to see available files."
|
||||
|
||||
with open(file_path, 'rb') as f:
|
||||
files = {'file': f}
|
||||
data = {'lang': language, 'preprocess': 'true'}
|
||||
files = {'file': (os.path.basename(file_path), f)}
|
||||
data = {'lang': self._normalize_language(language), 'preprocess': 'true'}
|
||||
|
||||
response = requests.post(
|
||||
self.ocr_url + "/ocr",
|
||||
@@ -66,7 +76,10 @@ class Tools:
|
||||
|
||||
def list_recent_files(self, count=10):
|
||||
try:
|
||||
files = glob.glob(os.path.join(self.uploads_dir, "*"))
|
||||
files = []
|
||||
|
||||
if os.path.exists(self.uploads_dir):
|
||||
files = glob.glob(os.path.join(self.uploads_dir, "*"))
|
||||
|
||||
if not files and os.path.exists(self.host_uploads_dir):
|
||||
files = glob.glob(os.path.join(self.host_uploads_dir, "*"))
|
||||
@@ -104,22 +117,14 @@ class Tools:
|
||||
if os.path.exists(path):
|
||||
return path
|
||||
|
||||
for directory in [self.uploads_dir, self.host_uploads_dir]:
|
||||
if not os.path.exists(directory):
|
||||
continue
|
||||
|
||||
pattern = os.path.join(directory, filename + "*")
|
||||
matches = glob.glob(pattern)
|
||||
if matches:
|
||||
return matches[0]
|
||||
|
||||
pattern = os.path.join(directory, "*" + filename + "*")
|
||||
matches = glob.glob(pattern)
|
||||
if matches:
|
||||
for m in matches:
|
||||
if filename in os.path.basename(m):
|
||||
return m
|
||||
return matches[0]
|
||||
search_dirs = [d for d in [self.uploads_dir, self.host_uploads_dir] if os.path.exists(d)]
|
||||
|
||||
for directory in search_dirs:
|
||||
all_files = glob.glob(os.path.join(directory, "*"))
|
||||
for f in all_files:
|
||||
basename = os.path.basename(f)
|
||||
if basename.startswith(filename) or filename in basename:
|
||||
return f
|
||||
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user