seawolf2357 commited on
Commit
669bbdf
ยท
verified ยท
1 Parent(s): 53c5654

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -19
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, f'${latex_string}$', size=20, ha='center', va='center')
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
- try:
162
- processed_message = process_and_convert_latex(message)
163
- parts = processed_message.split('<latex_image:')
164
-
165
- for part in parts:
166
- if part.startswith('data:image'):
167
- image_data = part.split('>')[0]
168
- image_binary = base64.b64decode(image_data)
169
- await channel.send(file=discord.File(BytesIO(image_binary), 'equation.png'))
170
- else:
171
- if part.strip():
172
- await self.send_long_message(channel, part)
173
- except Exception as e:
174
- logging.error(f"Error in send_message_with_latex: {str(e)}")
175
- await channel.send("An error occurred while processing the message.")
 
 
 
 
 
 
 
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: