Update app.py
Browse files
app.py
CHANGED
|
@@ -1,107 +1,107 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
-
from dotenv import load_dotenv
|
| 3 |
-
import requests
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
app = Flask(__name__)
|
| 7 |
-
load_dotenv()
|
| 8 |
-
|
| 9 |
-
VOICEFLOW_URL = "https://general-runtime.voiceflow.com/state/user/userID/interact?logs=off"
|
| 10 |
-
VOICEFLOW_API_KEY = os.getenv("VOICEFLOW_API_KEY")
|
| 11 |
-
|
| 12 |
-
HEADERS = {
|
| 13 |
-
"accept": "application/json",
|
| 14 |
-
"content-type": "application/json",
|
| 15 |
-
"Authorization": VOICEFLOW_API_KEY
|
| 16 |
-
}
|
| 17 |
-
|
| 18 |
-
CONFIG = {
|
| 19 |
-
"tts": False,
|
| 20 |
-
"stripSSML": True,
|
| 21 |
-
"stopAll": True,
|
| 22 |
-
"excludeTypes": ["block", "debug", "flow"]
|
| 23 |
-
}
|
| 24 |
-
|
| 25 |
-
def get_nested_data(data, keys, default=None):
|
| 26 |
-
for key in keys:
|
| 27 |
-
try:
|
| 28 |
-
if isinstance(data, list):
|
| 29 |
-
data = data[key] if key < len(data) else default
|
| 30 |
-
else:
|
| 31 |
-
data = data.get(key, default)
|
| 32 |
-
except (AttributeError, IndexError, TypeError):
|
| 33 |
-
return default
|
| 34 |
-
if data is default:
|
| 35 |
-
break
|
| 36 |
-
return data
|
| 37 |
-
|
| 38 |
-
def interact_with_voiceflow(action_type, payload=None):
|
| 39 |
-
payload = {
|
| 40 |
-
"action": {"type": action_type, "payload": payload},
|
| 41 |
-
"config": CONFIG
|
| 42 |
-
}
|
| 43 |
-
response = requests.post(VOICEFLOW_URL, json=payload, headers=HEADERS)
|
| 44 |
-
response.raise_for_status() # Will raise an error for bad status codes
|
| 45 |
-
return response.json()
|
| 46 |
-
|
| 47 |
-
def process_response(response_data):
|
| 48 |
-
messages = set() # Use a set to avoid duplicate messages
|
| 49 |
-
for item in response_data:
|
| 50 |
-
if item['type'] == 'text':
|
| 51 |
-
messages.add(item['payload'].get('message', '').replace('\n', ' ').replace('**', ''))
|
| 52 |
-
for content_item in item['payload'].get('slate', {}).get('content', []):
|
| 53 |
-
for child in content_item.get('children', []):
|
| 54 |
-
text = child.get('text', '')
|
| 55 |
-
if text:
|
| 56 |
-
messages.add(text)
|
| 57 |
-
return list(messages) # Convert the set back to a list
|
| 58 |
-
|
| 59 |
-
@app.route('/')
|
| 60 |
-
def index():
|
| 61 |
-
return f'/start for initiating booking process <br> /query for asking questions <br> API Key: {VOICEFLOW_API_KEY}'
|
| 62 |
-
|
| 63 |
-
@app.route('/start', methods=['POST'])
|
| 64 |
-
def launch():
|
| 65 |
-
if not request.is_json:
|
| 66 |
-
return jsonify({"error": "Request must be JSON"}), 400
|
| 67 |
-
|
| 68 |
-
data = request.get_json()
|
| 69 |
-
tool_call_id = get_nested_data(data, ["message", "toolWithToolCallList", 0, "toolCall", "id"])
|
| 70 |
-
user_question = get_nested_data(data, ["message", "toolWithToolCallList", 0, "toolCall", "function", "arguments", "query"])
|
| 71 |
-
|
| 72 |
-
if user_question is None:
|
| 73 |
-
return jsonify({"error": "Question not found in request"}), 400
|
| 74 |
-
|
| 75 |
-
print('STARTING VF...')
|
| 76 |
-
print('START QUESTION:', user_question)
|
| 77 |
-
|
| 78 |
-
response_data = interact_with_voiceflow("launch")
|
| 79 |
-
messages = process_response(response_data)
|
| 80 |
-
|
| 81 |
-
print('START VF RESPONSE:', messages)
|
| 82 |
-
|
| 83 |
-
return jsonify({"results": [{"toolCallId": tool_call_id, "result": messages}]})
|
| 84 |
-
|
| 85 |
-
@app.route('/query', methods=['POST'])
|
| 86 |
-
def api_call():
|
| 87 |
-
if not request.is_json:
|
| 88 |
-
return jsonify({"error": "Request must be JSON"}), 400
|
| 89 |
-
|
| 90 |
-
data = request.get_json()
|
| 91 |
-
tool_call_id = get_nested_data(data, ["message", "toolWithToolCallList", 0, "toolCall", "id"])
|
| 92 |
-
user_question = get_nested_data(data, ["message", "toolWithToolCallList", 0, "toolCall", "function", "arguments", "query"])
|
| 93 |
-
|
| 94 |
-
if user_question is None:
|
| 95 |
-
return jsonify({"error": "Question not found in request"}), 400
|
| 96 |
-
|
| 97 |
-
print('QUERY TO VF: ', user_question)
|
| 98 |
-
|
| 99 |
-
response_data = interact_with_voiceflow("text", user_question)
|
| 100 |
-
messages = process_response(response_data)
|
| 101 |
-
|
| 102 |
-
print('QUERY VF RESPONSE:', messages)
|
| 103 |
-
|
| 104 |
-
return jsonify({"results": [{"toolCallId": tool_call_id, "result": messages}]})
|
| 105 |
-
|
| 106 |
-
if __name__ == '__main__':
|
| 107 |
-
app.run(host='0.0.0.0', port=
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
VOICEFLOW_URL = "https://general-runtime.voiceflow.com/state/user/userID/interact?logs=off"
|
| 10 |
+
VOICEFLOW_API_KEY = os.getenv("VOICEFLOW_API_KEY")
|
| 11 |
+
|
| 12 |
+
HEADERS = {
|
| 13 |
+
"accept": "application/json",
|
| 14 |
+
"content-type": "application/json",
|
| 15 |
+
"Authorization": VOICEFLOW_API_KEY
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
CONFIG = {
|
| 19 |
+
"tts": False,
|
| 20 |
+
"stripSSML": True,
|
| 21 |
+
"stopAll": True,
|
| 22 |
+
"excludeTypes": ["block", "debug", "flow"]
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
def get_nested_data(data, keys, default=None):
|
| 26 |
+
for key in keys:
|
| 27 |
+
try:
|
| 28 |
+
if isinstance(data, list):
|
| 29 |
+
data = data[key] if key < len(data) else default
|
| 30 |
+
else:
|
| 31 |
+
data = data.get(key, default)
|
| 32 |
+
except (AttributeError, IndexError, TypeError):
|
| 33 |
+
return default
|
| 34 |
+
if data is default:
|
| 35 |
+
break
|
| 36 |
+
return data
|
| 37 |
+
|
| 38 |
+
def interact_with_voiceflow(action_type, payload=None):
|
| 39 |
+
payload = {
|
| 40 |
+
"action": {"type": action_type, "payload": payload},
|
| 41 |
+
"config": CONFIG
|
| 42 |
+
}
|
| 43 |
+
response = requests.post(VOICEFLOW_URL, json=payload, headers=HEADERS)
|
| 44 |
+
response.raise_for_status() # Will raise an error for bad status codes
|
| 45 |
+
return response.json()
|
| 46 |
+
|
| 47 |
+
def process_response(response_data):
|
| 48 |
+
messages = set() # Use a set to avoid duplicate messages
|
| 49 |
+
for item in response_data:
|
| 50 |
+
if item['type'] == 'text':
|
| 51 |
+
messages.add(item['payload'].get('message', '').replace('\n', ' ').replace('**', ''))
|
| 52 |
+
for content_item in item['payload'].get('slate', {}).get('content', []):
|
| 53 |
+
for child in content_item.get('children', []):
|
| 54 |
+
text = child.get('text', '')
|
| 55 |
+
if text:
|
| 56 |
+
messages.add(text)
|
| 57 |
+
return list(messages) # Convert the set back to a list
|
| 58 |
+
|
| 59 |
+
@app.route('/')
|
| 60 |
+
def index():
|
| 61 |
+
return f'/start for initiating booking process <br> /query for asking questions <br> API Key: {VOICEFLOW_API_KEY}'
|
| 62 |
+
|
| 63 |
+
@app.route('/start', methods=['POST'])
|
| 64 |
+
def launch():
|
| 65 |
+
if not request.is_json:
|
| 66 |
+
return jsonify({"error": "Request must be JSON"}), 400
|
| 67 |
+
|
| 68 |
+
data = request.get_json()
|
| 69 |
+
tool_call_id = get_nested_data(data, ["message", "toolWithToolCallList", 0, "toolCall", "id"])
|
| 70 |
+
user_question = get_nested_data(data, ["message", "toolWithToolCallList", 0, "toolCall", "function", "arguments", "query"])
|
| 71 |
+
|
| 72 |
+
if user_question is None:
|
| 73 |
+
return jsonify({"error": "Question not found in request"}), 400
|
| 74 |
+
|
| 75 |
+
print('STARTING VF...')
|
| 76 |
+
print('START QUESTION:', user_question)
|
| 77 |
+
|
| 78 |
+
response_data = interact_with_voiceflow("launch")
|
| 79 |
+
messages = process_response(response_data)
|
| 80 |
+
|
| 81 |
+
print('START VF RESPONSE:', messages)
|
| 82 |
+
|
| 83 |
+
return jsonify({"results": [{"toolCallId": tool_call_id, "result": messages}]})
|
| 84 |
+
|
| 85 |
+
@app.route('/query', methods=['POST'])
|
| 86 |
+
def api_call():
|
| 87 |
+
if not request.is_json:
|
| 88 |
+
return jsonify({"error": "Request must be JSON"}), 400
|
| 89 |
+
|
| 90 |
+
data = request.get_json()
|
| 91 |
+
tool_call_id = get_nested_data(data, ["message", "toolWithToolCallList", 0, "toolCall", "id"])
|
| 92 |
+
user_question = get_nested_data(data, ["message", "toolWithToolCallList", 0, "toolCall", "function", "arguments", "query"])
|
| 93 |
+
|
| 94 |
+
if user_question is None:
|
| 95 |
+
return jsonify({"error": "Question not found in request"}), 400
|
| 96 |
+
|
| 97 |
+
print('QUERY TO VF: ', user_question)
|
| 98 |
+
|
| 99 |
+
response_data = interact_with_voiceflow("text", user_question)
|
| 100 |
+
messages = process_response(response_data)
|
| 101 |
+
|
| 102 |
+
print('QUERY VF RESPONSE:', messages)
|
| 103 |
+
|
| 104 |
+
return jsonify({"results": [{"toolCallId": tool_call_id, "result": messages}]})
|
| 105 |
+
|
| 106 |
+
if __name__ == '__main__':
|
| 107 |
+
app.run(host='0.0.0.0', port=7860, threaded=True)
|