seawolf2357 commited on
Commit
c4be42e
β€’
1 Parent(s): 13feae4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -20
app.py CHANGED
@@ -10,19 +10,20 @@ from datasets import load_dataset
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
  # λ‘œκΉ… μ„€μ •
@@ -44,9 +45,6 @@ SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
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)
@@ -68,14 +66,12 @@ class MyClient(discord.Client):
68
  self.is_processing = True
69
  try:
70
  response = await generate_response(message)
71
- # λΉ„μ–΄ μžˆλŠ” 응닡을 ν™•μΈν•˜κ³  처리
72
  if response.strip() == "":
73
  response = "μ£„μ†‘ν•©λ‹ˆλ‹€, μ œκ³΅ν•  수 μžˆλŠ” 정보가 μ—†μŠ΅λ‹ˆλ‹€."
74
  await message.channel.send(response)
75
  finally:
76
  self.is_processing = False
77
 
78
-
79
  def is_message_in_specific_channel(self, message):
80
  return message.channel.id == SPECIFIC_CHANNEL_ID or (
81
  isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
@@ -89,26 +85,22 @@ async def generate_response(message):
89
 
90
  # 데이터 검색 및 응닡 μ€€λΉ„
91
  answer = search_in_dataset(user_input, law_dataset)
92
- full_response_text = system_message + "\n\n" + answer
 
93
 
94
- # 응닡 λΆ„ν•  전솑
95
  max_length = 2000
96
  if len(full_response_text) > max_length:
97
- # λ„ˆλ¬΄ κΈ΄ λ©”μ‹œμ§€λ₯Ό μ—¬λŸ¬ λΆ€λΆ„μœΌλ‘œ λ‚˜λˆ„μ–΄ λ³΄λƒ…λ‹ˆλ‹€.
98
  for i in range(0, len(full_response_text), max_length):
99
  part_response = full_response_text[i:i+max_length]
100
  await message.channel.send(part_response)
101
  else:
102
- # λ©”μ‹œμ§€ 길이가 μ μ ˆν•˜λ©΄ ν•œ λ²ˆμ— 전솑
103
  await message.channel.send(full_response_text)
104
 
105
  logging.debug(f'Full model response sent: {full_response_text}')
106
  conversation_history.append({"role": "assistant", "content": full_response_text})
107
 
108
-
109
  def search_in_dataset(query, dataset):
110
- # κ°„λ‹¨ν•œ 검색 λ‘œμ§μ„ κ΅¬ν˜„ν•©λ‹ˆλ‹€.
111
- # μ—¬κΈ°μ—μ„œλŠ” 예제둜 λ‹¨μˆœν™”ν•˜κΈ° μœ„ν•΄ 첫 번째 ν•­λͺ©μ„ λ°˜ν™˜ν•©λ‹ˆλ‹€.
112
  for record in dataset['train']:
113
  if query in record['사건λͺ…']:
114
  return record['μ‚¬κ±΄λ²ˆν˜Έ']
 
10
  print("Current Working Directory:", os.getcwd())
11
 
12
  # 데이터셋 파일 이름
13
+ data_files = ['train_0.csv', 'train_1.csv', 'train_2.csv', 'train_3.csv', 'train_4.csv', 'train_5.csv']
14
 
15
+ # ν˜„μž¬ μž‘μ—… 디렉토리에 λͺ¨λ“  파일이 μžˆλŠ”μ§€ 확인
16
+ missing_files = [file for file in data_files if not os.path.exists(file)]
17
+ if missing_files:
18
+ print(f"Missing files: {missing_files}")
19
+ # ν•„μš”ν•œ 경우 μž‘μ—… 디렉토리 λ³€κ²½
 
20
  os.chdir('/home/user/app')
21
  print("Changed directory to:", os.getcwd())
22
+ else:
23
+ print("All files are present in the current directory.")
24
 
25
  # 데이터셋 λ‘œλ“œ
26
+ law_dataset = load_dataset('csv', data_files=data_files)
27
  print("Dataset loaded successfully.")
28
 
29
  # λ‘œκΉ… μ„€μ •
 
45
  # λŒ€ν™” νžˆμŠ€ν† λ¦¬λ₯Ό μ €μž₯ν•  μ „μ—­ λ³€μˆ˜
46
  conversation_history = []
47
 
 
 
 
48
  class MyClient(discord.Client):
49
  def __init__(self, *args, **kwargs):
50
  super().__init__(*args, **kwargs)
 
66
  self.is_processing = True
67
  try:
68
  response = await generate_response(message)
 
69
  if response.strip() == "":
70
  response = "μ£„μ†‘ν•©λ‹ˆλ‹€, μ œκ³΅ν•  수 μžˆλŠ” 정보가 μ—†μŠ΅λ‹ˆλ‹€."
71
  await message.channel.send(response)
72
  finally:
73
  self.is_processing = False
74
 
 
75
  def is_message_in_specific_channel(self, message):
76
  return message.channel.id == SPECIFIC_CHANNEL_ID or (
77
  isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
 
85
 
86
  # 데이터 검색 및 응닡 μ€€λΉ„
87
  answer = search_in_dataset(user_input, law_dataset)
88
+ if not answer:
89
+ answer = "κ΄€λ ¨ 법λ₯  정보λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."
90
 
91
+ full_response_text = system_message + "\n\n" + answer
92
  max_length = 2000
93
  if len(full_response_text) > max_length:
 
94
  for i in range(0, len(full_response_text), max_length):
95
  part_response = full_response_text[i:i+max_length]
96
  await message.channel.send(part_response)
97
  else:
 
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
  def search_in_dataset(query, dataset):
 
 
104
  for record in dataset['train']:
105
  if query in record['사건λͺ…']:
106
  return record['μ‚¬κ±΄λ²ˆν˜Έ']