Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -89,7 +89,6 @@ class MyClient(discord.Client):
|
|
| 89 |
self.is_processing = True
|
| 90 |
try:
|
| 91 |
if isinstance(message.channel, discord.TextChannel):
|
| 92 |
-
# ์๋ก์ด ์ค๋ ๋ ์์ฑ
|
| 93 |
thread = await message.channel.create_thread(name=f"์ง๋ฌธ: {message.author.name}", message=message)
|
| 94 |
if self.is_math_question(message.content):
|
| 95 |
text_response = await self.handle_math_question(message.content)
|
|
@@ -99,6 +98,9 @@ class MyClient(discord.Client):
|
|
| 99 |
await self.send_message_with_latex(thread, response)
|
| 100 |
else:
|
| 101 |
logging.warning("Message is not in a TextChannel.")
|
|
|
|
|
|
|
|
|
|
| 102 |
finally:
|
| 103 |
self.is_processing = False
|
| 104 |
|
|
@@ -127,8 +129,8 @@ class MyClient(discord.Client):
|
|
| 127 |
|
| 128 |
combined_response = f"์ํ ์ ์๋ ๋ต๋ณ: ```{cohere_result}```"
|
| 129 |
|
| 130 |
-
except
|
| 131 |
-
logging.error(f"
|
| 132 |
combined_response = "An error occurred while processing the request."
|
| 133 |
|
| 134 |
return combined_response
|
|
@@ -154,8 +156,8 @@ class MyClient(discord.Client):
|
|
| 154 |
messages, max_tokens=1000, stream=True, temperature=0.7, top_p=0.85))
|
| 155 |
full_response = ''.join([part.choices[0].delta.content for part in response if part.choices and part.choices[0].delta and part.choices[0].delta.content])
|
| 156 |
conversation_history.append({"role": "assistant", "content": full_response})
|
| 157 |
-
except
|
| 158 |
-
logging.error(f"
|
| 159 |
full_response = "An error occurred while generating the response."
|
| 160 |
|
| 161 |
return f"{user_mention}, {full_response}"
|
|
@@ -192,20 +194,24 @@ class MyClient(discord.Client):
|
|
| 192 |
def switch_client(self):
|
| 193 |
if self.hf_client == hf_client_primary:
|
| 194 |
self.hf_client = hf_client_secondary
|
| 195 |
-
logging.info("Switched to secondary client.")
|
| 196 |
else:
|
| 197 |
self.hf_client = hf_client_primary
|
| 198 |
-
logging.info("Switched back to primary client.")
|
| 199 |
|
| 200 |
async def retry_request(self, func, retries=5, delay=2):
|
| 201 |
for i in range(retries):
|
| 202 |
try:
|
| 203 |
return await func()
|
| 204 |
-
except
|
| 205 |
-
|
|
|
|
| 206 |
logging.warning(f"503 error encountered. Retrying in {delay} seconds...")
|
| 207 |
self.switch_client() # ํด๋ผ์ด์ธํธ ์ ํ
|
| 208 |
await asyncio.sleep(delay)
|
|
|
|
|
|
|
|
|
|
| 209 |
else:
|
| 210 |
raise
|
| 211 |
|
|
|
|
| 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)
|
|
|
|
| 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 |
|
|
|
|
| 129 |
|
| 130 |
combined_response = f"์ํ ์ ์๋ ๋ต๋ณ: ```{cohere_result}```"
|
| 131 |
|
| 132 |
+
except Exception as e:
|
| 133 |
+
logging.error(f"Error in handle_math_question: {type(e).__name__}: {str(e)}")
|
| 134 |
combined_response = "An error occurred while processing the request."
|
| 135 |
|
| 136 |
return combined_response
|
|
|
|
| 156 |
messages, max_tokens=1000, stream=True, temperature=0.7, top_p=0.85))
|
| 157 |
full_response = ''.join([part.choices[0].delta.content for part in response if part.choices and part.choices[0].delta and part.choices[0].delta.content])
|
| 158 |
conversation_history.append({"role": "assistant", "content": full_response})
|
| 159 |
+
except Exception as e:
|
| 160 |
+
logging.error(f"Error in generate_response: {type(e).__name__}: {str(e)}")
|
| 161 |
full_response = "An error occurred while generating the response."
|
| 162 |
|
| 163 |
return f"{user_mention}, {full_response}"
|
|
|
|
| 194 |
def switch_client(self):
|
| 195 |
if self.hf_client == hf_client_primary:
|
| 196 |
self.hf_client = hf_client_secondary
|
| 197 |
+
logging.info("Switched to secondary client (CohereForAI/aya-23-35B).")
|
| 198 |
else:
|
| 199 |
self.hf_client = hf_client_primary
|
| 200 |
+
logging.info("Switched back to primary client (CohereForAI/c4ai-command-r-plus).")
|
| 201 |
|
| 202 |
async def retry_request(self, func, retries=5, delay=2):
|
| 203 |
for i in range(retries):
|
| 204 |
try:
|
| 205 |
return await func()
|
| 206 |
+
except Exception as e:
|
| 207 |
+
logging.error(f"Error encountered: {type(e).__name__}: {str(e)}")
|
| 208 |
+
if isinstance(e, HTTPError) and e.response.status_code == 503:
|
| 209 |
logging.warning(f"503 error encountered. Retrying in {delay} seconds...")
|
| 210 |
self.switch_client() # ํด๋ผ์ด์ธํธ ์ ํ
|
| 211 |
await asyncio.sleep(delay)
|
| 212 |
+
elif i < retries - 1:
|
| 213 |
+
logging.warning(f"Error occurred. Retrying in {delay} seconds...")
|
| 214 |
+
await asyncio.sleep(delay)
|
| 215 |
else:
|
| 216 |
raise
|
| 217 |
|