Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,18 +38,16 @@ conversation_history = []
|
|
| 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)
|
| 45 |
buffer.seek(0)
|
| 46 |
|
| 47 |
image_base64 = base64.b64encode(buffer.getvalue()).decode()
|
| 48 |
plt.close()
|
| 49 |
|
| 50 |
return image_base64
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
def process_and_convert_latex(text):
|
| 55 |
# ๋จ์ผ $ ๋๋ ์ด์ค $$ ๋ก ๋๋ฌ์ธ์ธ LaTeX ์์์ ์ฐพ์ต๋๋ค.
|
|
@@ -161,26 +159,22 @@ class MyClient(discord.Client):
|
|
| 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 |
-
|
| 184 |
async def send_long_message(self, channel, message):
|
| 185 |
if len(message) <= 2000:
|
| 186 |
await channel.send(message)
|
|
|
|
| 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 image_base64
|
|
|
|
|
|
|
| 51 |
|
| 52 |
def process_and_convert_latex(text):
|
| 53 |
# ๋จ์ผ $ ๋๋ ์ด์ค $$ ๋ก ๋๋ฌ์ธ์ธ LaTeX ์์์ ์ฐพ์ต๋๋ค.
|
|
|
|
| 159 |
# ํ
์คํธ์ LaTeX ์์ ๋ถ๋ฆฌ
|
| 160 |
text_parts = re.split(r'(\$\$.*?\$\$|\$.*?\$)', message, flags=re.DOTALL)
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
for part in text_parts:
|
| 163 |
if part.startswith('$'):
|
| 164 |
+
# LaTeX ์์ ์ฒ๋ฆฌ ๋ฐ ์ด๋ฏธ์ง๋ก ์ถ๋ ฅ
|
| 165 |
latex_content = part.strip('$')
|
| 166 |
image_base64 = latex_to_image(latex_content)
|
| 167 |
image_binary = base64.b64decode(image_base64)
|
| 168 |
await channel.send(file=discord.File(BytesIO(image_binary), 'equation.png'))
|
| 169 |
+
else:
|
| 170 |
+
# ํ
์คํธ ์ถ๋ ฅ
|
| 171 |
+
if part.strip():
|
| 172 |
+
await self.send_long_message(channel, part.strip())
|
| 173 |
|
| 174 |
except Exception as e:
|
| 175 |
logging.error(f"Error in send_message_with_latex: {str(e)}")
|
| 176 |
await channel.send("An error occurred while processing the message.")
|
| 177 |
+
|
|
|
|
|
|
|
| 178 |
async def send_long_message(self, channel, message):
|
| 179 |
if len(message) <= 2000:
|
| 180 |
await channel.send(message)
|