Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# bot.py
|
| 2 |
+
import discord
|
| 3 |
+
from gradio_client import Client
|
| 4 |
+
from huggingface_hub import InferenceClient
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Get tokens from environment variables (Hugging Face Spaces secrets)
|
| 8 |
+
TOKEN = os.getenv("DISCORD_TOKEN")
|
| 9 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
+
|
| 11 |
+
# Check if tokens are available
|
| 12 |
+
if not TOKEN or not HF_TOKEN:
|
| 13 |
+
raise ValueError("DISCORD_TOKEN and HF_TOKEN must be set as environment variables in Spaces secrets")
|
| 14 |
+
|
| 15 |
+
# Configuration des intents Discord
|
| 16 |
+
intents = discord.Intents.default()
|
| 17 |
+
intents.message_content = True
|
| 18 |
+
|
| 19 |
+
# Initialisation des clients
|
| 20 |
+
discord_client = discord.Client(intents=intents)
|
| 21 |
+
hf_client = InferenceClient(api_key=HF_TOKEN)
|
| 22 |
+
|
| 23 |
+
# Fonction pour obtenir une réponse du modèle
|
| 24 |
+
async def get_model_response(message):
|
| 25 |
+
messages = [
|
| 26 |
+
{"role": "user", "content": message}
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
response = ""
|
| 30 |
+
stream = hf_client.chat.completions.create(
|
| 31 |
+
model="Qwen/Qwen2.5-72B-Instruct",
|
| 32 |
+
messages=messages,
|
| 33 |
+
temperature=0.5,
|
| 34 |
+
max_tokens=2048,
|
| 35 |
+
top_p=0.7,
|
| 36 |
+
stream=True
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
for chunk in stream:
|
| 40 |
+
if chunk.choices[0].delta.content:
|
| 41 |
+
response += chunk.choices[0].delta.content
|
| 42 |
+
|
| 43 |
+
return response
|
| 44 |
+
|
| 45 |
+
@discord_client.event
|
| 46 |
+
async def on_ready():
|
| 47 |
+
print(f'Connecté en tant que {discord_client.user}')
|
| 48 |
+
|
| 49 |
+
@discord_client.event
|
| 50 |
+
async def on_message(message):
|
| 51 |
+
# Ignorer les messages du bot lui-même
|
| 52 |
+
if message.author == discord_client.user:
|
| 53 |
+
return
|
| 54 |
+
|
| 55 |
+
# Vérifier si le bot est mentionné
|
| 56 |
+
if discord_client.user in message.mentions:
|
| 57 |
+
try:
|
| 58 |
+
# Extraire le contenu du message sans la mention
|
| 59 |
+
clean_message = message.content.replace(f"<@{discord_client.user.id}>", "").strip()
|
| 60 |
+
|
| 61 |
+
if not clean_message:
|
| 62 |
+
await message.channel.send("Veuillez inclure un message après ma mention !")
|
| 63 |
+
return
|
| 64 |
+
|
| 65 |
+
# Obtenir la réponse du modèle
|
| 66 |
+
async with message.channel.typing():
|
| 67 |
+
response = await get_model_response(clean_message)
|
| 68 |
+
|
| 69 |
+
# Diviser la réponse si elle dépasse la limite Discord (2000 caractères)
|
| 70 |
+
if len(response) > 2000:
|
| 71 |
+
parts = [response[i:i+2000] for i in range(0, len(response), 2000)]
|
| 72 |
+
for part in parts:
|
| 73 |
+
await message.channel.send(part)
|
| 74 |
+
else:
|
| 75 |
+
await message.channel.send(response)
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
await message.channel.send(f"Une erreur est survenue : {str(e)}")
|
| 79 |
+
|
| 80 |
+
# Lancer le bot
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
# Vérifier que les tokens sont définis
|
| 83 |
+
if TOKEN == "VOTRE_TOKEN_DISCORD" or HF_TOKEN == "VOTRE_TOKEN_HF":
|
| 84 |
+
print("Veuillez remplacer les tokens par vos propres tokens Discord et Hugging Face")
|
| 85 |
+
else:
|
| 86 |
+
discord_client.run(TOKEN)
|