Spaces:
Running
Running
seawolf2357
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,7 @@ from transformers import pipeline
|
|
6 |
import asyncio
|
7 |
import subprocess
|
8 |
import re
|
9 |
-
|
10 |
# ๋ก๊น
์ค์
|
11 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
|
12 |
|
@@ -50,13 +50,16 @@ class MyClient(discord.Client):
|
|
50 |
self.is_processing = True
|
51 |
try:
|
52 |
if self.is_math_question(message.content):
|
53 |
-
|
|
|
|
|
54 |
else:
|
55 |
response = await self.generate_response(message)
|
56 |
-
|
57 |
finally:
|
58 |
self.is_processing = False
|
59 |
|
|
|
60 |
def is_message_in_specific_channel(self, message):
|
61 |
return message.channel.id == SPECIFIC_CHANNEL_ID or (
|
62 |
isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
@@ -65,15 +68,19 @@ class MyClient(discord.Client):
|
|
65 |
def is_math_question(self, content):
|
66 |
return bool(re.search(r'\b(solve|equation|calculate|math)\b', content, re.IGNORECASE))
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
#
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
77 |
async def generate_response(self, message):
|
78 |
global conversation_history
|
79 |
user_input = message.content
|
|
|
6 |
import asyncio
|
7 |
import subprocess
|
8 |
import re
|
9 |
+
import urllib.parse
|
10 |
# ๋ก๊น
์ค์
|
11 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
|
12 |
|
|
|
50 |
self.is_processing = True
|
51 |
try:
|
52 |
if self.is_math_question(message.content):
|
53 |
+
text_response, img_url = await self.handle_math_question(message.content)
|
54 |
+
await self.send_long_message(message.channel, text_response)
|
55 |
+
await self.send_long_message(message.channel, img_url) # ์ด๋ฏธ์ง URL์ ๋ณ๋์ ๋ฉ์์ง๋ก ์ ์ก
|
56 |
else:
|
57 |
response = await self.generate_response(message)
|
58 |
+
await self.send_long_message(message.channel, response)
|
59 |
finally:
|
60 |
self.is_processing = False
|
61 |
|
62 |
+
|
63 |
def is_message_in_specific_channel(self, message):
|
64 |
return message.channel.id == SPECIFIC_CHANNEL_ID or (
|
65 |
isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
|
|
68 |
def is_math_question(self, content):
|
69 |
return bool(re.search(r'\b(solve|equation|calculate|math)\b', content, re.IGNORECASE))
|
70 |
|
71 |
+
async def handle_math_question(self, question):
|
72 |
+
loop = asyncio.get_event_loop()
|
73 |
+
response = await loop.run_in_executor(None, lambda: self.math_pipe([{"role": "user", "content": question}]))
|
74 |
+
math_response = response[0]['generated_text']
|
75 |
+
|
76 |
+
# QuickLaTeX API๋ฅผ ์ด์ฉํ์ฌ LaTeX ์์์ ์ด๋ฏธ์ง๋ก ๋ณํ
|
77 |
+
encoded_formula = urllib.parse.quote_plus(math_response)
|
78 |
+
quicklatex_url = f"https://quicklatex.com/latex3.f/png?formula={encoded_formula}"
|
79 |
+
image_response = requests.get(quicklatex_url)
|
80 |
+
image_url = image_response.text.split('\n')[1] # ์๋ต์์ ์ด๋ฏธ์ง URL ์ถ์ถ
|
81 |
+
|
82 |
+
return math_response, image_url
|
83 |
+
|
84 |
async def generate_response(self, message):
|
85 |
global conversation_history
|
86 |
user_input = message.content
|