MINEOGO commited on
Commit
1f1ec90
·
verified ·
1 Parent(s): 01b645f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -48
app.py CHANGED
@@ -1,64 +1,79 @@
1
- import gradio as gr
 
 
 
 
2
  from huggingface_hub import InferenceClient
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
 
 
27
 
28
- response = ""
 
 
 
 
29
 
30
- for message in client.chat_completion(
 
31
  messages,
32
- max_tokens=max_tokens,
33
  stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
  ):
37
- token = message.choices[0].delta.content
 
 
38
 
39
- response += token
40
- yield response
 
 
 
 
 
 
41
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
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
- if __name__ == "__main__":
64
- demo.launch()
 
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)