Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,79 @@
|
|
1 |
-
import
|
|
|
|
|
|
|
|
|
2 |
from huggingface_hub import InferenceClient
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
|
|
31 |
messages,
|
32 |
-
max_tokens=
|
33 |
stream=True,
|
34 |
-
temperature=
|
35 |
-
top_p=
|
36 |
):
|
37 |
-
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
|
|
1 |
+
import discord
|
2 |
+
import asyncio
|
3 |
+
import os
|
4 |
+
import json
|
5 |
+
from dotenv import load_dotenv
|
6 |
from huggingface_hub import InferenceClient
|
7 |
+
from datetime import datetime
|
8 |
|
9 |
+
# Load token from .env
|
10 |
+
load_dotenv()
|
11 |
+
TOKEN = os.getenv("BOT_TOKEN")
|
|
|
12 |
|
13 |
+
# Hugging Face client setup
|
14 |
+
client_ai = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
15 |
|
16 |
+
# Discord client setup
|
17 |
+
intents = discord.Intents.default()
|
18 |
+
intents.messages = True
|
19 |
+
intents.message_content = True
|
20 |
+
bot = discord.Client(intents=intents)
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
# Settings for AI generation
|
23 |
+
SYSTEM_PROMPT = "You are a helpful and friendly chatbot."
|
24 |
+
MAX_TOKENS = 512
|
25 |
+
TEMPERATURE = 0.7
|
26 |
+
TOP_P = 0.95
|
27 |
|
28 |
+
# Create data folder if it doesn't exist
|
29 |
+
os.makedirs("data", exist_ok=True)
|
30 |
+
CONVERSATION_LOG = "data/conversations.jsonl"
|
31 |
|
32 |
+
async def get_ai_response(message_content):
|
33 |
+
messages = [
|
34 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
35 |
+
{"role": "user", "content": message_content}
|
36 |
+
]
|
37 |
|
38 |
+
full_response = ""
|
39 |
+
for part in client_ai.chat_completion(
|
40 |
messages,
|
41 |
+
max_tokens=MAX_TOKENS,
|
42 |
stream=True,
|
43 |
+
temperature=TEMPERATURE,
|
44 |
+
top_p=TOP_P,
|
45 |
):
|
46 |
+
if part.choices[0].delta.content:
|
47 |
+
full_response += part.choices[0].delta.content
|
48 |
+
return full_response
|
49 |
|
50 |
+
def save_conversation(user_message, bot_response):
|
51 |
+
log_entry = {
|
52 |
+
"timestamp": datetime.utcnow().isoformat(),
|
53 |
+
"user_message": user_message,
|
54 |
+
"bot_response": bot_response
|
55 |
+
}
|
56 |
+
with open(CONVERSATION_LOG, "a", encoding="utf-8") as f:
|
57 |
+
f.write(json.dumps(log_entry) + "\n")
|
58 |
|
59 |
+
@bot.event
|
60 |
+
async def on_ready():
|
61 |
+
print(f"Bot is running in background as {bot.user}")
|
62 |
|
63 |
+
@bot.event
|
64 |
+
async def on_message(message):
|
65 |
+
# Ignore bot's own messages
|
66 |
+
if message.author == bot.user:
|
67 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
try:
|
70 |
+
user_input = message.content
|
71 |
+
response = await get_ai_response(user_input)
|
72 |
+
if response.strip() != "":
|
73 |
+
await message.channel.send(response[:2000]) # Discord limit is 2000 chars
|
74 |
+
save_conversation(user_input, response)
|
75 |
+
except Exception as e:
|
76 |
+
print(f"Error responding to message: {e}")
|
77 |
|
78 |
+
# Run the bot
|
79 |
+
bot.run(TOKEN)
|