Upload code.py
Browse files
code.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
class SimpleAssistant:
|
| 4 |
+
def __init__(self, model_name="facebook/bart-large-cnn"):
|
| 5 |
+
"""
|
| 6 |
+
สร้าง AI ผู้ช่วยอย่างง่ายโดยใช้โมเดลภาษาที่กำหนด
|
| 7 |
+
"""
|
| 8 |
+
self.model_name = model_name
|
| 9 |
+
self.nlp = pipeline("text-generation", model=self.model_name)
|
| 10 |
+
|
| 11 |
+
def get_response(self, user_input, max_length=50, num_return_sequences=1):
|
| 12 |
+
"""
|
| 13 |
+
สร้างการตอบสนองต่ออินพุตของผู้ใช้
|
| 14 |
+
"""
|
| 15 |
+
try:
|
| 16 |
+
response = self.nlp(user_input, max_length=max_length, num_return_sequences=num_return_sequences)
|
| 17 |
+
return response[0]['generated_text']
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"เกิดข้อผิดพลาด: {e}")
|
| 20 |
+
return "ขออภัย ฉันไม่เข้าใจ"
|
| 21 |
+
|
| 22 |
+
# ตัวอย่างการใช้งาน
|
| 23 |
+
if __name__ == "__main__":
|
| 24 |
+
assistant = SimpleAssistant()
|
| 25 |
+
|
| 26 |
+
print("ผู้ช่วยพร้อมใช้งานแล้ว! พิมพ์ 'ออก' เพื่อจบการสนทนา")
|
| 27 |
+
|
| 28 |
+
while True:
|
| 29 |
+
user_input = input("คุณ: ")
|
| 30 |
+
if user_input.lower() == "ออก":
|
| 31 |
+
print("ลาก่อน!")
|
| 32 |
+
break
|
| 33 |
+
|
| 34 |
+
response = assistant.get_response(user_input)
|
| 35 |
+
print("ผู้ช่วย: " + response)
|