File size: 2,146 Bytes
14ad37e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import discord
from discord.ext import commands
import requests

# กำหนด Token ของบอท
TOKEN = 'your_discord_bot_token'
API_TOKEN = 'your_huggingface_api_token'
model_id = 'gpt-2'  # โมเดลที่ต้องการใช้ (สามารถเปลี่ยนเป็นโมเดลที่คุณต้องการ)

# สร้างบอท Discord
intents = discord.Intents.default()
intents.message_content = True  # ต้องเปิดให้บอทอ่านเนื้อหาข้อความ
bot = commands.Bot(command_prefix='!', intents=intents)

# ฟังก์ชั่นในการเรียก Hugging Face API
def get_huggingface_response(prompt):
    headers = {
        "Authorization": f"Bearer {API_TOKEN}",
    }
    url = f"https://api-inference.huggingface.co/models/{model_id}"
    data = {
        "inputs": prompt
    }
    
    # ส่งคำขอไปยัง API
    response = requests.post(url, headers=headers, json=data)
    
    # ตรวจสอบสถานะการตอบกลับ
    if response.status_code == 200:
        return response.json()[0]['generated_text']  # คืนค่าผลลัพธ์จากโมเดล
    else:
        return "เกิดข้อผิดพลาดในการตอบคำขอจาก Hugging Face API"

# คำสั่ง Discord ที่จะให้บอทตอบกลับ
@bot.command()
async def generate(ctx, *, prompt: str):
    """ใช้คำสั่ง !generate <ข้อความ> เพื่อให้บอทสร้างข้อความจากโมเดล"""
    await ctx.send(f"กำลังสร้างข้อความจากโมเดล...")

    # เรียกใช้ฟังก์ชั่นเพื่อส่งข้อความไปที่ Hugging Face
    result = get_huggingface_response(prompt)
    
    # ส่งผลลัพธ์กลับไปยัง Discord
    await ctx.send(result)

# เริ่มต้นบอท
bot.run(TOKEN)