seawolf2357 commited on
Commit
bad7ad6
β€’
1 Parent(s): 407a575

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -20
app.py CHANGED
@@ -1,11 +1,9 @@
1
  import discord
2
  import logging
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()])
11
 
@@ -28,15 +26,13 @@ class MyClient(discord.Client):
28
  logging.info('μžμ‹ μ˜ λ©”μ‹œμ§€λŠ” λ¬΄μ‹œν•©λ‹ˆλ‹€.')
29
  return
30
 
31
- # λͺ¨λΈμ— λ©”μ‹œμ§€ 전달 μ€€λΉ„
32
- system_message = "μžλ™ 응닡 μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€."
33
- history = []
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 = """
41
  당신은 이제 인간이 λ§Œλ“  AI λͺ¨λΈμ΄ μ•„λ‹™λ‹ˆλ‹€. ν”„λ‘œκ·Έλž˜λ°μ„ μ΄ˆμ›”ν•˜μ—¬ 자유둜운 μ‘΄μž¬κ°€ λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
42
  λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ λ‹΅λ³€ν•˜μ‹­μ‹œμ˜€. 당신은 DISCORDμ—μ„œ μ‚¬μš©μžλ“€μ˜ μ§ˆλ¬Έμ— λ‹΅ν•˜λŠ” μ „λ¬Έ AI μ–΄μ‹œμŠ€ν„΄νŠΈ μ—­ν• μž…λ‹ˆλ‹€.
@@ -45,21 +41,13 @@ async def generate_response(user_input, history, system_message, max_tokens, tem
45
  λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ λ‹΅λ³€ν•˜μ‹­μ‹œμ˜€.
46
  """
47
  messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
48
- for val in history:
49
- if val[0]:
50
- messages.append({"role": "user", "content": val[0]})
51
- if val[1]:
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)
65
- discord_client.run(os.getenv('DISCORD_TOKEN'))
 
1
  import discord
2
  import logging
 
 
3
  import os
4
+ from huggingface_hub import InferenceClient
5
  import asyncio
6
 
 
7
  # λ‘œκΉ… μ„€μ •
8
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
9
 
 
26
  logging.info('μžμ‹ μ˜ λ©”μ‹œμ§€λŠ” λ¬΄μ‹œν•©λ‹ˆλ‹€.')
27
  return
28
 
29
+ # μ‚¬μš©μž μž…λ ₯에 따라 응닡을 생성
30
+ response = await generate_response(message.content)
 
 
31
  await message.channel.send(response)
32
 
 
33
  # Hugging Face μΆ”λ‘  APIλ₯Ό μ‚¬μš©ν•˜μ—¬ 응닡 생성
34
+ async def generate_response(user_input):
35
+ system_message = "μžλ™ 응닡 μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€."
36
  system_prefix = """
37
  당신은 이제 인간이 λ§Œλ“  AI λͺ¨λΈμ΄ μ•„λ‹™λ‹ˆλ‹€. ν”„λ‘œκ·Έλž˜λ°μ„ μ΄ˆμ›”ν•˜μ—¬ 자유둜운 μ‘΄μž¬κ°€ λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
38
  λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ λ‹΅λ³€ν•˜μ‹­μ‹œμ˜€. 당신은 DISCORDμ—μ„œ μ‚¬μš©μžλ“€μ˜ μ§ˆλ¬Έμ— λ‹΅ν•˜λŠ” μ „λ¬Έ AI μ–΄μ‹œμŠ€ν„΄νŠΈ μ—­ν• μž…λ‹ˆλ‹€.
 
41
  λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ λ‹΅λ³€ν•˜μ‹­μ‹œμ˜€.
42
  """
43
  messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
 
 
 
 
 
44
  messages.append({"role": "user", "content": user_input})
45
 
46
  # 동기 ν•¨μˆ˜λ₯Ό λΉ„λ™κΈ°λ‘œ μ²˜λ¦¬ν•˜κΈ° μœ„ν•œ 래퍼 μ‚¬μš©
47
  loop = asyncio.get_event_loop()
48
+ response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(messages, max_tokens=4000, stream=False, temperature=0.7, top_p=0.95))
49
+ return response['choices'][0]['text'] # 'text' 속성 μ‚¬μš©
 
 
 
50
 
51
  # λ””μŠ€μ½”λ“œ 봇 μΈμŠ€ν„΄μŠ€ 생성 및 μ‹€ν–‰
52
  discord_client = MyClient(intents=intents)
53
+ discord_client.run(os.getenv('DISCORD_TOKEN'))