Spaces:
Sleeping
Sleeping
Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
from langdetect import detect
|
| 4 |
+
from langdetect.lang_detect_exception import LangDetectException
|
| 5 |
+
from googletrans import Translator
|
| 6 |
+
from deep_translator import GoogleTranslator
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import re
|
| 9 |
+
import langid
|
| 10 |
+
|
| 11 |
+
class Translation:
|
| 12 |
+
def __init__(self, text, target_lang):
|
| 13 |
+
self.text = text
|
| 14 |
+
|
| 15 |
+
def translatef(text, target_lang):
|
| 16 |
+
try:
|
| 17 |
+
print(text, target_lang)
|
| 18 |
+
if target_lang=='zh':
|
| 19 |
+
target_lang=='zh-CN'
|
| 20 |
+
translator = GoogleTranslator(source='auto', target=target_lang)
|
| 21 |
+
translated = translator.translate(text)
|
| 22 |
+
print(translated)
|
| 23 |
+
#return translated.text
|
| 24 |
+
return translated
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"Error during translation: {str(e)}"
|
| 27 |
+
|
| 28 |
+
@staticmethod
|
| 29 |
+
|
| 30 |
+
def detect_language(text):
|
| 31 |
+
try:
|
| 32 |
+
lang, confidence = langid.classify(text)
|
| 33 |
+
print(text)
|
| 34 |
+
#print(lang)
|
| 35 |
+
return lang # Devuelve el c贸digo ISO del idioma
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"ERROR: {str(e)}"
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def search_error_in_excel(text, excel_path="C:/Users/15572890/Desktop/StockPrediction/Coding/CorrelacionStocks/APIS-StockPlatforms/APIs/Voz/RTTranslator/bloom-multilingual-chatbot-main/scripts/errors.xlsx"):
|
| 41 |
+
"""
|
| 42 |
+
Busca informaci贸n en un archivo Excel seg煤n palabras clave y muestra texto, enlace o imagen.
|
| 43 |
+
|
| 44 |
+
:param text: Texto proporcionado por el usuario.
|
| 45 |
+
:param excel_path: Ruta al archivo Excel.
|
| 46 |
+
:return: Texto, enlace o imagen seg煤n la consulta del usuario.
|
| 47 |
+
"""
|
| 48 |
+
# Detectar palabras clave "error" o "errores" y un n煤mero
|
| 49 |
+
match = re.search(r"(?:error|errores)\s*(\d+)", text, re.IGNORECASE)
|
| 50 |
+
if not match:
|
| 51 |
+
return "No se encontr贸 ning煤n c贸digo de error en el texto.", None
|
| 52 |
+
|
| 53 |
+
error_code = int(match.group(1))
|
| 54 |
+
|
| 55 |
+
# Detectar tipo de consulta: "texto", "v铆deo", o "foto"
|
| 56 |
+
is_text = "protocolo" in text.lower()
|
| 57 |
+
is_video = "video" in text.lower()
|
| 58 |
+
is_photo = "foto" in text.lower() or "imagen" in text.lower()
|
| 59 |
+
|
| 60 |
+
if not (is_text or is_video or is_photo):
|
| 61 |
+
return "Debe especificar si desea 'texto', 'video' o 'foto' en su consulta.", None
|
| 62 |
+
|
| 63 |
+
# Cargar el archivo Excel
|
| 64 |
+
try:
|
| 65 |
+
df = pd.read_excel(excel_path)
|
| 66 |
+
except FileNotFoundError:
|
| 67 |
+
return f"El archivo {excel_path} no fue encontrado.", None
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return f"Error al abrir el archivo Excel: {str(e)}", None
|
| 70 |
+
|
| 71 |
+
# Verificar si el c贸digo de error existe en las columnas
|
| 72 |
+
if error_code not in df.columns:
|
| 73 |
+
return f"No se encontr贸 informaci贸n para el c贸digo de error {error_code}.", None
|
| 74 |
+
|
| 75 |
+
# Devolver informaci贸n basada en el tipo de consulta
|
| 76 |
+
try:
|
| 77 |
+
if is_text:
|
| 78 |
+
return str(df[error_code].dropna().iloc[0]), "protocolo" # Primera fila
|
| 79 |
+
elif is_video:
|
| 80 |
+
return str(df[error_code].dropna().iloc[1]), "video" # Segunda fila
|
| 81 |
+
elif is_photo:
|
| 82 |
+
image_path = str(df[error_code].dropna().iloc[2]) # Tercera fila
|
| 83 |
+
return image_path, "foto" # Devolver la ruta de la imagen
|
| 84 |
+
except IndexError:
|
| 85 |
+
return f"La informaci贸n para el c贸digo de error {error_code} est谩 incompleta en el archivo Excel.", None
|