Spaces:
Sleeping
Sleeping
import os | |
import re | |
import aiohttp | |
import discord | |
import uvicorn | |
import threading | |
import google.generativeai as genai | |
from fastapi import FastAPI | |
from discord.ext import commands | |
app = FastAPI() | |
message_history = {} | |
# Configuración de claves (coloca tus propias claves aquí) | |
GOOGLE_AI_KEY = "AIzaSyAbarn6onc769Jv3CBqoC6VF9zb5slAtpc" | |
DISCORD_BOT_TOKEN = "MTI2NDM3NDYzNTA0NzQyNDE1Mw.GcGTmk.Htew_dUNsInFHLVh9Y2T1bdyRPnoZiuE2S_Fxw" | |
MAX_HISTORY = 12 # Historial máximo de mensajes | |
#---------------------------------------------System Prompts------------------------------------------------- | |
system_prompt = "You are a helpful bot!" | |
image_prompt = "You are a helpful bot!" | |
#---------------------------------------------AI Configuration------------------------------------------------- | |
genai.configure(api_key=GOOGLE_AI_KEY) | |
text_generation_config = { | |
"temperature": 0.9, | |
"top_p": 1, | |
"top_k": 1, | |
"max_output_tokens": 512, | |
} | |
image_generation_config = { | |
"temperature": 0.4, | |
"top_p": 1, | |
"top_k": 32, | |
"max_output_tokens": 512, | |
} | |
safety_settings = [ | |
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, | |
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, | |
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, | |
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"} | |
] | |
text_model = genai.GenerativeModel( | |
model_name="gemini-2.0-flash-thinking-exp-01-21", | |
generation_config=text_generation_config, | |
safety_settings=safety_settings, | |
system_instruction=system_prompt | |
) | |
image_model = genai.GenerativeModel( | |
model_name="gemini-2.0-flash-thinking-exp-01-21", | |
generation_config=image_generation_config, | |
safety_settings=safety_settings, | |
system_instruction=image_prompt | |
) | |
#---------------------------------------------Discord Bot Setup------------------------------------------------- | |
intents = discord.Intents.default() | |
intents.members = True | |
intents.message_content = True | |
bot = commands.Bot(command_prefix='!', description="Assistant bot", intents=intents) | |
async def on_ready(): | |
print("----------------------------------------") | |
print(f'Bot de Discord conectado como {bot.user}') | |
print("----------------------------------------") | |
async def on_message(message): | |
if message.author == bot.user or message.mention_everyone: | |
return | |
if bot.user.mentioned_in(message) or isinstance(message.channel, discord.DMChannel): | |
cleaned_text = clean_discord_message(message.content) | |
async with message.channel.typing(): | |
if message.attachments: | |
print(f"Mensaje con imagen de {message.author}: {cleaned_text}") | |
for attachment in message.attachments: | |
if any(attachment.filename.lower().endswith(ext) for ext in ['.png', '.jpg', '.jpeg', '.gif', '.webp']): | |
await message.add_reaction('🎨') | |
async with aiohttp.ClientSession() as session: | |
async with session.get(attachment.url) as resp: | |
if resp.status == 200: | |
image_data = await resp.read() | |
response_text = await generate_response_with_image_and_text(image_data, cleaned_text) | |
await split_and_send_messages(message, response_text, 1700) | |
return | |
else: | |
print(f"Mensaje de {message.author}: {cleaned_text}") | |
if "RESET" in cleaned_text: | |
if message.author.id in message_history: | |
del message_history[message.author.id] | |
await message.channel.send(f"🤖 Historial reiniciado para {message.author.name}") | |
return | |
await message.add_reaction('💬') | |
update_message_history(message.author.id, cleaned_text) | |
response_text = await generate_response_with_text(get_formatted_message_history(message.author.id)) | |
update_message_history(message.author.id, response_text) | |
await split_and_send_messages(message, response_text, 1700) | |
#---------------------------------------------Funciones de IA------------------------------------------------- | |
async def generate_response_with_text(message_text): | |
response = text_model.generate_content([message_text]) | |
return response.text if not response._error else f"❌ Error: {response._error}" | |
async def generate_response_with_image_and_text(image_data, text): | |
prompt_parts = [{"mime_type": "image/jpeg", "data": image_data}, f"\n{text or 'What is this a picture of?'}"] | |
response = image_model.generate_content(prompt_parts) | |
return response.text if not response._error else f"❌ Error: {response._error}" | |
#---------------------------------------------Manejo de Historial------------------------------------------------- | |
def update_message_history(user_id, text): | |
if user_id in message_history: | |
message_history[user_id].append(text) | |
if len(message_history[user_id]) > MAX_HISTORY: | |
message_history[user_id].pop(0) | |
else: | |
message_history[user_id] = [text] | |
def get_formatted_message_history(user_id): | |
return '\n\n'.join(message_history.get(user_id, ["No hay historial de mensajes"])) | |
#---------------------------------------------Funciones Utilitarias------------------------------------------------- | |
async def split_and_send_messages(message_system, text, max_length): | |
for i in range(0, len(text), max_length): | |
await message_system.channel.send(text[i:i+max_length]) | |
def clean_discord_message(input_string): | |
return re.sub(r'<[^>]+>', '', input_string) | |
#---------------------------------------------Endpoints FastAPI------------------------------------------------- | |
async def read_root(): | |
return {"status": "API en funcionamiento"} | |
#---------------------------------------------Ejecución del Servicio------------------------------------------------- | |
def run_discord_bot(): | |
bot.run(DISCORD_BOT_TOKEN) | |
if __name__ == "__main__": | |
# Iniciar bot de Discord en segundo plano | |
discord_thread = threading.Thread(target=run_discord_bot) | |
discord_thread.start() | |
# Iniciar servidor FastAPI | |
uvicorn.run(app, host="0.0.0.0", port=7860) |