seawolf2357 commited on
Commit
aa7be7c
ยท
verified ยท
1 Parent(s): 95635e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -1
app.py CHANGED
@@ -29,6 +29,9 @@ hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("
29
  model = PaliGemmaForConditionalGeneration.from_pretrained("gokaygokay/sd3-long-captioner").to("cpu").eval()
30
  processor = PaliGemmaProcessor.from_pretrained("gokaygokay/sd3-long-captioner")
31
 
 
 
 
32
  def modify_caption(caption: str) -> str:
33
  prefix_substrings = [
34
  ('captured from ', ''),
@@ -82,6 +85,31 @@ async def translate_to_korean(text: str) -> str:
82
  full_response_text = ''.join(full_response)
83
  return full_response_text.strip()
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ •
86
  def create_captions_rich_sync(image):
87
  caption = asyncio.run(create_captions_rich(image))
@@ -142,6 +170,10 @@ class MyClient(discord.Client):
142
  image_url = message.attachments[0].url
143
  response = await process_image(image_url, message)
144
  await message.channel.send(response)
 
 
 
 
145
  finally:
146
  self.is_processing = False
147
 
@@ -154,7 +186,8 @@ async def process_image(image_url, message):
154
  image = await download_image(image_url)
155
  caption = await create_captions_rich(image)
156
  translated_caption = await translate_to_korean(caption)
157
- return f"{message.author.mention}, ์ธ์‹๋œ ์ด๋ฏธ์ง€ ์„ค๋ช…: {translated_caption}"
 
158
 
159
  async def download_image(url):
160
  response = requests.get(url)
@@ -164,3 +197,4 @@ async def download_image(url):
164
  if __name__ == "__main__":
165
  discord_client = MyClient(intents=intents)
166
  discord_client.run(os.getenv('DISCORD_TOKEN'))
 
 
29
  model = PaliGemmaForConditionalGeneration.from_pretrained("gokaygokay/sd3-long-captioner").to("cpu").eval()
30
  processor = PaliGemmaProcessor.from_pretrained("gokaygokay/sd3-long-captioner")
31
 
32
+ # ๋Œ€ํ™” ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ์ €์žฅํ•  ์ „์—ญ ๋ณ€์ˆ˜
33
+ conversation_history = []
34
+
35
  def modify_caption(caption: str) -> str:
36
  prefix_substrings = [
37
  ('captured from ', ''),
 
85
  full_response_text = ''.join(full_response)
86
  return full_response_text.strip()
87
 
88
+ async def interact_with_model(user_input: str) -> str:
89
+ global conversation_history
90
+ conversation_history.append({"role": "user", "content": user_input})
91
+
92
+ messages = [
93
+ {"role": "system", "content": "Translate the following text from English to Korean and respond as if you are an assistant who provides detailed answers in Korean."},
94
+ ] + conversation_history
95
+
96
+ loop = asyncio.get_event_loop()
97
+ response = await loop.run_in_executor(
98
+ None,
99
+ lambda: hf_client.chat_completion(
100
+ messages, max_tokens=1000, stream=True, temperature=0.7, top_p=0.85
101
+ )
102
+ )
103
+
104
+ full_response = []
105
+ for part in response:
106
+ if part.choices and part.choices[0].delta and part.choices[0].delta.content:
107
+ full_response.append(part.choices[0].delta.content)
108
+
109
+ full_response_text = ''.join(full_response)
110
+ conversation_history.append({"role": "assistant", "content": full_response_text})
111
+ return full_response_text.strip()
112
+
113
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ •
114
  def create_captions_rich_sync(image):
115
  caption = asyncio.run(create_captions_rich(image))
 
170
  image_url = message.attachments[0].url
171
  response = await process_image(image_url, message)
172
  await message.channel.send(response)
173
+ else:
174
+ # ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•˜์—ฌ Cohere ๋ชจ๋ธ๊ณผ ์ƒํ˜ธ์ž‘์šฉ
175
+ response = await interact_with_model(message.content)
176
+ await message.channel.send(response)
177
  finally:
178
  self.is_processing = False
179
 
 
186
  image = await download_image(image_url)
187
  caption = await create_captions_rich(image)
188
  translated_caption = await translate_to_korean(caption)
189
+ intro_message = f"{message.author.mention}, ์ธ์‹๋œ ์ด๋ฏธ์ง€ ์„ค๋ช…: {translated_caption}\n\n์งˆ๋ฌธ์ด ์žˆ์œผ๋ฉด ๋ฌผ์–ด๋ณด์„ธ์š”!"
190
+ return intro_message
191
 
192
  async def download_image(url):
193
  response = requests.get(url)
 
197
  if __name__ == "__main__":
198
  discord_client = MyClient(intents=intents)
199
  discord_client.run(os.getenv('DISCORD_TOKEN'))
200
+