fix(effi-ocr): update tool for port 8301 and correct file paths
- Change OCR_URL to port 8301 - Add uploads directory paths for finding files - Add list_recent_files function
This commit is contained in:
+103
-123
@@ -1,49 +1,45 @@
|
||||
# Tool OCR pour OpenWebUI
|
||||
# Tool OCR effi-ocr pour OpenWebUI
|
||||
|
||||
## Configuration
|
||||
|
||||
Dans OpenWebUI (http://192.168.1.27:3000) :
|
||||
|
||||
1. **Aller dans** : Admin → Tools → Create Tool
|
||||
2. **Nom** : OCR Extracteur de Texte
|
||||
3. **ID** : ocr_extractor
|
||||
4. **Description** : Extrait le texte des images pour les livres et documents
|
||||
2. **Nom** : effi-ocr
|
||||
3. **Description** : Extrait le texte des images avec Tesseract OCR (précision française)
|
||||
4. **Coller le code ci-dessous**
|
||||
5. **Save and Activate**
|
||||
|
||||
## Code du Tool
|
||||
|
||||
```python
|
||||
from typing import Optional
|
||||
import requests
|
||||
import base64
|
||||
import os
|
||||
import glob
|
||||
import requests
|
||||
|
||||
|
||||
class Tools:
|
||||
def __init__(self):
|
||||
"""Initialize OCR Tool"""
|
||||
self.ocr_url = os.getenv("OCR_URL", "http://192.168.1.11:8300")
|
||||
self.ocr_url = os.getenv("OCR_URL", "http://192.168.1.11:8301")
|
||||
self.uploads_dir = "/app/backend/data/uploads"
|
||||
self.host_uploads_dir = "/home/effi/effi/data/uploads"
|
||||
|
||||
def extract_text_from_image(self, image_path: str, language: str = "fra") -> str:
|
||||
"""
|
||||
Extract text from an image file using OCR
|
||||
|
||||
Args:
|
||||
image_path: Path to the image file (jpg, png, etc.)
|
||||
language: Language code (fra, eng, fra+eng, etc.)
|
||||
|
||||
Returns:
|
||||
Extracted text from the image
|
||||
"""
|
||||
def extract_text_from_image(self, image_path, language="fra"):
|
||||
try:
|
||||
# Read image file
|
||||
with open(image_path, 'rb') as f:
|
||||
file_path = self._find_file(image_path)
|
||||
|
||||
if not file_path:
|
||||
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}
|
||||
data = {'lang': language, 'preprocess': 'true'}
|
||||
|
||||
response = requests.post(
|
||||
f"{self.ocr_url}/ocr",
|
||||
self.ocr_url + "/ocr",
|
||||
files=files,
|
||||
data=data,
|
||||
timeout=30
|
||||
timeout=60
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
@@ -52,130 +48,114 @@ class Tools:
|
||||
text = result.get('text', '')
|
||||
char_count = result.get('characters_count', 0)
|
||||
word_count = result.get('words_count', 0)
|
||||
return f"Texte extrait ({char_count} caractères, {word_count} mots):\n\n{text}"
|
||||
if text:
|
||||
return "OCR effi-ocr (" + str(char_count) + " chars, " + str(word_count) + " words):\n\n" + text
|
||||
else:
|
||||
return "[WARNING] No text extracted from " + os.path.basename(file_path)
|
||||
else:
|
||||
return f"Erreur OCR: {result.get('error', 'Unknown error')}"
|
||||
return "[ERROR] OCR failed: " + str(result.get('error', 'Unknown error'))
|
||||
else:
|
||||
return f"Erreur HTTP {response.status_code}: {response.text}"
|
||||
return "[ERROR] HTTP " + str(response.status_code) + ": " + response.text[:200]
|
||||
|
||||
except FileNotFoundError:
|
||||
return f"Erreur: Fichier non trouvé: {image_path}"
|
||||
except requests.Timeout:
|
||||
return "[ERROR] Timeout - file is too large"
|
||||
except requests.ConnectionError:
|
||||
return "[ERROR] Cannot connect to effi-ocr at " + self.ocr_url
|
||||
except Exception as e:
|
||||
return f"Erreur lors de l'OCR: {str(e)}"
|
||||
return "[ERROR] " + str(e)
|
||||
|
||||
def extract_text_from_base64(self, image_base64: str, language: str = "fra") -> str:
|
||||
"""
|
||||
Extract text from a base64 encoded image
|
||||
|
||||
Args:
|
||||
image_base64: Base64 encoded image string
|
||||
language: Language code (fra, eng, fra+eng, etc.)
|
||||
|
||||
Returns:
|
||||
Extracted text from the image
|
||||
"""
|
||||
def list_recent_files(self, count=10):
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{self.ocr_url}/ocr/base64",
|
||||
json={
|
||||
'image_base64': image_base64,
|
||||
'lang': language,
|
||||
'preprocess': True
|
||||
},
|
||||
timeout=30
|
||||
)
|
||||
files = glob.glob(os.path.join(self.uploads_dir, "*"))
|
||||
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
if result.get('success'):
|
||||
text = result.get('text', '')
|
||||
char_count = result.get('characters_count', 0)
|
||||
word_count = result.get('words_count', 0)
|
||||
return f"Texte extrait ({char_count} caractères, {word_count} mots):\n\n{text}"
|
||||
else:
|
||||
return f"Erreur OCR: extraction échouée"
|
||||
else:
|
||||
return f"Erreur HTTP {response.status_code}: {response.text}"
|
||||
if not files and os.path.exists(self.host_uploads_dir):
|
||||
files = glob.glob(os.path.join(self.host_uploads_dir, "*"))
|
||||
|
||||
files = [f for f in files if os.path.isfile(f)]
|
||||
files = sorted(files, key=os.path.getmtime, reverse=True)[:count]
|
||||
|
||||
if not files:
|
||||
return "[INFO] No files found in uploads directory."
|
||||
|
||||
result = "[INFO] " + str(len(files)) + " recent files:\n\n"
|
||||
for f in files:
|
||||
name = os.path.basename(f)
|
||||
size = os.path.getsize(f)
|
||||
size_str = self._format_size(size)
|
||||
result += "- " + name + " (" + size_str + ")\n"
|
||||
|
||||
result += "\nUse extract_text_from_image(\"filename\", \"fra\") to OCR a file."
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
return f"Erreur lors de l'OCR: {str(e)}"
|
||||
return "[ERROR] " + str(e)
|
||||
|
||||
def get_available_languages(self) -> str:
|
||||
"""
|
||||
Get list of available OCR languages
|
||||
def _find_file(self, filename):
|
||||
filename = filename.strip().strip('"').strip("'")
|
||||
|
||||
Returns:
|
||||
List of available language codes
|
||||
"""
|
||||
try:
|
||||
response = requests.get(f"{self.ocr_url}/languages", timeout=10)
|
||||
if os.path.exists(filename):
|
||||
return filename
|
||||
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
languages = result.get('available_languages', [])
|
||||
return f"Langues disponibles: {', '.join(languages)}"
|
||||
else:
|
||||
return f"Erreur: {response.status_code}"
|
||||
path = os.path.join(self.uploads_dir, filename)
|
||||
if os.path.exists(path):
|
||||
return path
|
||||
|
||||
except Exception as e:
|
||||
return f"Erreur: {str(e)}"
|
||||
```
|
||||
path = os.path.join(self.host_uploads_dir, filename)
|
||||
if os.path.exists(path):
|
||||
return path
|
||||
|
||||
## Variables d'environnement
|
||||
for directory in [self.uploads_dir, self.host_uploads_dir]:
|
||||
if not os.path.exists(directory):
|
||||
continue
|
||||
|
||||
Dans les Settings d'OpenWebUI (Admin → Settings → Environment Variables) :
|
||||
pattern = os.path.join(directory, filename + "*")
|
||||
matches = glob.glob(pattern)
|
||||
if matches:
|
||||
return matches[0]
|
||||
|
||||
**Si effi-ocr est sur le MEME serveur (recommandé):**
|
||||
```
|
||||
OCR_URL=http://host.docker.internal:8300
|
||||
```
|
||||
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]
|
||||
|
||||
**Si effi-ocr est sur un autre serveur (Fedora 192.168.1.11):**
|
||||
```
|
||||
OCR_URL=http://192.168.1.11:8300
|
||||
return None
|
||||
|
||||
def _format_size(self, size):
|
||||
if size < 1024:
|
||||
return str(size) + " B"
|
||||
elif size < 1024 * 1024:
|
||||
return str(int(size / 1024)) + " KB"
|
||||
else:
|
||||
return str(int(size / (1024 * 1024))) + " MB"
|
||||
|
||||
|
||||
def extract_text_from_image(image_path, language="fra"):
|
||||
tools = Tools()
|
||||
return tools.extract_text_from_image(image_path, language)
|
||||
|
||||
def list_recent_files(count=10):
|
||||
tools = Tools()
|
||||
return tools.list_recent_files(count)
|
||||
```
|
||||
|
||||
## Utilisation
|
||||
|
||||
Une fois le Tool créé, les agents pourront l'utiliser avec ces fonctions :
|
||||
|
||||
- `extract_text_from_image("/chemin/vers/image.jpg", "fra")` - Extraire le texte d'un fichier image
|
||||
- `extract_text_from_base64("base64encoded...", "fra")` - Extraire d'une image encodée en base64
|
||||
- `get_available_languages()` - Lister les langues disponibles
|
||||
|
||||
## Exemple de prompt
|
||||
|
||||
```
|
||||
"Effi, lis-moi ce texte" [upload d'image]
|
||||
→ L'agent utilise automatiquement le Tool OCR
|
||||
extract_text_from_image("photo.jpg", "fra")
|
||||
extract_text_from_image("9af3d665_document.pdf", "fra")
|
||||
list_recent_files()
|
||||
```
|
||||
|
||||
## Langues disponibles
|
||||
## Variables d'environnement
|
||||
|
||||
- `fra` - Français
|
||||
- `eng` - Anglais
|
||||
- `fra+eng` - Français + Anglais (bilingue)
|
||||
- `deu` - Allemand
|
||||
- `spa` - Espagnol
|
||||
- `ita` - Italien
|
||||
|
||||
## Dépannage
|
||||
|
||||
**Erreur de connexion** :
|
||||
```bash
|
||||
# Vérifier que le service OCR tourne
|
||||
curl http://192.168.1.11:8300/health
|
||||
|
||||
# Redémarrer si nécessaire
|
||||
cd ~/Documents/OpenCode/Projets/Effi/effi-ocr
|
||||
sudo docker compose restart
|
||||
Dans OpenWebUI Settings:
|
||||
```
|
||||
OCR_URL=http://192.168.1.11:8301
|
||||
```
|
||||
|
||||
**Texte mal extrait** :
|
||||
- Essayer `fra+eng` pour les documents bilingues
|
||||
- Vérifier la qualité de l'image (résolution, contraste)
|
||||
- L'OCR fonctionne mieux avec des images bien éclairées
|
||||
|
||||
---
|
||||
|
||||
*Tool créé le 6 Avril 2026*
|
||||
*Updated 11 Avril 2026 - Port 8301*
|
||||
|
||||
Reference in New Issue
Block a user