Spaces:
Running
Running
seawolf2357
commited on
Commit
โข
9c071a8
1
Parent(s):
2656039
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,25 @@ import asyncio
|
|
6 |
import subprocess
|
7 |
from datasets import load_dataset
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
# ๋ก๊น
์ค์
|
10 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
|
11 |
|
@@ -16,10 +35,6 @@ intents.messages = True
|
|
16 |
intents.guilds = True
|
17 |
intents.guild_messages = True
|
18 |
|
19 |
-
# ๋ฐ์ดํฐ์
๋ก๋
|
20 |
-
data_files = ['train_0.csv', 'train_1.csv', 'train_2.csv', 'train_3.csv', 'train_4.csv', 'train_5.csv']
|
21 |
-
law_dataset = load_dataset('csv', data_files=data_files)
|
22 |
-
|
23 |
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์
|
24 |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
|
25 |
|
@@ -29,6 +44,9 @@ SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
|
29 |
# ๋ํ ํ์คํ ๋ฆฌ๋ฅผ ์ ์ฅํ ์ ์ญ ๋ณ์
|
30 |
conversation_history = []
|
31 |
|
|
|
|
|
|
|
32 |
class MyClient(discord.Client):
|
33 |
def __init__(self, *args, **kwargs):
|
34 |
super().__init__(*args, **kwargs)
|
@@ -48,7 +66,7 @@ class MyClient(discord.Client):
|
|
48 |
return
|
49 |
self.is_processing = True
|
50 |
try:
|
51 |
-
response = await
|
52 |
await message.channel.send(response)
|
53 |
finally:
|
54 |
self.is_processing = False
|
@@ -58,39 +76,38 @@ class MyClient(discord.Client):
|
|
58 |
isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
59 |
)
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
|
95 |
if __name__ == "__main__":
|
96 |
discord_client = MyClient(intents=intents)
|
|
|
6 |
import subprocess
|
7 |
from datasets import load_dataset
|
8 |
|
9 |
+
# ํ์ฌ ์์
๋๋ ํ ๋ฆฌ ์ถ๋ ฅ
|
10 |
+
print("Current Working Directory:", os.getcwd())
|
11 |
+
|
12 |
+
# ๋ฐ์ดํฐ์
ํ์ผ ์ด๋ฆ
|
13 |
+
data_file = 'train_0.csv'
|
14 |
+
|
15 |
+
# ํ์ฌ ์์
๋๋ ํ ๋ฆฌ์ ํ์ผ์ด ์๋์ง ํ์ธ
|
16 |
+
if os.path.exists(data_file):
|
17 |
+
print(f"File {data_file} exists in the current directory.")
|
18 |
+
else:
|
19 |
+
print(f"File {data_file} does not exist in the current directory.")
|
20 |
+
# ์์
๋๋ ํ ๋ฆฌ ๋ณ๊ฒฝ (ํ์ํ ๊ฒฝ์ฐ)
|
21 |
+
os.chdir('/home/user/app')
|
22 |
+
print("Changed directory to:", os.getcwd())
|
23 |
+
|
24 |
+
# ๋ฐ์ดํฐ์
๋ก๋
|
25 |
+
law_dataset = load_dataset('csv', data_files=data_file)
|
26 |
+
print("Dataset loaded successfully.")
|
27 |
+
|
28 |
# ๋ก๊น
์ค์
|
29 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
|
30 |
|
|
|
35 |
intents.guilds = True
|
36 |
intents.guild_messages = True
|
37 |
|
|
|
|
|
|
|
|
|
38 |
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์
|
39 |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
|
40 |
|
|
|
44 |
# ๋ํ ํ์คํ ๋ฆฌ๋ฅผ ์ ์ฅํ ์ ์ญ ๋ณ์
|
45 |
conversation_history = []
|
46 |
|
47 |
+
# ๋ฒ๋ฅ ๋ฐ์ดํฐ์
๋ก๋
|
48 |
+
law_dataset = load_dataset('csv', data_files='train_0.csv')
|
49 |
+
|
50 |
class MyClient(discord.Client):
|
51 |
def __init__(self, *args, **kwargs):
|
52 |
super().__init__(*args, **kwargs)
|
|
|
66 |
return
|
67 |
self.is_processing = True
|
68 |
try:
|
69 |
+
response = await generate_response(message)
|
70 |
await message.channel.send(response)
|
71 |
finally:
|
72 |
self.is_processing = False
|
|
|
76 |
isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
77 |
)
|
78 |
|
79 |
+
async def generate_response(message):
|
80 |
+
global conversation_history
|
81 |
+
user_input = message.content
|
82 |
+
user_mention = message.author.mention
|
83 |
+
system_message = f"{user_mention}, DISCORD์์ ์ฌ์ฉ์๋ค์ ์ง๋ฌธ์ ๋ตํ๋ ์ด์์คํดํธ์
๋๋ค."
|
84 |
+
|
85 |
+
# ๋ฐ์ดํฐ ๊ฒ์ ๋ฐ ์๋ต ์ค๋น
|
86 |
+
answer = search_in_dataset(user_input, law_dataset)
|
87 |
+
full_response_text = system_message + "\n\n" + answer
|
88 |
+
|
89 |
+
# ์๋ต ๋ถํ ์ ์ก
|
90 |
+
max_length = 2000
|
91 |
+
if len(full_response_text) > max_length:
|
92 |
+
# ๋๋ฌด ๊ธด ๋ฉ์์ง๋ฅผ ์ฌ๋ฌ ๋ถ๋ถ์ผ๋ก ๋๋์ด ๋ณด๋
๋๋ค.
|
93 |
+
for i in range(0, len(full_response_text), max_length):
|
94 |
+
part_response = full_response_text[i:i+max_length]
|
95 |
+
await message.channel.send(part_response)
|
96 |
+
else:
|
97 |
+
# ๋ฉ์์ง ๊ธธ์ด๊ฐ ์ ์ ํ๋ฉด ํ ๋ฒ์ ์ ์ก
|
98 |
+
await message.channel.send(full_response_text)
|
99 |
+
|
100 |
+
logging.debug(f'Full model response sent: {full_response_text}')
|
101 |
+
conversation_history.append({"role": "assistant", "content": full_response_text})
|
102 |
+
|
103 |
+
|
104 |
+
def search_in_dataset(query, dataset):
|
105 |
+
# ๊ฐ๋จํ ๊ฒ์ ๋ก์ง์ ๊ตฌํํฉ๋๋ค.
|
106 |
+
# ์ฌ๊ธฐ์์๋ ์์ ๋ก ๋จ์ํํ๊ธฐ ์ํด ์ฒซ ๋ฒ์งธ ํญ๋ชฉ์ ๋ฐํํฉ๋๋ค.
|
107 |
+
for record in dataset['train']:
|
108 |
+
if query in record['์ฌ๊ฑด๋ช
']:
|
109 |
+
return record['์ฌ๊ฑด๋ฒํธ']
|
110 |
+
return "๊ด๋ จ ๋ฒ๋ฅ ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค."
|
|
|
111 |
|
112 |
if __name__ == "__main__":
|
113 |
discord_client = MyClient(intents=intents)
|