Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,8 +24,7 @@ intents.guilds = True
|
|
| 24 |
intents.guild_messages = True
|
| 25 |
|
| 26 |
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์
|
| 27 |
-
|
| 28 |
-
hf_client_primary = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
|
| 29 |
|
| 30 |
# ์ํ ์ ๋ฌธ LLM ํ์ดํ๋ผ์ธ ์ค์
|
| 31 |
math_pipe = pipeline("text-generation", model="AI-MO/NuminaMath-7B-TIR")
|
|
@@ -71,59 +70,65 @@ class MyClient(discord.Client):
|
|
| 71 |
super().__init__(*args, **kwargs)
|
| 72 |
self.is_processing = False
|
| 73 |
self.math_pipe = math_pipe
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 112 |
[{"role": "system", "content": "๋ค์ ํ
์คํธ๋ฅผ ํ๊ธ๋ก ๋ฒ์ญํ์ญ์์ค: "}, {"role": "user", "content": math_result}], max_tokens=1000))
|
| 113 |
|
|
|
|
| 114 |
cohere_result = ''.join([part.choices[0].delta.content for part in cohere_response if part.choices and part.choices[0].delta and part.choices[0].delta.content])
|
| 115 |
|
| 116 |
combined_response = f"์ํ ์ ์๋ ๋ต๋ณ: ```{cohere_result}```"
|
| 117 |
|
| 118 |
-
except
|
| 119 |
-
logging.error(f"
|
| 120 |
combined_response = "An error occurred while processing the request."
|
| 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
|
|
@@ -141,12 +146,12 @@ class MyClient(discord.Client):
|
|
| 141 |
messages = [{"role": "system", "content": f"{system_prefix}"}] + conversation_history
|
| 142 |
|
| 143 |
try:
|
| 144 |
-
response = await
|
| 145 |
messages, max_tokens=1000, stream=True, temperature=0.7, top_p=0.85))
|
| 146 |
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])
|
| 147 |
conversation_history.append({"role": "assistant", "content": full_response})
|
| 148 |
-
except
|
| 149 |
-
logging.error(f"
|
| 150 |
full_response = "An error occurred while generating the response."
|
| 151 |
|
| 152 |
return f"{user_mention}, {full_response}"
|
|
@@ -180,30 +185,6 @@ class MyClient(discord.Client):
|
|
| 180 |
for part in parts:
|
| 181 |
await channel.send(part)
|
| 182 |
|
| 183 |
-
def switch_client(self):
|
| 184 |
-
if self.hf_client == hf_client_primary:
|
| 185 |
-
self.hf_client = hf_client_secondary
|
| 186 |
-
logging.info("Switched to secondary client (CohereForAI/aya-23-35B).")
|
| 187 |
-
else:
|
| 188 |
-
self.hf_client = hf_client_primary
|
| 189 |
-
logging.info("Switched back to primary client (CohereForAI/c4ai-command-r-plus).")
|
| 190 |
-
|
| 191 |
-
async def retry_request(self, func, retries=5, delay=2):
|
| 192 |
-
for i in range(retries):
|
| 193 |
-
try:
|
| 194 |
-
return await func()
|
| 195 |
-
except Exception as e:
|
| 196 |
-
logging.error(f"Error encountered: {type(e).__name__}: {str(e)}")
|
| 197 |
-
if isinstance(e, HTTPError) and e.response.status_code == 503:
|
| 198 |
-
logging.warning(f"503 error encountered. Retrying in {delay} seconds...")
|
| 199 |
-
self.switch_client() # ํด๋ผ์ด์ธํธ ์ ํ
|
| 200 |
-
await asyncio.sleep(delay)
|
| 201 |
-
elif i < retries - 1:
|
| 202 |
-
logging.warning(f"Error occurred. Retrying in {delay} seconds...")
|
| 203 |
-
await asyncio.sleep(delay)
|
| 204 |
-
else:
|
| 205 |
-
raise
|
| 206 |
-
|
| 207 |
if __name__ == "__main__":
|
| 208 |
discord_client = MyClient(intents=intents)
|
| 209 |
-
discord_client.run(os.getenv('DISCORD_TOKEN'))
|
|
|
|
| 24 |
intents.guild_messages = True
|
| 25 |
|
| 26 |
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์
|
| 27 |
+
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
|
|
|
|
| 28 |
|
| 29 |
# ์ํ ์ ๋ฌธ LLM ํ์ดํ๋ผ์ธ ์ค์
|
| 30 |
math_pipe = pipeline("text-generation", model="AI-MO/NuminaMath-7B-TIR")
|
|
|
|
| 70 |
super().__init__(*args, **kwargs)
|
| 71 |
self.is_processing = False
|
| 72 |
self.math_pipe = math_pipe
|
| 73 |
+
|
| 74 |
+
async def on_ready(self):
|
| 75 |
+
logging.info(f'{self.user}๋ก ๋ก๊ทธ์ธ๋์์ต๋๋ค!')
|
| 76 |
+
subprocess.Popen(["python", "web.py"])
|
| 77 |
+
logging.info("Web.py server has been started.")
|
| 78 |
+
|
| 79 |
+
async def on_message(self, message):
|
| 80 |
+
if message.author == self.user:
|
| 81 |
+
return
|
| 82 |
+
if not self.is_message_in_specific_channel(message):
|
| 83 |
+
return
|
| 84 |
+
if self.is_processing:
|
| 85 |
+
return
|
| 86 |
+
|
| 87 |
+
self.is_processing = True
|
| 88 |
+
try:
|
| 89 |
+
# ์๋ก์ด ์ค๋ ๋ ์์ฑ
|
| 90 |
+
thread = await message.channel.create_thread(name=f"์ง๋ฌธ: {message.author.name}", message=message)
|
| 91 |
+
if self.is_math_question(message.content):
|
| 92 |
+
text_response = await self.handle_math_question(message.content)
|
| 93 |
+
await self.send_message_with_latex(thread, text_response)
|
| 94 |
+
else:
|
| 95 |
+
response = await self.generate_response(message)
|
| 96 |
+
await self.send_message_with_latex(thread, response)
|
| 97 |
+
finally:
|
| 98 |
+
self.is_processing = False
|
| 99 |
+
|
| 100 |
+
def is_message_in_specific_channel(self, message):
|
| 101 |
+
return message.channel.id == SPECIFIC_CHANNEL_ID or (
|
| 102 |
+
isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
def is_math_question(self, content):
|
| 106 |
+
return bool(re.search(r'\b(solve|equation|calculate|math)\b', content, re.IGNORECASE))
|
| 107 |
|
| 108 |
async def handle_math_question(self, question):
|
| 109 |
loop = asyncio.get_event_loop()
|
| 110 |
|
| 111 |
+
# AI-MO/NuminaMath-7B-TIR ๋ชจ๋ธ์๊ฒ ์ํ ๋ฌธ์ ๋ฅผ ํ๋๋ก ์์ฒญ
|
| 112 |
math_response_future = loop.run_in_executor(None, lambda: self.math_pipe(question, max_new_tokens=2000))
|
| 113 |
math_response = await math_response_future
|
| 114 |
math_result = math_response[0]['generated_text']
|
| 115 |
|
| 116 |
try:
|
| 117 |
+
# Cohere ๋ชจ๋ธ์๊ฒ AI-MO/NuminaMath-7B-TIR ๋ชจ๋ธ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฒ์ญํ๋๋ก ์์ฒญ
|
| 118 |
+
cohere_response_future = loop.run_in_executor(None, lambda: hf_client.chat_completion(
|
| 119 |
[{"role": "system", "content": "๋ค์ ํ
์คํธ๋ฅผ ํ๊ธ๋ก ๋ฒ์ญํ์ญ์์ค: "}, {"role": "user", "content": math_result}], max_tokens=1000))
|
| 120 |
|
| 121 |
+
cohere_response = await cohere_response_future
|
| 122 |
cohere_result = ''.join([part.choices[0].delta.content for part in cohere_response if part.choices and part.choices[0].delta and part.choices[0].delta.content])
|
| 123 |
|
| 124 |
combined_response = f"์ํ ์ ์๋ ๋ต๋ณ: ```{cohere_result}```"
|
| 125 |
|
| 126 |
+
except HTTPError as e:
|
| 127 |
+
logging.error(f"Hugging Face API error: {e}")
|
| 128 |
combined_response = "An error occurred while processing the request."
|
| 129 |
|
| 130 |
return combined_response
|
| 131 |
|
|
|
|
|
|
|
|
|
|
| 132 |
async def generate_response(self, message):
|
| 133 |
global conversation_history
|
| 134 |
user_input = message.content
|
|
|
|
| 146 |
messages = [{"role": "system", "content": f"{system_prefix}"}] + conversation_history
|
| 147 |
|
| 148 |
try:
|
| 149 |
+
response = await asyncio.get_event_loop().run_in_executor(None, lambda: hf_client.chat_completion(
|
| 150 |
messages, max_tokens=1000, stream=True, temperature=0.7, top_p=0.85))
|
| 151 |
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])
|
| 152 |
conversation_history.append({"role": "assistant", "content": full_response})
|
| 153 |
+
except HTTPError as e:
|
| 154 |
+
logging.error(f"Hugging Face API error: {e}")
|
| 155 |
full_response = "An error occurred while generating the response."
|
| 156 |
|
| 157 |
return f"{user_mention}, {full_response}"
|
|
|
|
| 185 |
for part in parts:
|
| 186 |
await channel.send(part)
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
if __name__ == "__main__":
|
| 189 |
discord_client = MyClient(intents=intents)
|
| 190 |
+
discord_client.run(os.getenv('DISCORD_TOKEN'))
|