Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from simple_salesforce import Salesforce
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
# Load the Hugging Face model for Question Answering (FAQ-based)
|
| 8 |
+
faq_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
| 9 |
+
|
| 10 |
+
# Connect to Salesforce (replace with your credentials)
|
| 11 |
+
sf = Salesforce(username='your_username', password='your_password', security_token='your_security_token')
|
| 12 |
+
|
| 13 |
+
# Function to get the answer from the model
|
| 14 |
+
def get_answer(question, context):
|
| 15 |
+
result = faq_model(question=question, context=context)
|
| 16 |
+
return result['answer']
|
| 17 |
+
|
| 18 |
+
# Function to create a case in Salesforce if the chatbot requires more assistance
|
| 19 |
+
def create_case(subject, description):
|
| 20 |
+
case = sf.Case.create({
|
| 21 |
+
'Subject': subject,
|
| 22 |
+
'Description': description,
|
| 23 |
+
'Status': 'New',
|
| 24 |
+
'Priority': 'Medium'
|
| 25 |
+
})
|
| 26 |
+
return case
|
| 27 |
+
|
| 28 |
+
# Route to handle the user’s question
|
| 29 |
+
@app.route('/ask', methods=['POST'])
|
| 30 |
+
def ask_question():
|
| 31 |
+
data = request.get_json()
|
| 32 |
+
question = data.get('question')
|
| 33 |
+
|
| 34 |
+
# Define the FAQ context (static or fetched from a dynamic source)
|
| 35 |
+
faq_context = "Here are some frequently asked questions and answers about our service..."
|
| 36 |
+
|
| 37 |
+
# Get the answer to the question
|
| 38 |
+
answer = get_answer(question, faq_context)
|
| 39 |
+
|
| 40 |
+
# If the answer requires further assistance, create a case in Salesforce
|
| 41 |
+
if "need help" in answer or "contact us" in answer:
|
| 42 |
+
create_case('Customer Support Needed', answer)
|
| 43 |
+
|
| 44 |
+
return jsonify({"answer": answer})
|
| 45 |
+
|
| 46 |
+
if __name__ == '__main__':
|
| 47 |
+
app.run(debug=True, host='0.0.0.0', port=5000)
|