from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel import gradio as gr import emoji import random class MemeAgent: def __init__(self): model = HfApiModel('mistralai/Mistral-7B-Instruct-v0.1') self.agent = CodeAgent( model=model, tools=[DuckDuckGoSearchTool()] ) self.styles = { "sarcastic": self._sarcastic_style, "vibe": self._vibe_style, "dramatic": self._dramatic_style, "uwu": self._uwu_style, "gamer": self._gamer_style, "deep": self._deep_style, "cool": self._cool_style } def _sarcastic_style(self, text): return "".join(c.upper() if i % 2 else c.lower() for i, c in enumerate(text)) + " 🥴" def _vibe_style(self, text): return "✨ " + " ✨ ".join(text.split()) + " ✨" def _dramatic_style(self, text): return f"😔✋ {text} 😭💔" def _uwu_style(self, text): text = text.replace('r', 'w').replace('l', 'w') return f"{text} uwu 🥺👉👈" def _gamer_style(self, text): return f"🎮 {text.replace('o', '0').replace('a', '4')} GG! 🎯" def _deep_style(self, text): return f"🌌 {text} 💭✨ #deep #mindblown" def _cool_style(self, text): return f"😎 {text} 🔥" def process_text(self, text, style=None): if not text.strip(): return "Por favor, ingresa algún texto", "No hay texto para procesar" # Usar el agente para analizar el texto y sugerir un estilo si no se proporciona uno if not style: prompt = f""" Analiza este texto: '{text}' Selecciona el mejor estilo de meme entre estas opciones: {list(self.styles.keys())} Solo responde con el nombre del estilo, nada más. """ style = self.agent.run(prompt).strip().lower() if style not in self.styles: style = random.choice(list(self.styles.keys())) explanation = f"El agente eligió el estilo '{style}' basado en el análisis del texto." else: explanation = f"Usando el estilo '{style}' como solicitaste." converted_text = self.styles[style](text) return converted_text, explanation def create_interface(): agent = MemeAgent() def process_input(text, style=None): return agent.process_text(text, style) iface = gr.Interface( fn=process_input, inputs=[ gr.Textbox( label="Ingresa tu texto", placeholder="Escribe algo para convertir en meme..." ), gr.Dropdown( choices=[""] + list(agent.styles.keys()), # Agregamos una opción vacía label="Estilo de meme (opcional)", value="" # Establecemos string vacío como valor por defecto ) ], outputs=[ gr.Textbox(label="Texto convertido"), gr.Textbox(label="Explicación") ], title="🎭 Convertidor de Memes con IA", description="Convierte texto normal en diferentes estilos de memes usando IA", examples=[ ["la vida es bella", "dramatic"], ["me encanta programar"], ["soy un profesional", "sarcastic"], ["victoria royale", "gamer"], ["estoy pensando en el universo", "deep"] ], theme="huggingface" ) return iface if __name__ == "__main__": demo = create_interface() demo.launch()