seawolf2357 commited on
Commit
2f4a3bc
ยท
verified ยท
1 Parent(s): bfdbd1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
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 image_base64
 
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
- # ํ…์ŠคํŠธ์™€ LaTeX ์ˆ˜์‹ ๋ถ„๋ฆฌ
165
  text_parts = re.split(r'(\$\$.*?\$\$|\$.*?\$)', message, flags=re.DOTALL)
166
 
167
  for part in text_parts:
168
  if part.startswith('$'):
169
- # LaTeX ์ˆ˜์‹ ์ฒ˜๋ฆฌ ๋ฐ ์ด๋ฏธ์ง€๋กœ ์ถœ๋ ฅ
170
  latex_content = part.strip('$')
171
- image_base64 = latex_to_image(latex_content)
172
- image_binary = base64.b64decode(image_base64)
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: