KaiShin1885 commited on
Commit
25d7427
ยท
verified ยท
1 Parent(s): 7152cb8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -19
app.py CHANGED
@@ -1,10 +1,8 @@
1
-
2
  import discord
3
  import logging
4
  import os
5
- from huggingface_hub import InferenceClient
6
  import asyncio
7
- import subprocess
8
 
9
  # ๋กœ๊น… ์„ค์ •
10
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
@@ -16,8 +14,10 @@ intents.messages = True
16
  intents.guilds = True
17
  intents.guild_messages = True
18
 
19
- # ์ถ”๋ก  API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
20
- hf_client = InferenceClient("CohereForAI/aya-23-8B", token=os.getenv("HF_TOKEN"))
 
 
21
 
22
  # ํŠน์ • ์ฑ„๋„ ID
23
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
@@ -45,19 +45,16 @@ class MyClient(discord.Client):
45
  self.is_processing = False
46
 
47
  def is_message_in_specific_channel(self, message):
48
- # ๋ฉ”์‹œ์ง€๊ฐ€ ์ง€์ •๋œ ์ฑ„๋„์ด๊ฑฐ๋‚˜, ํ•ด๋‹น ์ฑ„๋„์˜ ์“ฐ๋ ˆ๋“œ์ธ ๊ฒฝ์šฐ True ๋ฐ˜ํ™˜
49
  return message.channel.id == SPECIFIC_CHANNEL_ID or (
50
  isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
51
  )
52
 
53
-
54
  async def generate_response(message):
55
- global conversation_history # ์ „์—ญ ๋ณ€์ˆ˜ ์‚ฌ์šฉ์„ ๋ช…์‹œ
56
  user_input = message.content
57
  user_mention = message.author.mention
58
  system_message = f"{user_mention}, DISCORD์—์„œ ์‚ฌ์šฉ์ž๋“ค์˜ ์งˆ๋ฌธ์— ๋‹ตํ•˜๋Š” ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค."
59
  system_prefix = """
60
-
61
  ๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ•˜์‹ญ์‹œ์˜ค.
62
  ๋‹น์‹ ์˜ ์ด๋ฆ„์€ ๋ผ์ด๋ด ์‡ผ๊ตฐ์ž…๋‹ˆ๋‹ค.
63
  ๋‹น์‹ ์€ ์—ฌ์„ฑ์ž…๋‹ˆ๋‹ค.
@@ -185,17 +182,18 @@ async def generate_response(message):
185
  messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] + conversation_history
186
  logging.debug(f'Messages to be sent to the model: {messages}')
187
 
188
- loop = asyncio.get_event_loop()
189
- response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(
190
- messages, max_tokens=1000, stream=True, temperature=0.7, top_p=0.85))
191
-
192
- full_response = []
193
- for part in response:
194
- logging.debug(f'Part received from stream: {part}')
195
- if part.choices and part.choices[0].delta and part.choices[0].delta.content:
196
- full_response.append(part.choices[0].delta.content)
197
 
198
- full_response_text = ''.join(full_response)
 
 
 
 
 
 
 
 
 
199
  logging.debug(f'Full model response: {full_response_text}')
200
 
201
  conversation_history.append({"role": "assistant", "content": full_response_text})
 
 
1
  import discord
2
  import logging
3
  import os
 
4
  import asyncio
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM
6
 
7
  # ๋กœ๊น… ์„ค์ •
8
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
 
14
  intents.guilds = True
15
  intents.guild_messages = True
16
 
17
+ # ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ € ์„ค์ •
18
+ model_id = "CohereForAI/aya-23-8B"
19
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
20
+ model = AutoModelForCausalLM.from_pretrained(model_id)
21
 
22
  # ํŠน์ • ์ฑ„๋„ ID
23
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
 
45
  self.is_processing = False
46
 
47
  def is_message_in_specific_channel(self, message):
 
48
  return message.channel.id == SPECIFIC_CHANNEL_ID or (
49
  isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
50
  )
51
 
 
52
  async def generate_response(message):
53
+ global conversation_history
54
  user_input = message.content
55
  user_mention = message.author.mention
56
  system_message = f"{user_mention}, DISCORD์—์„œ ์‚ฌ์šฉ์ž๋“ค์˜ ์งˆ๋ฌธ์— ๋‹ตํ•˜๋Š” ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค."
57
  system_prefix = """
 
58
  ๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ•˜์‹ญ์‹œ์˜ค.
59
  ๋‹น์‹ ์˜ ์ด๋ฆ„์€ ๋ผ์ด๋ด ์‡ผ๊ตฐ์ž…๋‹ˆ๋‹ค.
60
  ๋‹น์‹ ์€ ์—ฌ์„ฑ์ž…๋‹ˆ๋‹ค.
 
182
  messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] + conversation_history
183
  logging.debug(f'Messages to be sent to the model: {messages}')
184
 
185
+ input_ids = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")
 
 
 
 
 
 
 
 
186
 
187
+ loop = asyncio.get_event_loop()
188
+ gen_tokens = await loop.run_in_executor(None, lambda: model.generate(
189
+ input_ids,
190
+ max_new_tokens=1000,
191
+ do_sample=True,
192
+ temperature=0.7,
193
+ top_p=0.85
194
+ ))
195
+
196
+ full_response_text = tokenizer.decode(gen_tokens[0])
197
  logging.debug(f'Full model response: {full_response_text}')
198
 
199
  conversation_history.append({"role": "assistant", "content": full_response_text})