Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from flask import Flask, render_template, request, jsonify
|
| 3 |
+
import openai
|
| 4 |
+
import requests
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from utils import create_case_in_salesforce
|
| 7 |
+
|
| 8 |
+
# Load environment variables from .env file
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
# Set up API keys
|
| 12 |
+
HUGGINGFACE_API_KEY = os.getenv('HUGGINGFACE_API_KEY')
|
| 13 |
+
SALESFORCE_API_URL = os.getenv('SALESFORCE_API_URL')
|
| 14 |
+
SALESFORCE_API_KEY = os.getenv('SALESFORCE_API_KEY')
|
| 15 |
+
|
| 16 |
+
# Set up Flask app
|
| 17 |
+
app = Flask(__name__)
|
| 18 |
+
|
| 19 |
+
# Set up Hugging Face API Client
|
| 20 |
+
client = InferenceClient(HUGGINGFACE_API_KEY)
|
| 21 |
+
|
| 22 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 23 |
+
messages = [{"role": "system", "content": system_message}]
|
| 24 |
+
|
| 25 |
+
# Include message history (FAQs)
|
| 26 |
+
for val in history:
|
| 27 |
+
if val[0]:
|
| 28 |
+
messages.append({"role": "user", "content": val[0]})
|
| 29 |
+
if val[1]:
|
| 30 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 31 |
+
|
| 32 |
+
messages.append({"role": "user", "content": message})
|
| 33 |
+
|
| 34 |
+
# Get response from Hugging Face API (for FAQ)
|
| 35 |
+
response = ""
|
| 36 |
+
for message in client.chat_completion(
|
| 37 |
+
messages,
|
| 38 |
+
max_tokens=max_tokens,
|
| 39 |
+
stream=True,
|
| 40 |
+
temperature=temperature,
|
| 41 |
+
top_p=top_p,
|
| 42 |
+
):
|
| 43 |
+
token = message.choices[0].delta.content
|
| 44 |
+
response += token
|
| 45 |
+
|
| 46 |
+
return response
|
| 47 |
+
|
| 48 |
+
# Endpoint for chat responses
|
| 49 |
+
@app.route('/chat', methods=['POST'])
|
| 50 |
+
def chat():
|
| 51 |
+
user_message = request.form['message']
|
| 52 |
+
history = request.form['history']
|
| 53 |
+
system_message = "You are a helpful FAQ chatbot for customer support."
|
| 54 |
+
max_tokens = 512
|
| 55 |
+
temperature = 0.7
|
| 56 |
+
top_p = 0.9
|
| 57 |
+
|
| 58 |
+
# Get response from Hugging Face model
|
| 59 |
+
bot_reply = respond(user_message, history, system_message, max_tokens, temperature, top_p)
|
| 60 |
+
|
| 61 |
+
# If the FAQ response requires a case to be created, initiate the case creation
|
| 62 |
+
if "create a case" in bot_reply.lower():
|
| 63 |
+
create_case_in_salesforce(user_message)
|
| 64 |
+
|
| 65 |
+
return jsonify({'response': bot_reply})
|
| 66 |
+
|
| 67 |
+
# Home page for the chatbot UI
|
| 68 |
+
@app.route('/')
|
| 69 |
+
def index():
|
| 70 |
+
return render_template('index.html')
|
| 71 |
+
|
| 72 |
+
if __name__ == '__main__':
|
| 73 |
+
app.run(debug=True)
|