Spaces:
Running
Running
Create bot.py
Browse files
bot.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import discord
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
import asyncio
|
5 |
+
|
6 |
+
# Load Discord bot token from Hugging Face Secrets
|
7 |
+
TOKEN = os.getenv("DISCORD_BOT_TOKEN") # You will set this in Hugging Face Secrets
|
8 |
+
|
9 |
+
# Hugging Face Space API URL (DeepScaleR runs here)
|
10 |
+
HF_API_URL = "http://0.0.0.0:7860/run/predict"
|
11 |
+
|
12 |
+
intents = discord.Intents.default()
|
13 |
+
intents.message_content = True
|
14 |
+
client = discord.Client(intents=intents)
|
15 |
+
|
16 |
+
@client.event
|
17 |
+
async def on_ready():
|
18 |
+
print(f'Logged in as {client.user}')
|
19 |
+
|
20 |
+
@client.event
|
21 |
+
async def on_message(message):
|
22 |
+
if message.author == client.user:
|
23 |
+
return # Avoid replying to itself
|
24 |
+
|
25 |
+
user_input = message.content.strip()
|
26 |
+
if user_input:
|
27 |
+
try:
|
28 |
+
response = requests.post(HF_API_URL, json={"data": [user_input]})
|
29 |
+
response_json = response.json()
|
30 |
+
ai_response = response_json.get("data", ["Sorry, something went wrong."])[0]
|
31 |
+
await message.channel.send(ai_response)
|
32 |
+
except Exception as e:
|
33 |
+
await message.channel.send("Error communicating with AI.")
|
34 |
+
|
35 |
+
# Run the bot
|
36 |
+
async def start_bot():
|
37 |
+
await client.start(TOKEN)
|
38 |
+
|
39 |
+
asyncio.run(start_bot())
|