File size: 3,156 Bytes
df3d965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bf3c37
df3d965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
796ddda
df3d965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85


from langdetect import detect
from langdetect.lang_detect_exception import LangDetectException
from googletrans import Translator
from deep_translator import GoogleTranslator
import pandas as pd
import re
import langid

class Translation:
    def __init__(self, text, target_lang):
        self.text = text
    
    def translatef(text, target_lang):
        try:
            print(text, target_lang)
            if target_lang=='zh':
                target_lang=='zh-CN'
            translator = GoogleTranslator(source='auto', target=target_lang)
            translated = translator.translate(text)
            print(translated)
            #return translated.text
            return translated
        except Exception as e:
            return f"Error during translation: {str(e)}"

    @staticmethod   

    def detect_language(text):
        try:
            lang, confidence = langid.classify(text)
            print(text)
            #print(lang)
            return lang  # Devuelve el c贸digo ISO del idioma
        except Exception as e:
            return f"ERROR: {str(e)}"


def search_error_in_excel(text, excel_path="errors.xlsx"):
    """
    Busca informaci贸n en un archivo Excel seg煤n palabras clave y muestra texto, enlace o imagen.
    
    :param text: Texto proporcionado por el usuario.
    :param excel_path: Ruta al archivo Excel.
    :return: Texto, enlace o imagen seg煤n la consulta del usuario.
    """
    # Detectar palabras clave "error" o "errores" y un n煤mero
    match = re.search(r"(?:error|errores)\s*(\d+)", text, re.IGNORECASE)
    if not match:
        return "No se encontr贸 ning煤n c贸digo de error en el texto.", None

    error_code = int(match.group(1))

    # Detectar tipo de consulta: "texto", "v铆deo", o "foto"
    is_text = "protocolo" in text.lower() or "registro" in text.lower() or "log" in text.lower()
    is_video = "video" in text.lower()
    is_photo = "foto" in text.lower() or "imagen" in text.lower()

    if not (is_text or is_video or is_photo):
        return "Debe especificar si desea 'texto', 'video' o 'foto' en su consulta.", None

    # Cargar el archivo Excel
    try:
        df = pd.read_excel(excel_path)
    except FileNotFoundError:
        return f"El archivo {excel_path} no fue encontrado.", None
    except Exception as e:
        return f"Error al abrir el archivo Excel: {str(e)}", None

    # Verificar si el c贸digo de error existe en las columnas
    if error_code not in df.columns:
        return f"No se encontr贸 informaci贸n para el c贸digo de error {error_code}.", None

    # Devolver informaci贸n basada en el tipo de consulta
    try:
        if is_text:
            return str(df[error_code].dropna().iloc[0]),  "protocolo"  # Primera fila
        elif is_video:
            return str(df[error_code].dropna().iloc[1]),  "video"  # Segunda fila
        elif is_photo:
            image_path = str(df[error_code].dropna().iloc[2])  # Tercera fila
            return image_path, "foto"  # Devolver la ruta de la imagen
    except IndexError:
        return f"La informaci贸n para el c贸digo de error {error_code} est谩 incompleta en el archivo Excel.", None