Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,59 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import comunicacion_gmail
|
|
|
|
| 3 |
|
| 4 |
def gmail_interface(accion, parametros):
|
| 5 |
"""Funci贸n que llama a gmail_tool y formatea la respuesta."""
|
| 6 |
try:
|
|
|
|
|
|
|
| 7 |
resultado = comunicacion_gmail.gmail_tool(accion, parametros)
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
except Exception as e:
|
| 11 |
-
return f"Error: {e}"
|
| 12 |
|
| 13 |
|
| 14 |
iface = gr.Interface(
|
| 15 |
fn=gmail_interface,
|
| 16 |
inputs=[
|
| 17 |
-
gr.Dropdown(["leer_correos", "enviar_correo", "verificar_almacenamiento"], label="Acci贸n"
|
| 18 |
-
gr.Textbox(label="Par谩metros (JSON)", lines=3
|
| 19 |
],
|
| 20 |
-
outputs=gr.Textbox(label="Resultado"),
|
| 21 |
title="Herramienta de Gmail",
|
| 22 |
description="Herramienta para interactuar con Gmail.",
|
| 23 |
-
allow_flagging="never",
|
| 24 |
)
|
| 25 |
|
| 26 |
iface.queue().launch(share=True) # Agrega .queue().launch(share=True), importante para el flujo de OAuth.
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import comunicacion_gmail
|
| 3 |
+
import json
|
| 4 |
|
| 5 |
def gmail_interface(accion, parametros):
|
| 6 |
"""Funci贸n que llama a gmail_tool y formatea la respuesta."""
|
| 7 |
try:
|
| 8 |
+
parametros = json.loads(parametros) # Convierte la cadena JSON a un diccionario
|
| 9 |
+
|
| 10 |
resultado = comunicacion_gmail.gmail_tool(accion, parametros)
|
| 11 |
+
|
| 12 |
+
if "requires_auth" in resultado and resultado["requires_auth"]:
|
| 13 |
+
auth_url = resultado["auth_url"]
|
| 14 |
+
return gr.update(visible=True, value=f"Por favor, visita esta URL para autorizar la aplicaci贸n:\n{auth_url}\n\nLuego, copia el c贸digo de autorizaci贸n y p茅galo aqu铆 como par谩metro JSON: {{\"auth_code\": \"TU_CODIGO\"}}."), gr.update(visible=True) # Deja el campo para pegar el c贸digo visible
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Formatear la respuesta de gmail_tool
|
| 18 |
+
if accion == "leer_correos":
|
| 19 |
+
messages = resultado.get("messages", [])
|
| 20 |
+
formatted_messages = ""
|
| 21 |
+
for message in messages:
|
| 22 |
+
formatted_messages += f"ID: {message['id']}\nCuerpo: {message['body']}\n---\n" # Formateo b谩sico. Adaptar seg煤n sea necesario.
|
| 23 |
+
return formatted_messages
|
| 24 |
+
|
| 25 |
+
elif accion == "enviar_correo":
|
| 26 |
+
message_id = resultado.get("message_id")
|
| 27 |
+
return f"Correo enviado con ID: {message_id}"
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
elif accion == "verificar_almacenamiento":
|
| 31 |
+
# Formatear la informaci贸n de almacenamiento
|
| 32 |
+
storage = resultado.get("storageQuota", {}) # Manejar el caso donde no hay storageQuota
|
| 33 |
+
formatted_storage = f"L铆mite: {storage.get('limit', 'N/A')}\nUso: {storage.get('usage', 'N/A')}\nEn Drive: {storage.get('usageInDrive', 'N/A')}"
|
| 34 |
+
return formatted_storage
|
| 35 |
+
|
| 36 |
+
elif "error" in resultado: # Manejar errores de gmail_tool
|
| 37 |
+
return f"Error: {resultado['error']}"
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
return str(resultado) # Respuesta por defecto si no hay formato espec铆fico
|
| 41 |
+
|
| 42 |
+
|
| 43 |
except Exception as e:
|
| 44 |
+
return f"Error en gmail_interface: {e}"
|
| 45 |
|
| 46 |
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=gmail_interface,
|
| 49 |
inputs=[
|
| 50 |
+
gr.Dropdown(["leer_correos", "enviar_correo", "verificar_almacenamiento"], label="Acci贸n", info="Selecciona la acci贸n que quieres realizar con Gmail."),
|
| 51 |
+
gr.Textbox(label="Par谩metros (JSON)", lines=3, info="Introduce los par谩metros en formato JSON (ej. {\"maxResults\": 5})."),
|
| 52 |
],
|
| 53 |
+
outputs=[gr.Textbox(label="Resultado"), gr.HTML(visible=False)], # A帽ade gr.HTML aunque no se use en este ejemplo
|
| 54 |
title="Herramienta de Gmail",
|
| 55 |
description="Herramienta para interactuar con Gmail.",
|
| 56 |
+
allow_flagging="never",
|
| 57 |
)
|
| 58 |
|
| 59 |
iface.queue().launch(share=True) # Agrega .queue().launch(share=True), importante para el flujo de OAuth.
|