Chatbot1 / app.py
jithenderchoudary's picture
Update app.py
191db0a verified
raw
history blame
1.91 kB
from flask import Flask, request, jsonify
from transformers import pipeline
from simple_salesforce import Salesforce
app = Flask(__name__)
# Load the Hugging Face model for Question Answering (FAQ-based)
faq_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
# Salesforce connection (replace with your credentials)
sf = Salesforce(username='your_username', password='your_password', security_token='your_security_token')
# Function to get the answer from the model
def get_answer(question, context):
result = faq_model(question=question, context=context)
return result['answer']
# Function to create a case in Salesforce if the chatbot requires more assistance
def create_case(subject, description):
case = sf.Case.create({
'Subject': subject,
'Description': description,
'Status': 'New',
'Priority': 'Medium'
})
return case
# Route to handle the user’s question
@app.route('/ask', methods=['POST'])
def ask_question():
data = request.get_json()
question = data.get('question')
# Define the FAQ context (static or dynamic data can be used here)
faq_context = """
Here are some frequently asked questions and answers about our services.
1. How do I contact customer support? - You can email us at support@company.com.
2. What are your business hours? - We are open from 9 AM to 6 PM, Monday to Friday.
3. How do I reset my password? - Click on 'Forgot Password' on the login page.
"""
# Get the answer to the question
answer = get_answer(question, faq_context)
# If the answer suggests further help, create a case in Salesforce
if "contact us" in answer or "need help" in answer:
create_case('Customer Support Needed', answer)
return jsonify({"answer": answer})
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)