Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -35,19 +35,20 @@ SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
|
35 |
# ๋ํ ํ์คํ ๋ฆฌ๋ฅผ ์ ์ฅํ ์ ์ญ ๋ณ์
|
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', color='white')
|
42 |
|
43 |
buffer = BytesIO()
|
44 |
plt.savefig(buffer, format='png', bbox_inches='tight', pad_inches=0.1, transparent=True, facecolor='black')
|
45 |
buffer.seek(0)
|
46 |
|
47 |
-
image_base64 = base64.b64encode(buffer.getvalue()).decode()
|
48 |
plt.close()
|
49 |
|
50 |
-
return
|
|
|
51 |
|
52 |
def process_and_convert_latex(text):
|
53 |
# ๋จ์ผ $ ๋๋ ์ด์ค $$ ๋ก ๋๋ฌ์ธ์ธ LaTeX ์์์ ์ฐพ์ต๋๋ค.
|
@@ -161,24 +162,26 @@ class MyClient(discord.Client):
|
|
161 |
|
162 |
async def send_message_with_latex(self, channel, message):
|
163 |
try:
|
164 |
-
|
165 |
text_parts = re.split(r'(\$\$.*?\$\$|\$.*?\$)', message, flags=re.DOTALL)
|
166 |
|
167 |
for part in text_parts:
|
168 |
if part.startswith('$'):
|
169 |
-
|
170 |
latex_content = part.strip('$')
|
171 |
-
|
172 |
-
|
173 |
-
await channel.send(file=discord.File(BytesIO(image_binary), 'equation.png'))
|
174 |
else:
|
175 |
-
|
176 |
if part.strip():
|
177 |
await self.send_long_message(channel, part.strip())
|
178 |
|
179 |
except Exception as e:
|
180 |
logging.error(f"Error in send_message_with_latex: {str(e)}")
|
181 |
await channel.send("An error occurred while processing the message.")
|
|
|
|
|
|
|
182 |
|
183 |
async def send_long_message(self, channel, message):
|
184 |
if len(message) <= 2000:
|
|
|
35 |
# ๋ํ ํ์คํ ๋ฆฌ๋ฅผ ์ ์ฅํ ์ ์ญ ๋ณ์
|
36 |
conversation_history = []
|
37 |
|
38 |
+
|
39 |
def latex_to_image(latex_string):
|
40 |
plt.figure(figsize=(10, 1))
|
41 |
plt.axis('off')
|
42 |
+
plt.text(0.5, 0.5, f'${latex_string}$', size=20, ha='center', va='center', color='white')
|
43 |
|
44 |
buffer = BytesIO()
|
45 |
plt.savefig(buffer, format='png', bbox_inches='tight', pad_inches=0.1, transparent=True, facecolor='black')
|
46 |
buffer.seek(0)
|
47 |
|
|
|
48 |
plt.close()
|
49 |
|
50 |
+
return buffer
|
51 |
+
|
52 |
|
53 |
def process_and_convert_latex(text):
|
54 |
# ๋จ์ผ $ ๋๋ ์ด์ค $$ ๋ก ๋๋ฌ์ธ์ธ LaTeX ์์์ ์ฐพ์ต๋๋ค.
|
|
|
162 |
|
163 |
async def send_message_with_latex(self, channel, message):
|
164 |
try:
|
165 |
+
# ํ
์คํธ์ LaTeX ์์ ๋ถ๋ฆฌ
|
166 |
text_parts = re.split(r'(\$\$.*?\$\$|\$.*?\$)', message, flags=re.DOTALL)
|
167 |
|
168 |
for part in text_parts:
|
169 |
if part.startswith('$'):
|
170 |
+
# LaTeX ์์ ์ฒ๋ฆฌ ๋ฐ ์ด๋ฏธ์ง๋ก ์ถ๋ ฅ
|
171 |
latex_content = part.strip('$')
|
172 |
+
image_buffer = latex_to_image(latex_content)
|
173 |
+
await channel.send(file=discord.File(fp=image_buffer, filename='equation.png'))
|
|
|
174 |
else:
|
175 |
+
# ํ
์คํธ ์ถ๋ ฅ
|
176 |
if part.strip():
|
177 |
await self.send_long_message(channel, part.strip())
|
178 |
|
179 |
except Exception as e:
|
180 |
logging.error(f"Error in send_message_with_latex: {str(e)}")
|
181 |
await channel.send("An error occurred while processing the message.")
|
182 |
+
|
183 |
+
|
184 |
+
|
185 |
|
186 |
async def send_long_message(self, channel, message):
|
187 |
if len(message) <= 2000:
|