seawolf2357 commited on
Commit
7145368
ยท
verified ยท
1 Parent(s): e95d1dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -6,7 +6,7 @@ from transformers import pipeline
6
  import asyncio
7
  import subprocess
8
  import re
9
-
10
  # ๋กœ๊น… ์„ค์ •
11
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
12
 
@@ -50,13 +50,16 @@ class MyClient(discord.Client):
50
  self.is_processing = True
51
  try:
52
  if self.is_math_question(message.content):
53
- response = await self.handle_math_question(message.content)
 
 
54
  else:
55
  response = await self.generate_response(message)
56
- await self.send_long_message(message.channel, response)
57
  finally:
58
  self.is_processing = False
59
 
 
60
  def is_message_in_specific_channel(self, message):
61
  return message.channel.id == SPECIFIC_CHANNEL_ID or (
62
  isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
@@ -65,15 +68,19 @@ class MyClient(discord.Client):
65
  def is_math_question(self, content):
66
  return bool(re.search(r'\b(solve|equation|calculate|math)\b', content, re.IGNORECASE))
67
 
68
- async def handle_math_question(self, question):
69
- loop = asyncio.get_event_loop()
70
- response = await loop.run_in_executor(None, lambda: self.math_pipe([{"role": "user", "content": question}]))
71
- # ์ˆ˜ํ•™์  ์ˆ˜์‹์„ LaTeX ํฌ๋งท์œผ๋กœ ๊ฐ์‹ธ๊ธฐ
72
- math_response = response[0]['generated_text']
73
- # ๋””์Šค์ฝ”๋“œ์—์„œ LaTeX ์ˆ˜์‹์œผ๋กœ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•œ ํ˜•์‹
74
- latex_output = f"$$\n{math_response}\n$$"
75
- return latex_output
76
-
 
 
 
 
77
  async def generate_response(self, message):
78
  global conversation_history
79
  user_input = message.content
 
6
  import asyncio
7
  import subprocess
8
  import re
9
+ import urllib.parse
10
  # ๋กœ๊น… ์„ค์ •
11
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
12
 
 
50
  self.is_processing = True
51
  try:
52
  if self.is_math_question(message.content):
53
+ text_response, img_url = await self.handle_math_question(message.content)
54
+ await self.send_long_message(message.channel, text_response)
55
+ await self.send_long_message(message.channel, img_url) # ์ด๋ฏธ์ง€ URL์„ ๋ณ„๋„์˜ ๋ฉ”์‹œ์ง€๋กœ ์ „์†ก
56
  else:
57
  response = await self.generate_response(message)
58
+ await self.send_long_message(message.channel, response)
59
  finally:
60
  self.is_processing = False
61
 
62
+
63
  def is_message_in_specific_channel(self, message):
64
  return message.channel.id == SPECIFIC_CHANNEL_ID or (
65
  isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
 
68
  def is_math_question(self, content):
69
  return bool(re.search(r'\b(solve|equation|calculate|math)\b', content, re.IGNORECASE))
70
 
71
+ async def handle_math_question(self, question):
72
+ loop = asyncio.get_event_loop()
73
+ response = await loop.run_in_executor(None, lambda: self.math_pipe([{"role": "user", "content": question}]))
74
+ math_response = response[0]['generated_text']
75
+
76
+ # QuickLaTeX API๋ฅผ ์ด์šฉํ•˜์—ฌ LaTeX ์ˆ˜์‹์„ ์ด๋ฏธ์ง€๋กœ ๋ณ€ํ™˜
77
+ encoded_formula = urllib.parse.quote_plus(math_response)
78
+ quicklatex_url = f"https://quicklatex.com/latex3.f/png?formula={encoded_formula}"
79
+ image_response = requests.get(quicklatex_url)
80
+ image_url = image_response.text.split('\n')[1] # ์‘๋‹ต์—์„œ ์ด๋ฏธ์ง€ URL ์ถ”์ถœ
81
+
82
+ return math_response, image_url
83
+
84
  async def generate_response(self, message):
85
  global conversation_history
86
  user_input = message.content