Spaces:
No application file
No application file
bot
#1
by
kitsada001
- opened
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import discord
|
2 |
+
from discord.ext import commands
|
3 |
+
import requests
|
4 |
+
|
5 |
+
# กำหนด Token ของบอท
|
6 |
+
TOKEN = 'your_discord_bot_token'
|
7 |
+
API_TOKEN = 'your_huggingface_api_token'
|
8 |
+
model_id = 'gpt-2' # โมเดลที่ต้องการใช้ (สามารถเปลี่ยนเป็นโมเดลที่คุณต้องการ)
|
9 |
+
|
10 |
+
# สร้างบอท Discord
|
11 |
+
intents = discord.Intents.default()
|
12 |
+
intents.message_content = True # ต้องเปิดให้บอทอ่านเนื้อหาข้อความ
|
13 |
+
bot = commands.Bot(command_prefix='!', intents=intents)
|
14 |
+
|
15 |
+
# ฟังก์ชั่นในการเรียก Hugging Face API
|
16 |
+
def get_huggingface_response(prompt):
|
17 |
+
headers = {
|
18 |
+
"Authorization": f"Bearer {API_TOKEN}",
|
19 |
+
}
|
20 |
+
url = f"https://api-inference.huggingface.co/models/{model_id}"
|
21 |
+
data = {
|
22 |
+
"inputs": prompt
|
23 |
+
}
|
24 |
+
|
25 |
+
# ส่งคำขอไปยัง API
|
26 |
+
response = requests.post(url, headers=headers, json=data)
|
27 |
+
|
28 |
+
# ตรวจสอบสถานะการตอบกลับ
|
29 |
+
if response.status_code == 200:
|
30 |
+
return response.json()[0]['generated_text'] # คืนค่าผลลัพธ์จากโมเดล
|
31 |
+
else:
|
32 |
+
return "เกิดข้อผิดพลาดในการตอบคำขอจาก Hugging Face API"
|
33 |
+
|
34 |
+
# คำสั่ง Discord ที่จะให้บอทตอบกลับ
|
35 |
+
@bot.command()
|
36 |
+
async def generate(ctx, *, prompt: str):
|
37 |
+
"""ใช้คำสั่ง !generate <ข้อความ> เพื่อให้บอทสร้างข้อความจากโมเดล"""
|
38 |
+
await ctx.send(f"กำลังสร้างข้อความจากโมเดล...")
|
39 |
+
|
40 |
+
# เรียกใช้ฟังก์ชั่นเพื่อส่งข้อความไปที่ Hugging Face
|
41 |
+
result = get_huggingface_response(prompt)
|
42 |
+
|
43 |
+
# ส่งผลลัพธ์กลับไปยัง Discord
|
44 |
+
await ctx.send(result)
|
45 |
+
|
46 |
+
# เริ่มต้นบอท
|
47 |
+
bot.run(TOKEN)
|