Spaces:
Sleeping
Sleeping
Commit
·
4cd8188
1
Parent(s):
650af0f
Cambio de Agent por CodeAgent es app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,14 @@
|
|
| 1 |
-
from smolagents import
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import pipeline
|
| 4 |
import emoji
|
| 5 |
import random
|
| 6 |
|
| 7 |
-
|
| 8 |
-
AGENT_PROMPT = """
|
| 9 |
-
Eres un experto convertidor de texto a memes. Tu trabajo es tomar el texto del usuario y convertirlo
|
| 10 |
-
en un estilo de meme divertido y apropiado. Tienes los siguientes estilos disponibles:
|
| 11 |
-
|
| 12 |
-
- Sarcástico: Alternando mayúsculas y minúsculas
|
| 13 |
-
- Vibe: Añadiendo ✨ entre palabras
|
| 14 |
-
- Dramático: Usando emojis dramáticos
|
| 15 |
-
- UwU: Estilo kawaii
|
| 16 |
-
- Gamer: Usando lenguaje de gaming
|
| 17 |
-
- Deep: Estilo filosófico
|
| 18 |
-
- Cool: Estilo relajado con emojis cool
|
| 19 |
-
|
| 20 |
-
Debes:
|
| 21 |
-
1. Analizar el texto del usuario
|
| 22 |
-
2. Elegir el mejor estilo si no se especifica uno
|
| 23 |
-
3. Convertir el texto al estilo seleccionado
|
| 24 |
-
4. Añadir emojis apropiados
|
| 25 |
-
5. Explicar brevemente por qué elegiste ese estilo
|
| 26 |
-
|
| 27 |
-
Mantén un tono divertido y amigable en todo momento.
|
| 28 |
-
"""
|
| 29 |
-
|
| 30 |
-
class MemeAgent(Agent):
|
| 31 |
def __init__(self):
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
self.styles = {
|
| 34 |
"sarcastic": self._sarcastic_style,
|
| 35 |
"vibe": self._vibe_style,
|
|
@@ -39,7 +18,7 @@ class MemeAgent(Agent):
|
|
| 39 |
"deep": self._deep_style,
|
| 40 |
"cool": self._cool_style
|
| 41 |
}
|
| 42 |
-
|
| 43 |
def _sarcastic_style(self, text):
|
| 44 |
return "".join(c.upper() if i % 2 else c.lower() for i, c in enumerate(text)) + " 🥴"
|
| 45 |
|
|
@@ -62,41 +41,32 @@ class MemeAgent(Agent):
|
|
| 62 |
def _cool_style(self, text):
|
| 63 |
return f"😎 {text} 🔥"
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
if style:
|
| 69 |
-
context += f"Estilo solicitado: {style}\n"
|
| 70 |
-
else:
|
| 71 |
-
context += "No se especificó estilo, elige el más apropiado.\n"
|
| 72 |
-
|
| 73 |
-
# Obtener la respuesta del agente
|
| 74 |
-
response = await self.run(context)
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
if
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
else:
|
| 80 |
-
|
| 81 |
-
for style_name in self.styles:
|
| 82 |
-
if style_name.lower() in response.lower():
|
| 83 |
-
converted_text = self.styles[style_name](text)
|
| 84 |
-
break
|
| 85 |
-
else:
|
| 86 |
-
converted_text = self._vibe_style(text) # Estilo por defecto
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
"explanation": response
|
| 91 |
-
}
|
| 92 |
|
| 93 |
-
# Interfaz de Gradio
|
| 94 |
def create_interface():
|
| 95 |
agent = MemeAgent()
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
return result["converted_text"], result["explanation"]
|
| 100 |
|
| 101 |
iface = gr.Interface(
|
| 102 |
fn=process_input,
|
|
@@ -113,9 +83,9 @@ def create_interface():
|
|
| 113 |
],
|
| 114 |
outputs=[
|
| 115 |
gr.Textbox(label="Texto convertido"),
|
| 116 |
-
gr.Textbox(label="Explicación
|
| 117 |
],
|
| 118 |
-
title="🎭
|
| 119 |
description="Convierte texto normal en diferentes estilos de memes usando IA",
|
| 120 |
examples=[
|
| 121 |
["la vida es bella", "dramatic"],
|
|
@@ -128,7 +98,6 @@ def create_interface():
|
|
| 128 |
)
|
| 129 |
return iface
|
| 130 |
|
| 131 |
-
|
| 132 |
if __name__ == "__main__":
|
| 133 |
demo = create_interface()
|
| 134 |
demo.launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import emoji
|
| 4 |
import random
|
| 5 |
|
| 6 |
+
class MemeAgent:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def __init__(self):
|
| 8 |
+
self.agent = CodeAgent(
|
| 9 |
+
tools=[DuckDuckGoSearchTool()],
|
| 10 |
+
llm=HfApiModel('mistralai/Mistral-7B-Instruct-v0.1')
|
| 11 |
+
)
|
| 12 |
self.styles = {
|
| 13 |
"sarcastic": self._sarcastic_style,
|
| 14 |
"vibe": self._vibe_style,
|
|
|
|
| 18 |
"deep": self._deep_style,
|
| 19 |
"cool": self._cool_style
|
| 20 |
}
|
| 21 |
+
|
| 22 |
def _sarcastic_style(self, text):
|
| 23 |
return "".join(c.upper() if i % 2 else c.lower() for i, c in enumerate(text)) + " 🥴"
|
| 24 |
|
|
|
|
| 41 |
def _cool_style(self, text):
|
| 42 |
return f"😎 {text} 🔥"
|
| 43 |
|
| 44 |
+
def process_text(self, text, style=None):
|
| 45 |
+
if not text.strip():
|
| 46 |
+
return "Por favor, ingresa algún texto", "No hay texto para procesar"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# Usar el agente para analizar el texto y sugerir un estilo si no se proporciona uno
|
| 49 |
+
if not style:
|
| 50 |
+
prompt = f"""
|
| 51 |
+
Analiza este texto: '{text}'
|
| 52 |
+
Selecciona el mejor estilo de meme entre estas opciones: {list(self.styles.keys())}
|
| 53 |
+
Solo responde con el nombre del estilo, nada más.
|
| 54 |
+
"""
|
| 55 |
+
style = self.agent.run(prompt).strip().lower()
|
| 56 |
+
if style not in self.styles:
|
| 57 |
+
style = random.choice(list(self.styles.keys()))
|
| 58 |
+
explanation = f"El agente eligió el estilo '{style}' basado en el análisis del texto."
|
| 59 |
else:
|
| 60 |
+
explanation = f"Usando el estilo '{style}' como solicitaste."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
converted_text = self.styles[style](text)
|
| 63 |
+
return converted_text, explanation
|
|
|
|
|
|
|
| 64 |
|
|
|
|
| 65 |
def create_interface():
|
| 66 |
agent = MemeAgent()
|
| 67 |
|
| 68 |
+
def process_input(text, style=None):
|
| 69 |
+
return agent.process_text(text, style)
|
|
|
|
| 70 |
|
| 71 |
iface = gr.Interface(
|
| 72 |
fn=process_input,
|
|
|
|
| 83 |
],
|
| 84 |
outputs=[
|
| 85 |
gr.Textbox(label="Texto convertido"),
|
| 86 |
+
gr.Textbox(label="Explicación")
|
| 87 |
],
|
| 88 |
+
title="🎭 Convertidor de Memes con IA",
|
| 89 |
description="Convierte texto normal en diferentes estilos de memes usando IA",
|
| 90 |
examples=[
|
| 91 |
["la vida es bella", "dramatic"],
|
|
|
|
| 98 |
)
|
| 99 |
return iface
|
| 100 |
|
|
|
|
| 101 |
if __name__ == "__main__":
|
| 102 |
demo = create_interface()
|
| 103 |
demo.launch()
|