Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,75 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import discord
|
| 3 |
+
from discord.ext import commands
|
| 4 |
+
import asyncio
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
+
# Load environment variables
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
# Set up Discord bot
|
| 11 |
+
intents = discord.Intents.default()
|
| 12 |
+
intents.message_content = True
|
| 13 |
+
bot = commands.Bot(command_prefix="!", intents=intents)
|
| 14 |
+
|
| 15 |
+
from gradio_client import Client
|
| 16 |
+
|
| 17 |
+
# Create the Gradio client once (you can also make this global)
|
| 18 |
+
gr_client = Client("NotSoundRated/meta-llama-Llama-3.2-3B-Instruct")
|
| 19 |
+
|
| 20 |
+
async def generate_response(prompt):
|
| 21 |
+
try:
|
| 22 |
+
# Use the client to call the model's /chat endpoint
|
| 23 |
+
result = gr_client.predict(
|
| 24 |
+
message=prompt,
|
| 25 |
+
api_name="/chat"
|
| 26 |
+
)
|
| 27 |
+
return result
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"Error generating response: {e}"
|
| 30 |
+
|
| 31 |
+
@bot.event
|
| 32 |
+
async def on_ready():
|
| 33 |
+
print(f"Bot is logged in as {bot.user}")
|
| 34 |
+
print("------")
|
| 35 |
+
|
| 36 |
+
@bot.event
|
| 37 |
+
async def on_message(message):
|
| 38 |
+
# Ignore own messages
|
| 39 |
+
if message.author == bot.user:
|
| 40 |
+
return
|
| 41 |
+
|
| 42 |
+
# Only respond to mentions
|
| 43 |
+
if bot.user.mentioned_in(message):
|
| 44 |
+
content = message.content.replace(f'<@{bot.user.id}>', '').strip()
|
| 45 |
+
if not content:
|
| 46 |
+
content = "Hello!"
|
| 47 |
+
|
| 48 |
+
async with message.channel.typing():
|
| 49 |
+
response = await generate_response(content)
|
| 50 |
+
await message.reply(response)
|
| 51 |
+
|
| 52 |
+
await bot.process_commands(message)
|
| 53 |
+
|
| 54 |
+
@bot.command(name="ping")
|
| 55 |
+
async def ping(ctx):
|
| 56 |
+
await ctx.send("Pong! I'm here.")
|
| 57 |
+
|
| 58 |
+
@bot.command(name="ask")
|
| 59 |
+
async def ask(ctx, *, question=None):
|
| 60 |
+
if question is None:
|
| 61 |
+
await ctx.send("Please provide a question. Example: !ask What is machine learning?")
|
| 62 |
+
return
|
| 63 |
+
|
| 64 |
+
async with ctx.typing():
|
| 65 |
+
response = await generate_response(question)
|
| 66 |
+
await ctx.send(response)
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
# Get token from environment variable
|
| 70 |
+
token = os.getenv("DISCORD_TOKEN")
|
| 71 |
+
|
| 72 |
+
if not token:
|
| 73 |
+
print("Error: No Discord token found.")
|
| 74 |
+
else:
|
| 75 |
+
bot.run(token)
|