File size: 1,500 Bytes
2a871d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import pipeline

class SimpleAssistant:
    def __init__(self, model_name="facebook/bart-large-cnn"):
        """
        สร้าง AI ผู้ช่วยอย่างง่ายโดยใช้โมเดลภาษาที่กำหนด
        """
        self.model_name = model_name
        self.nlp = pipeline("text-generation", model=self.model_name)

    def get_response(self, user_input, max_length=50, num_return_sequences=1):
        """
        สร้างการตอบสนองต่ออินพุตของผู้ใช้
        """
        try:
            response = self.nlp(user_input, max_length=max_length, num_return_sequences=num_return_sequences)
            return response[0]['generated_text']
        except Exception as e:
            print(f"เกิดข้อผิดพลาด: {e}")
            return "ขออภัย ฉันไม่เข้าใจ"

# ตัวอย่างการใช้งาน
if __name__ == "__main__":
    assistant = SimpleAssistant()

    print("ผู้ช่วยพร้อมใช้งานแล้ว! พิมพ์ 'ออก' เพื่อจบการสนทนา")

    while True:
        user_input = input("คุณ: ")
        if user_input.lower() == "ออก":
            print("ลาก่อน!")
            break

        response = assistant.get_response(user_input)
        print("ผู้ช่วย: " + response)