Spaces:
Running
Running
seawolf2357
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -36,12 +36,9 @@ SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
|
36 |
conversation_history = []
|
37 |
|
38 |
def latex_to_image(latex_string):
|
39 |
-
# $$ ๊ธฐํธ ์ ๊ฑฐ
|
40 |
-
latex_string = latex_string.strip('$')
|
41 |
-
|
42 |
plt.figure(figsize=(10, 1))
|
43 |
plt.axis('off')
|
44 |
-
plt.text(0.5, 0.5,
|
45 |
|
46 |
buffer = BytesIO()
|
47 |
plt.savefig(buffer, format='png', bbox_inches='tight', pad_inches=0.1, transparent=True)
|
@@ -51,6 +48,8 @@ def latex_to_image(latex_string):
|
|
51 |
plt.close()
|
52 |
|
53 |
return image_base64
|
|
|
|
|
54 |
|
55 |
def process_and_convert_latex(text):
|
56 |
# ๋จ์ผ $ ๋๋ ์ด์ค $$ ๋ก ๋๋ฌ์ธ์ธ LaTeX ์์์ ์ฐพ์ต๋๋ค.
|
@@ -158,21 +157,28 @@ class MyClient(discord.Client):
|
|
158 |
return f"{user_mention}, {full_response}"
|
159 |
|
160 |
async def send_message_with_latex(self, channel, message):
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
await
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
async def send_long_message(self, channel, message):
|
178 |
if len(message) <= 2000:
|
|
|
36 |
conversation_history = []
|
37 |
|
38 |
def latex_to_image(latex_string):
|
|
|
|
|
|
|
39 |
plt.figure(figsize=(10, 1))
|
40 |
plt.axis('off')
|
41 |
+
plt.text(0.5, 0.5, latex_string, size=20, ha='center', va='center')
|
42 |
|
43 |
buffer = BytesIO()
|
44 |
plt.savefig(buffer, format='png', bbox_inches='tight', pad_inches=0.1, transparent=True)
|
|
|
48 |
plt.close()
|
49 |
|
50 |
return image_base64
|
51 |
+
|
52 |
+
|
53 |
|
54 |
def process_and_convert_latex(text):
|
55 |
# ๋จ์ผ $ ๋๋ ์ด์ค $$ ๋ก ๋๋ฌ์ธ์ธ LaTeX ์์์ ์ฐพ์ต๋๋ค.
|
|
|
157 |
return f"{user_mention}, {full_response}"
|
158 |
|
159 |
async def send_message_with_latex(self, channel, message):
|
160 |
+
try:
|
161 |
+
# ํ
์คํธ์ LaTeX ์์ ๋ถ๋ฆฌ
|
162 |
+
text_parts = re.split(r'(\$\$.*?\$\$|\$.*?\$)', message, flags=re.DOTALL)
|
163 |
+
|
164 |
+
# ํ
์คํธ ๋จผ์ ์ถ๋ ฅ
|
165 |
+
for part in text_parts:
|
166 |
+
if not part.startswith('$'):
|
167 |
+
if part.strip():
|
168 |
+
await self.send_long_message(channel, part.strip())
|
169 |
+
|
170 |
+
# LaTeX ์์ ์ฒ๋ฆฌ ๋ฐ ์ด๋ฏธ์ง๋ก ์ถ๋ ฅ
|
171 |
+
for part in text_parts:
|
172 |
+
if part.startswith('$'):
|
173 |
+
latex_content = part.strip('$')
|
174 |
+
image_base64 = latex_to_image(latex_content)
|
175 |
+
image_binary = base64.b64decode(image_base64)
|
176 |
+
await channel.send(file=discord.File(BytesIO(image_binary), 'equation.png'))
|
177 |
+
|
178 |
+
except Exception as e:
|
179 |
+
logging.error(f"Error in send_message_with_latex: {str(e)}")
|
180 |
+
await channel.send("An error occurred while processing the message.")
|
181 |
+
|
182 |
|
183 |
async def send_long_message(self, channel, message):
|
184 |
if len(message) <= 2000:
|