Update app.py
Browse files
app.py
CHANGED
@@ -1,144 +1,77 @@
|
|
1 |
-
import discord
|
2 |
import os
|
3 |
-
import
|
4 |
-
import sys
|
5 |
import requests
|
6 |
from dotenv import load_dotenv
|
7 |
-
from
|
8 |
-
|
|
|
|
|
|
|
9 |
|
10 |
-
# Load
|
11 |
load_dotenv()
|
12 |
-
TOKEN = os.getenv("BOT_TOKEN")
|
13 |
-
|
14 |
-
# Hugging Face client setup
|
15 |
-
client_ai = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
intents.messages = True
|
20 |
-
intents.message_content = True
|
21 |
-
bot = discord.Client(intents=intents)
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
MAX_TOKENS = 512
|
26 |
-
TEMPERATURE = 0.7
|
27 |
-
TOP_P = 0.95
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
|
|
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
{"role": "system", "content": SYSTEM_PROMPT},
|
38 |
-
{"role": "user", "content": message_content}
|
39 |
-
]
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
messages,
|
44 |
-
max_tokens=MAX_TOKENS,
|
45 |
-
stream=True,
|
46 |
-
temperature=TEMPERATURE,
|
47 |
-
top_p=TOP_P,
|
48 |
-
):
|
49 |
-
if part.choices[0].delta.content:
|
50 |
-
full_response += part.choices[0].delta.content
|
51 |
-
return full_response
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
"timestamp": datetime.utcnow().isoformat(),
|
56 |
-
"user_message": user_message,
|
57 |
-
"bot_response": bot_response
|
58 |
-
}
|
59 |
-
with open(CONVERSATION_LOG, "a", encoding="utf-8") as f:
|
60 |
-
f.write(json.dumps(log_entry) + "\n")
|
61 |
-
|
62 |
-
def generate_dynamic_command():
|
63 |
-
"""Generate a command using the external API (0zyen)"""
|
64 |
-
url = "https://0zyen.vercel.app/api/chat/completions"
|
65 |
payload = {
|
66 |
-
"content":
|
67 |
-
"model": "
|
68 |
}
|
69 |
-
|
70 |
-
# Get response from the external API
|
71 |
response = requests.post(url, json=payload)
|
72 |
-
|
73 |
if response.status_code == 200:
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
cleaned_text = generated_text.replace('```', '').replace('\\n', '\n').strip()
|
78 |
-
|
79 |
-
# Generate the command and save it to app.py
|
80 |
-
command_code = f"""
|
81 |
-
@bot.command()
|
82 |
-
async def test(ctx):
|
83 |
-
{cleaned_text}
|
84 |
-
"""
|
85 |
-
try:
|
86 |
-
# Append to app.py
|
87 |
-
with open("app.py", "a", encoding="utf-8") as f:
|
88 |
-
f.write(command_code)
|
89 |
-
print(f"Successfully added the command to app.py")
|
90 |
-
|
91 |
-
# Optional: Reload bot after adding command
|
92 |
-
os.execv(sys.executable, ['python'] + sys.argv)
|
93 |
|
94 |
-
|
95 |
-
|
|
|
96 |
|
97 |
-
#
|
|
|
|
|
|
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
log_entry = {
|
102 |
-
"timestamp": datetime.utcnow().isoformat(),
|
103 |
-
"error_message": error_message
|
104 |
-
}
|
105 |
-
with open("app.py", "a", encoding="utf-8") as f:
|
106 |
-
f.write("\n# Error Log\n")
|
107 |
-
f.write(json.dumps(log_entry) + "\n")
|
108 |
-
|
109 |
-
# === Events ===
|
110 |
-
|
111 |
-
@bot.event
|
112 |
async def on_ready():
|
113 |
-
print(f"Bot
|
114 |
|
115 |
-
|
|
|
116 |
async def on_message(message):
|
117 |
-
|
118 |
-
if message.author == bot.user:
|
119 |
return
|
120 |
|
121 |
-
#
|
122 |
-
|
123 |
-
|
124 |
-
|
|
|
125 |
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
if response.strip() != "":
|
131 |
-
await message.channel.send(response[:2000]) # Discord limit is 2000 chars
|
132 |
-
save_conversation(user_input, response)
|
133 |
-
except Exception as e:
|
134 |
-
print(f"Error responding to message: {e}")
|
135 |
-
diagnose_and_save_error(f"Error responding to message: {e}")
|
136 |
|
137 |
-
#
|
138 |
-
|
139 |
-
bot.run(TOKEN)
|
140 |
-
except Exception as e:
|
141 |
-
print(f"Bot crashed: {e}")
|
142 |
-
diagnose_and_save_error(f"Bot crashed: {e}")
|
143 |
-
# Optional: You can auto-restart after crash
|
144 |
-
# os.execv(sys.executable, ['python'] + sys.argv)
|
|
|
|
|
1 |
import os
|
2 |
+
import discord
|
|
|
3 |
import requests
|
4 |
from dotenv import load_dotenv
|
5 |
+
from fastapi import FastAPI
|
6 |
+
import gradio as gr
|
7 |
+
import asyncio
|
8 |
+
import threading
|
9 |
+
import time
|
10 |
|
11 |
+
# Load environment variables
|
12 |
load_dotenv()
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
# Get bot token from .env file
|
15 |
+
BOT_TOKEN = os.getenv("BOT_TOKEN")
|
|
|
|
|
|
|
16 |
|
17 |
+
# Set up FastAPI and Gradio
|
18 |
+
app = FastAPI()
|
|
|
|
|
|
|
19 |
|
20 |
+
# This will simulate the "Bot is running" message in the background
|
21 |
+
@app.get("/")
|
22 |
+
async def read_root():
|
23 |
+
return {"message": "Bot is running"}
|
24 |
|
25 |
+
# Set up Discord bot client
|
26 |
+
intents = discord.Intents.default()
|
27 |
+
intents.message_content = True
|
28 |
+
client = discord.Client(intents=intents)
|
|
|
|
|
|
|
29 |
|
30 |
+
# API URL for fetching responses from the external service
|
31 |
+
url = "https://0zyen.vercel.app/api/chat/completions"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
# Function to interact with the API
|
34 |
+
def get_response_from_api(user_input):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
payload = {
|
36 |
+
"content": user_input,
|
37 |
+
"model": "Evil"
|
38 |
}
|
|
|
|
|
39 |
response = requests.post(url, json=payload)
|
|
|
40 |
if response.status_code == 200:
|
41 |
+
api_response = response.json()
|
42 |
+
return f'prompt\nmodel:evil\n{api_response.get("generated_text", "")}'
|
43 |
+
return "Error communicating with API."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
# Async function to start the Discord bot
|
46 |
+
async def run_discord_bot():
|
47 |
+
await client.start(BOT_TOKEN)
|
48 |
|
49 |
+
# Function to keep FastAPI and Discord bot running simultaneously
|
50 |
+
def start_fastapi_server():
|
51 |
+
import uvicorn
|
52 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
53 |
|
54 |
+
# Event handler for when the bot is ready
|
55 |
+
@client.event
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
async def on_ready():
|
57 |
+
print(f"Bot logged in as {client.user}")
|
58 |
|
59 |
+
# Event handler for when the bot receives a message
|
60 |
+
@client.event
|
61 |
async def on_message(message):
|
62 |
+
if message.author == client.user:
|
|
|
63 |
return
|
64 |
|
65 |
+
# Interact with the external API
|
66 |
+
response_text = get_response_from_api(message.content)
|
67 |
+
|
68 |
+
# Send the response back to the Discord channel
|
69 |
+
await message.channel.send(response_text)
|
70 |
|
71 |
+
# Run both FastAPI server and Discord bot concurrently
|
72 |
+
if __name__ == "__main__":
|
73 |
+
# Start the FastAPI server in a separate thread
|
74 |
+
threading.Thread(target=start_fastapi_server, daemon=True).start()
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
+
# Run the Discord bot
|
77 |
+
asyncio.run(run_discord_bot())
|
|
|
|
|
|
|
|
|
|
|
|