|
|
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) |