Spaces:
Runtime error
Runtime error
Create chatbot.py
Browse files- chatbot.py +66 -0
chatbot.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 2 |
+
import torch
|
| 3 |
+
from simple_salesforce import Salesforce
|
| 4 |
+
from model import label_encoder # Assuming the label_encoder was saved in the model.py file
|
| 5 |
+
|
| 6 |
+
# Load pre-trained model and tokenizer
|
| 7 |
+
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
| 8 |
+
model = BertForSequenceClassification.from_pretrained("results") # Path to your trained model
|
| 9 |
+
|
| 10 |
+
# Salesforce authentication setup
|
| 11 |
+
sf = Salesforce(username="your_username", password="your_password", security_token="your_token")
|
| 12 |
+
|
| 13 |
+
# Function to classify the intent of user input
|
| 14 |
+
def classify_intent(user_input):
|
| 15 |
+
# Tokenize the user input and make a prediction
|
| 16 |
+
encoded_input = tokenizer(user_input, padding=True, truncation=True, return_tensors="pt")
|
| 17 |
+
|
| 18 |
+
# Run the model without gradients for efficiency
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
output = model(**encoded_input)
|
| 21 |
+
|
| 22 |
+
# Get the predicted label and return it
|
| 23 |
+
predicted_label = torch.argmax(output.logits, dim=1).item()
|
| 24 |
+
return label_encoder.inverse_transform([predicted_label])[0] # Return intent label
|
| 25 |
+
|
| 26 |
+
# Function to generate a response based on the classified intent
|
| 27 |
+
def generate_response(intent):
|
| 28 |
+
# Simple responses based on predefined intents
|
| 29 |
+
responses = {
|
| 30 |
+
"greeting": "Hi, how can I help you today?",
|
| 31 |
+
"goodbye": "Goodbye! Have a nice day!",
|
| 32 |
+
"question": "Hugging Face is a company that specializes in NLP models."
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
# Return response for the given intent
|
| 36 |
+
return responses.get(intent, "Sorry, I didn't understand that.")
|
| 37 |
+
|
| 38 |
+
# Function to create a case in Salesforce based on user input and chatbot response
|
| 39 |
+
def create_case_in_salesforce(user_input, response):
|
| 40 |
+
case_data = {
|
| 41 |
+
'Subject': f"User Query: {user_input}",
|
| 42 |
+
'Description': f"User asked: {user_input}\nResponse: {response}"
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
# Create the case in Salesforce
|
| 46 |
+
case = sf.Case.create(case_data)
|
| 47 |
+
print(f"Case Created with ID: {case['id']}")
|
| 48 |
+
|
| 49 |
+
# Main function to handle the full interaction
|
| 50 |
+
def chatbot_response(user_input):
|
| 51 |
+
# Step 1: Classify the user input into an intent
|
| 52 |
+
intent = classify_intent(user_input)
|
| 53 |
+
|
| 54 |
+
# Step 2: Generate the appropriate response based on the intent
|
| 55 |
+
response = generate_response(intent)
|
| 56 |
+
|
| 57 |
+
# Step 3: Optionally create a case in Salesforce (e.g., for queries or support requests)
|
| 58 |
+
create_case_in_salesforce(user_input, response)
|
| 59 |
+
|
| 60 |
+
# Step 4: Return the chatbot's response
|
| 61 |
+
return response
|
| 62 |
+
|
| 63 |
+
# Example of using the chatbot
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
user_input = "Hello, tell me about Hugging Face."
|
| 66 |
+
print(chatbot_response(user_input)) # Should print the chatbot's response and create a Salesforce case
|