Spaces:
Runtime error
Runtime error
Upload auto_responder.py
Browse files- auto_responder.py +68 -0
auto_responder.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
| 2 |
+
import torch
|
| 3 |
+
from typing import Dict
|
| 4 |
+
|
| 5 |
+
class AutoResponder:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
"""
|
| 8 |
+
Initialize the AutoResponder with the Blenderbot model
|
| 9 |
+
"""
|
| 10 |
+
self.model_name = "facebook/blenderbot-400M-distill"
|
| 11 |
+
self.tokenizer = BlenderbotTokenizer.from_pretrained(self.model_name)
|
| 12 |
+
self.model = BlenderbotForConditionalGeneration.from_pretrained(self.model_name)
|
| 13 |
+
self.conversation_history: Dict[str, list] = {}
|
| 14 |
+
|
| 15 |
+
def generate_response(self, message: str, sender_id: str) -> str:
|
| 16 |
+
"""
|
| 17 |
+
Generate an AI response to the incoming message using Blenderbot
|
| 18 |
+
"""
|
| 19 |
+
if sender_id not in self.conversation_history:
|
| 20 |
+
self.conversation_history[sender_id] = []
|
| 21 |
+
|
| 22 |
+
# Add the user message to conversation history
|
| 23 |
+
self.conversation_history[sender_id].append(message)
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
# Encode the input text
|
| 27 |
+
inputs = self.tokenizer(message, return_tensors="pt", truncation=True, max_length=512)
|
| 28 |
+
|
| 29 |
+
# Generate response
|
| 30 |
+
reply_ids = self.model.generate(
|
| 31 |
+
inputs["input_ids"],
|
| 32 |
+
max_length=128,
|
| 33 |
+
num_beams=4,
|
| 34 |
+
no_repeat_ngram_size=3,
|
| 35 |
+
temperature=0.7
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Decode the response
|
| 39 |
+
response = self.tokenizer.decode(reply_ids[0], skip_special_tokens=True)
|
| 40 |
+
|
| 41 |
+
# Add AI response to conversation history
|
| 42 |
+
self.conversation_history[sender_id].append(response)
|
| 43 |
+
|
| 44 |
+
return response
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"عذراً، حدث خطأ: {str(e)}"
|
| 48 |
+
|
| 49 |
+
def clear_history(self, sender_id: str):
|
| 50 |
+
"""
|
| 51 |
+
Clear conversation history for a specific sender
|
| 52 |
+
"""
|
| 53 |
+
if sender_id in self.conversation_history:
|
| 54 |
+
self.conversation_history[sender_id] = []
|
| 55 |
+
|
| 56 |
+
# Example usage
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
# Create an instance of AutoResponder
|
| 59 |
+
responder = AutoResponder()
|
| 60 |
+
|
| 61 |
+
# Example message
|
| 62 |
+
example_message = "مرحبا، كيف حالك؟"
|
| 63 |
+
sender = "user123"
|
| 64 |
+
|
| 65 |
+
# Generate and print response
|
| 66 |
+
response = responder.generate_response(example_message, sender)
|
| 67 |
+
print(f"الرسالة: {example_message}")
|
| 68 |
+
print(f"الرد: {response}")
|