Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,43 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
|
|
|
| 3 |
import os
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
import subprocess
|
| 7 |
-
|
| 8 |
-
# λ‘κΉ
μ€μ
|
| 9 |
-
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
|
| 10 |
-
|
| 11 |
-
# μΈν
νΈ μ€μ
|
| 12 |
-
intents = discord.Intents.default()
|
| 13 |
-
intents.message_content = True
|
| 14 |
-
intents.messages = True
|
| 15 |
-
intents.guilds = True
|
| 16 |
-
intents.guild_messages = True
|
| 17 |
-
|
| 18 |
-
# μΆλ‘ API ν΄λΌμ΄μΈνΈ μ€μ
|
| 19 |
-
hf_client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct", token=os.getenv("HF_TOKEN"))
|
| 20 |
-
|
| 21 |
-
# νΉμ μ±λ ID
|
| 22 |
-
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
| 23 |
-
|
| 24 |
-
# λν νμ€ν 리λ₯Ό μ μ₯ν μ μ λ³μ
|
| 25 |
-
conversation_history = []
|
| 26 |
-
|
| 27 |
-
class MyClient(discord.Client):
|
| 28 |
-
def __init__(self, *args, **kwargs):
|
| 29 |
-
super().__init__(*args, **kwargs)
|
| 30 |
-
self.is_processing = False
|
| 31 |
-
|
| 32 |
-
async def on_message(self, message):
|
| 33 |
-
if message.author == self.user:
|
| 34 |
-
return
|
| 35 |
-
if not self.is_message_in_specific_channel(message):
|
| 36 |
-
return
|
| 37 |
-
if self.is_processing:
|
| 38 |
-
return
|
| 39 |
-
self.is_processing = True
|
| 40 |
-
try:
|
| 41 |
-
response = await generate_response(message)
|
| 42 |
-
await message.channel.send(response)
|
| 43 |
-
finally:
|
| 44 |
-
self.is_processing = False
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
| 50 |
-
)
|
| 51 |
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
user_mention = message.author.mention
|
| 57 |
-
system_message = f"{user_mention}, DISCORDμμ μ¬μ©μλ€μ μ§λ¬Έμ λ΅νλ μ΄μμ€ν΄νΈμ
λλ€."
|
| 58 |
-
system_prefix = """
|
| 59 |
-
λ°λμ νκΈλ‘ λ΅λ³νμμμ€.
|
| 60 |
-
"""
|
| 61 |
-
conversation_history.append({"role": "user", "content": user_input})
|
| 62 |
-
logging.debug(f'Conversation history updated: {conversation_history}')
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
if part.choices and part.choices[0].delta and part.choices[0].delta.content:
|
| 75 |
-
full_response.append(part.choices[0].delta.content)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
import pandas as pd
|
| 4 |
import os
|
| 5 |
+
import pyarrow.parquet as pq
|
| 6 |
+
import discord
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Discord λ΄ ν ν° λ° μ±λ I μ€μ
|
| 9 |
+
discordtoken = "MI2Mk0zQ3czc0Mz4Mw.Glf6.2bSeer-q5jHVnClSe1wsP0IHpVyIdxGaYac"
|
| 10 |
+
discordchannelid = 1261896610506604564
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Discord ν΄λΌμ΄μΈνΈ μμ±
|
| 13 |
+
client = discord.Client()
|
| 14 |
|
| 15 |
+
def respondtodiscord(message):
|
| 16 |
+
# λ©μμ§ μ μ‘μ μν Discord API μλν¬μΈνΈ
|
| 17 |
+
url = f"https://discord.com/api/v9/channels/{discordchannelid}/messages"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# λ©μμ§ μ μ‘ λ°μ΄ν°
|
| 20 |
+
data = {
|
| 21 |
+
"content": message
|
| 22 |
+
}
|
| 23 |
|
| 24 |
+
# Discord API μΈμ¦ ν€λ
|
| 25 |
+
headers = {
|
| 26 |
+
"Authorization": discordtoken
|
| 27 |
+
}
|
| 28 |
|
| 29 |
+
try:
|
| 30 |
+
# λ©μμ§ μ μ‘
|
| 31 |
+
response = requests.post(url, headers=headers, json=data)
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
if response.statuscode == 200:
|
| 34 |
+
print("λ©μμ§ μ μ‘ μ±κ³΅")
|
| 35 |
+
else:
|
| 36 |
+
print(f"λ©μμ§ μ μ‘ μ€ν¨: {response.statuscode}")
|
| 37 |
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"μκΈ°μΉ λͺ»ν μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}")
|
| 40 |
|
| 41 |
+
def getanswer(question):
|
| 42 |
+
matchinganswer = df[df['question'] == question]['answer'].values
|
| 43 |
+
return matching
|