Spaces:
No application file
No application file
| 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 ที่จะให้บอทตอบกลับ | |
| 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) | |