Spaces:
Runtime error
Runtime error
KaiShin1885
commited on
Update app.py
Browse files
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
|
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 |
-
#
|
20 |
-
|
|
|
|
|
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 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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})
|