seawolf2357 commited on
Commit
c3a9389
โ€ข
1 Parent(s): 20c4f30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -31
app.py CHANGED
@@ -1,11 +1,9 @@
1
-
2
  import discord
3
  import logging
4
  import os
 
5
  import asyncio
6
- import aiohttp
7
  import subprocess
8
- from huggingface_hub import InferenceClient
9
 
10
  # ๋กœ๊น… ์„ค์ •
11
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
@@ -18,7 +16,7 @@ intents.guilds = True
18
  intents.guild_messages = True
19
 
20
  # ์ถ”๋ก  API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
21
- hf_client = InferenceClient(token=os.getenv("HF_TOKEN"))
22
 
23
  # ํŠน์ • ์ฑ„๋„ ID
24
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
@@ -33,7 +31,7 @@ class MyClient(discord.Client):
33
 
34
  async def on_ready(self):
35
  logging.info(f'{self.user}๋กœ ๋กœ๊ทธ์ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!')
36
- self.loop.create_task(start_web_server())
37
  logging.info("Web.py server has been started.")
38
 
39
  async def on_message(self, message):
@@ -75,32 +73,22 @@ async def generate_response(message):
75
  messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] + conversation_history
76
  logging.debug(f'Messages to be sent to the model: {messages}')
77
 
78
- async with aiohttp.ClientSession() as session:
79
- async with session.post(
80
- f'https://api-inference.huggingface.co/models/CohereForAI/c4ai-command-r-plus',
81
- headers={"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"},
82
- json={"inputs": {"text": user_input}}
83
- ) as resp:
84
- response_data = await resp.json()
85
- response_text = response_data.get('generated_text', '')
86
-
87
- logging.debug(f'Full model response: {response_text}')
88
-
89
- conversation_history.append({"role": "assistant", "content": response_text})
90
- return f"{user_mention}, {response_text}"
91
-
92
- async def start_web_server():
93
- # Gradio ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•  ํฌํŠธ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํฌํŠธ๋ฅผ ๋™์ ์œผ๋กœ ์„ค์ •
94
- import socket
95
- port = 7860
96
- while True:
97
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
98
- if s.connect_ex(('localhost', port)) != 0:
99
- break
100
- port += 1
101
- python_executable = 'python3' if os.name == 'posix' else 'python'
102
- subprocess.Popen([python_executable, "web.py"], env={"GRADIO_SERVER_PORT": str(port)})
103
 
104
  if __name__ == "__main__":
105
  discord_client = MyClient(intents=intents)
106
- discord_client.run(os.getenv('DISCORD_TOKEN'))
 
 
1
  import discord
2
  import logging
3
  import os
4
+ from huggingface_hub import InferenceClient
5
  import asyncio
 
6
  import subprocess
 
7
 
8
  # ๋กœ๊น… ์„ค์ •
9
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
 
16
  intents.guild_messages = True
17
 
18
  # ์ถ”๋ก  API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
19
+ hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
20
 
21
  # ํŠน์ • ์ฑ„๋„ ID
22
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
 
31
 
32
  async def on_ready(self):
33
  logging.info(f'{self.user}๋กœ ๋กœ๊ทธ์ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!')
34
+ subprocess.Popen(["python", "web.py"])
35
  logging.info("Web.py server has been started.")
36
 
37
  async def on_message(self, message):
 
73
  messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] + conversation_history
74
  logging.debug(f'Messages to be sent to the model: {messages}')
75
 
76
+ loop = asyncio.get_event_loop()
77
+ response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(
78
+ messages, max_tokens=1000, stream=True, temperature=0.7, top_p=0.85))
79
+
80
+ full_response = []
81
+ for part in response:
82
+ logging.debug(f'Part received from stream: {part}')
83
+ if part.choices and part.choices[0].delta and part.choices[0].delta.content:
84
+ full_response.append(part.choices[0].delta.content)
85
+
86
+ full_response_text = ''.join(full_response)
87
+ logging.debug(f'Full model response: {full_response_text}')
88
+
89
+ conversation_history.append({"role": "assistant", "content": full_response_text})
90
+ return f"{user_mention}, {full_response_text}"
 
 
 
 
 
 
 
 
 
 
91
 
92
  if __name__ == "__main__":
93
  discord_client = MyClient(intents=intents)
94
+ discord_client.run(os.getenv('DISCORD_TOKEN'))