File size: 1,140 Bytes
d76e213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import discord
import requests
import os
import asyncio

# Load Discord bot token from Hugging Face Secrets
TOKEN = os.getenv("DISCORD_BOT_TOKEN")  # You will set this in Hugging Face Secrets

# Hugging Face Space API URL (DeepScaleR runs here)
HF_API_URL = "http://0.0.0.0:7860/run/predict"  

intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f'Logged in as {client.user}')

@client.event
async def on_message(message):
    if message.author == client.user:
        return  # Avoid replying to itself

    user_input = message.content.strip()
    if user_input:
        try:
            response = requests.post(HF_API_URL, json={"data": [user_input]})
            response_json = response.json()
            ai_response = response_json.get("data", ["Sorry, something went wrong."])[0]
            await message.channel.send(ai_response)
        except Exception as e:
            await message.channel.send("Error communicating with AI.")

# Run the bot
async def start_bot():
    await client.start(TOKEN)

asyncio.run(start_bot())