seawolf2357 commited on
Commit
a23f151
ยท
verified ยท
1 Parent(s): 912c2c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -40
app.py CHANGED
@@ -71,57 +71,43 @@ class MyClient(discord.Client):
71
  super().__init__(*args, **kwargs)
72
  self.is_processing = False
73
  self.math_pipe = math_pipe
74
- self.hf_client = hf_client_primary # ์ดˆ๊ธฐ ํด๋ผ์ด์–ธํŠธ ์„ค์ •
75
-
76
- async def on_ready(self):
77
- logging.info(f'{self.user}๋กœ ๋กœ๊ทธ์ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!')
78
- subprocess.Popen(["python", "web.py"])
79
- logging.info("Web.py server has been started.")
80
-
81
- async def on_message(self, message):
82
- if message.author == self.user:
83
- return
84
- if not self.is_message_in_specific_channel(message):
85
- return
86
- if self.is_processing:
87
- return
88
-
89
- self.is_processing = True
90
- try:
91
- if isinstance(message.channel, discord.TextChannel):
92
- thread = await message.channel.create_thread(name=f"์งˆ๋ฌธ: {message.author.name}", message=message)
93
- if self.is_math_question(message.content):
94
- text_response = await self.handle_math_question(message.content)
95
- await self.send_message_with_latex(thread, text_response)
96
- else:
97
- response = await self.generate_response(message)
98
- await self.send_message_with_latex(thread, response)
99
- else:
100
- logging.warning("Message is not in a TextChannel.")
101
- except Exception as e:
102
- logging.error(f"Error in on_message: {type(e).__name__}: {str(e)}")
103
- await message.channel.send("An error occurred while processing the message.")
104
- finally:
105
- self.is_processing = False
106
 
107
- def is_message_in_specific_channel(self, message):
108
- return isinstance(message.channel, discord.TextChannel) and (message.channel.id == SPECIFIC_CHANNEL_ID or (
109
- isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
110
- ))
 
 
 
 
 
111
 
112
- def is_math_question(self, content):
113
- return bool(re.search(r'\b(solve|equation|calculate|math)\b', content, re.IGNORECASE))
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
  async def handle_math_question(self, question):
116
  loop = asyncio.get_event_loop()
117
 
118
- # AI-MO/NuminaMath-7B-TIR ๋ชจ๋ธ์—๊ฒŒ ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ํ’€๋„๋ก ์š”์ฒญ
119
  math_response_future = loop.run_in_executor(None, lambda: self.math_pipe(question, max_new_tokens=2000))
120
  math_response = await math_response_future
121
  math_result = math_response[0]['generated_text']
122
 
123
  try:
124
- # Cohere ๋ชจ๋ธ์—๊ฒŒ AI-MO/NuminaMath-7B-TIR ๋ชจ๋ธ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฒˆ์—ญํ•˜๋„๋ก ์š”์ฒญ
125
  cohere_response = await self.retry_request(lambda: self.hf_client.chat_completion(
126
  [{"role": "system", "content": "๋‹ค์Œ ํ…์ŠคํŠธ๋ฅผ ํ•œ๊ธ€๋กœ ๋ฒˆ์—ญํ•˜์‹ญ์‹œ์˜ค: "}, {"role": "user", "content": math_result}], max_tokens=1000))
127
 
@@ -135,6 +121,9 @@ class MyClient(discord.Client):
135
 
136
  return combined_response
137
 
 
 
 
138
  async def generate_response(self, message):
139
  global conversation_history
140
  user_input = message.content
 
71
  super().__init__(*args, **kwargs)
72
  self.is_processing = False
73
  self.math_pipe = math_pipe
74
+ self.current_client = "primary"
75
+ self.hf_client = hf_client_primary
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
+ def switch_client(self):
78
+ if self.current_client == "primary":
79
+ self.hf_client = hf_client_secondary
80
+ self.current_client = "secondary"
81
+ logging.info("Switched to secondary client (CohereForAI/aya-23-35B).")
82
+ else:
83
+ self.hf_client = hf_client_primary
84
+ self.current_client = "primary"
85
+ logging.info("Switched back to primary client (CohereForAI/c4ai-command-r-plus).")
86
 
87
+ async def retry_request(self, func, retries=5, delay=2):
88
+ for i in range(retries):
89
+ try:
90
+ return await func()
91
+ except Exception as e:
92
+ logging.error(f"Attempt {i+1}/{retries}: Error encountered: {type(e).__name__}: {str(e)}")
93
+ if isinstance(e, HTTPError) and getattr(e.response, 'status_code', None) == 503:
94
+ logging.warning(f"503 error encountered. Switching client and retrying in {delay} seconds...")
95
+ self.switch_client()
96
+ elif i < retries - 1:
97
+ logging.warning(f"Error occurred. Retrying in {delay} seconds...")
98
+ await asyncio.sleep(delay)
99
+
100
+ logging.error(f"All {retries} attempts failed.")
101
+ raise Exception("Max retries reached")
102
 
103
  async def handle_math_question(self, question):
104
  loop = asyncio.get_event_loop()
105
 
 
106
  math_response_future = loop.run_in_executor(None, lambda: self.math_pipe(question, max_new_tokens=2000))
107
  math_response = await math_response_future
108
  math_result = math_response[0]['generated_text']
109
 
110
  try:
 
111
  cohere_response = await self.retry_request(lambda: self.hf_client.chat_completion(
112
  [{"role": "system", "content": "๋‹ค์Œ ํ…์ŠคํŠธ๋ฅผ ํ•œ๊ธ€๋กœ ๋ฒˆ์—ญํ•˜์‹ญ์‹œ์˜ค: "}, {"role": "user", "content": math_result}], max_tokens=1000))
113
 
 
121
 
122
  return combined_response
123
 
124
+
125
+
126
+
127
  async def generate_response(self, message):
128
  global conversation_history
129
  user_input = message.content