Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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.
|
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
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
-
def
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|