seawolf2357 commited on
Commit
407a575
โ€ข
1 Parent(s): 55aa389

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -7
app.py CHANGED
@@ -3,6 +3,8 @@ import logging
3
  import gradio as gr
4
  from huggingface_hub import InferenceClient
5
  import os
 
 
6
 
7
  # ๋กœ๊น… ์„ค์ •
8
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
@@ -32,6 +34,7 @@ class MyClient(discord.Client):
32
  response = await generate_response(message.content, history, system_message, 4000, 0.7, 0.95)
33
  await message.channel.send(response)
34
 
 
35
  # Hugging Face ์ถ”๋ก  API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‘๋‹ต ์ƒ์„ฑ
36
  async def generate_response(user_input, history, system_message, max_tokens, temperature, top_p):
37
  system_prefix = """
@@ -49,13 +52,13 @@ async def generate_response(user_input, history, system_message, max_tokens, tem
49
  messages.append({"role": "assistant", "content": val[1]})
50
  messages.append({"role": "user", "content": user_input})
51
 
52
- try:
53
- response = await hf_client.chat_completion(messages, max_tokens=max_tokens, stream=False, temperature=temperature, top_p=top_p)
54
- # ์˜ฌ๋ฐ”๋ฅธ ์†์„ฑ์— ์ ‘๊ทผํ•˜์—ฌ ์‘๋‹ต ๋‚ด์šฉ์„ ์ถ”์ถœ
55
- return response.choices[0].text # 'delta' ๋Œ€์‹  'text' ๋˜๋Š” API ๋ฌธ์„œ์— ๋”ฐ๋ฅธ ์ ์ ˆํ•œ ์†์„ฑ ์‚ฌ์šฉ
56
- except Exception as e:
57
- logging.error(f"An error occurred: {e}")
58
- return "์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์‹œ๋„ํ•ด ์ฃผ์„ธ์š”."
59
 
60
  # ๋””์Šค์ฝ”๋“œ ๋ด‡ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ๋ฐ ์‹คํ–‰
61
  discord_client = MyClient(intents=intents)
 
3
  import gradio as gr
4
  from huggingface_hub import InferenceClient
5
  import os
6
+ import asyncio
7
+
8
 
9
  # ๋กœ๊น… ์„ค์ •
10
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
 
34
  response = await generate_response(message.content, history, system_message, 4000, 0.7, 0.95)
35
  await message.channel.send(response)
36
 
37
+
38
  # Hugging Face ์ถ”๋ก  API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‘๋‹ต ์ƒ์„ฑ
39
  async def generate_response(user_input, history, system_message, max_tokens, temperature, top_p):
40
  system_prefix = """
 
52
  messages.append({"role": "assistant", "content": val[1]})
53
  messages.append({"role": "user", "content": user_input})
54
 
55
+ # ๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ๋น„๋™๊ธฐ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ž˜ํผ ์‚ฌ์šฉ
56
+ loop = asyncio.get_event_loop()
57
+ response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(messages, max_tokens=max_tokens, stream=False, temperature=temperature, top_p=top_p))
58
+ return response['choices'][0]['message']['content'] # ์ ์ ˆํ•œ ์†์„ฑ ์‚ฌ์šฉ
59
+
60
+ # ๊ธฐ์กด ์ฝ”๋“œ๋Š” ์œ ์ง€
61
+
62
 
63
  # ๋””์Šค์ฝ”๋“œ ๋ด‡ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ๋ฐ ์‹คํ–‰
64
  discord_client = MyClient(intents=intents)