File size: 2,740 Bytes
bc2c36b
9b1b1fc
 
11c7767
bc2c36b
 
 
 
12b5ed5
 
 
11c7767
 
 
12b5ed5
a73f742
9b1b1fc
eccd759
 
9b1b1fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12b5ed5
a73f742
12b5ed5
a73f742
9b1b1fc
 
bc2c36b
a73f742
bc2c36b
 
 
 
 
9b1b1fc
 
bc2c36b
9b1b1fc
bc2c36b
1386c15
9b1b1fc
6ca45ef
 
eccd759
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
import gradio as gr
import comunicacion_gmail
import json
from urllib.parse import urlparse, parse_qs

def gmail_interface(accion, parametros):
    """Funci贸n que llama a gmail_tool y formatea la respuesta."""
    try:
        print(f"Acci贸n recibida: {accion}")  # Depuraci贸n
        print(f"Par谩metros recibidos: {parametros}")  # Depuraci贸n
        
        if parametros:  # Si hay par谩metros, verifica si contiene el auth_code
            try:
                parametros = json.loads(parametros) # Convierte la cadena JSON a un diccionario
            except json.JSONDecodeError as e:
                return "Error al decodificar JSON: " + str(e), "" #  Devolver dos valores

           
        resultado = comunicacion_gmail.gmail_tool(accion)

        # Formatear la respuesta de gmail_tool
        if accion == "leer_correos":
            messages = resultado.get("messages", [])
            formatted_messages = ""
            for message in messages:
                formatted_messages += f"ID: {message['id']}\nCuerpo: {message['body']}\n---\n" # Formateo b谩sico.  Adaptar seg煤n sea necesario.
            return formatted_messages

        elif accion == "enviar_correo":
            message_id = resultado.get("message_id")
            return f"Correo enviado con ID: {message_id}"


        elif accion == "verificar_almacenamiento":
            # Formatear la informaci贸n de almacenamiento
            storage = resultado.get("storageQuota", {}) # Manejar el caso donde no hay storageQuota
            formatted_storage = f"L铆mite: {storage.get('limit', 'N/A')}\nUso: {storage.get('usage', 'N/A')}\nEn Drive: {storage.get('usageInDrive', 'N/A')}"
            return formatted_storage
        
        elif "error" in resultado:
            return f"Error en gmail_tool: {resultado['error']}", "" #  Muestra el error de gmail_tool
        else:
            return str(resultado), ""  # Respuesta por defecto


    except Exception as e:
        return f"Error en gmail_interface: {e}", "" 


iface = gr.Interface(
    fn=gmail_interface,
    inputs=[
        gr.Dropdown(["leer_correos", "enviar_correo", "verificar_almacenamiento"], label="Acci贸n", info="Selecciona la acci贸n que quieres realizar con Gmail."),
        gr.Textbox(label="Par谩metros (JSON)", lines=3, info="Introduce los par谩metros en formato JSON (ej. {\"maxResults\": 5})."),
    ],
    outputs=[gr.Textbox(label="Resultado"), gr.HTML(visible=False)], #  A帽ade gr.HTML aunque no se use en este ejemplo
    title="Herramienta de Gmail",
    description="Herramienta para interactuar con Gmail.",
    allow_flagging="never",
)

iface.queue(concurrency_count=5).launch(share=True) #  A帽ade concurrency_count para manejar m煤ltiples solicitudes