Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask,request,make_response
|
| 2 |
import os
|
| 3 |
import logging
|
| 4 |
from dotenv import load_dotenv
|
|
@@ -12,36 +12,28 @@ from google.cloud import firestore
|
|
| 12 |
# load env data
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
# messenger object
|
| 20 |
messenger = WhatsApp(
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
aai.settings.api_key = os.environ["aai_key"]
|
| 26 |
transcriber = aai.Transcriber()
|
| 27 |
|
| 28 |
-
|
| 29 |
-
# Authenticate to Firesotre with the JSON account key
|
| 30 |
db = firestore.Client.from_service_account_json("firestore-key.json")
|
| 31 |
|
| 32 |
-
|
| 33 |
app = Flask(__name__)
|
| 34 |
|
| 35 |
VERIFY_TOKEN = "30cca545-3838-48b2-80a7-9e43b1ae8ce4"
|
| 36 |
|
| 37 |
-
|
| 38 |
client = openai.OpenAI(
|
| 39 |
api_key=os.environ.get("sambanova_api_key"),
|
| 40 |
base_url="https://api.sambanova.ai/v1",
|
| 41 |
)
|
| 42 |
|
| 43 |
# Interactive button
|
| 44 |
-
# Returned message type: interactive
|
| 45 |
def messenger_button(recipient_phone, message, header='Transaction Confirmation', footer='', btn_name='Confirm Details'):
|
| 46 |
messenger.send_button(
|
| 47 |
recipient_id=recipient_phone,
|
|
@@ -56,11 +48,7 @@ def messenger_button(recipient_phone, message, header='Transaction Confirmation'
|
|
| 56 |
"title": "iBank",
|
| 57 |
"rows": [
|
| 58 |
{"id": "confirm", "title": "Record Transaction", "description": ""},
|
| 59 |
-
{
|
| 60 |
-
"id": "cancel",
|
| 61 |
-
"title": "Cancel Transaction",
|
| 62 |
-
"description": "",
|
| 63 |
-
},
|
| 64 |
],
|
| 65 |
}
|
| 66 |
],
|
|
@@ -94,14 +82,13 @@ def messenger_reply_button(recipient_phone, message):
|
|
| 94 |
}
|
| 95 |
]
|
| 96 |
}
|
| 97 |
-
|
| 98 |
)
|
| 99 |
|
| 100 |
-
def respond(query_str:str):
|
| 101 |
response = "hello, I don't have a brain yet"
|
| 102 |
return response
|
| 103 |
|
| 104 |
-
|
| 105 |
@app.route("/", methods=["GET", "POST"])
|
| 106 |
def hook():
|
| 107 |
if request.method == "GET":
|
|
@@ -116,17 +103,16 @@ def hook():
|
|
| 116 |
# get message update..
|
| 117 |
data = request.get_json()
|
| 118 |
changed_field = messenger.changed_field(data)
|
| 119 |
-
|
| 120 |
if changed_field == "messages":
|
| 121 |
new_message = messenger.get_mobile(data)
|
| 122 |
if new_message:
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
if message_type == "text":
|
| 128 |
message = messenger.get_message(data)
|
| 129 |
-
|
| 130 |
elif message_type == "audio":
|
| 131 |
audio = messenger.get_audio(data)
|
| 132 |
audio_id, mime_type = audio["id"], audio["mime_type"]
|
|
@@ -136,57 +122,45 @@ def hook():
|
|
| 136 |
print(audio_filename)
|
| 137 |
print(transcript.text)
|
| 138 |
message = transcript.text
|
| 139 |
-
messenger.send_message(message=f"Transcribed message:\n {message}", recipient_id=mobile)
|
| 140 |
logging.info(f"\nAudio: {audio}\n")
|
| 141 |
-
|
| 142 |
else:
|
| 143 |
-
messenger.send_message(message="Please send me text or audio messages",recipient_id=mobile)
|
| 144 |
return None
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
else:
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
# messenger.send_message(message=f"{response} \n\n {parsed_trans_data}", recipient_id=mobile)
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
# print("Response:", response)
|
| 159 |
-
logging.info(f"\nAnswer: {response}\n")
|
| 160 |
-
|
| 161 |
-
# Send confirmation button
|
| 162 |
-
# messenger_button(mobile, response)
|
| 163 |
-
# messenger_reply_button(mobile, response)
|
| 164 |
-
|
| 165 |
-
intent = parsed_trans_data[0]['intent'].lower()
|
| 166 |
-
trans_type = parsed_trans_data[0]['transaction_type'].lower()
|
| 167 |
-
if intent == 'create':
|
| 168 |
-
if trans_type == 'purchase':
|
| 169 |
-
if create_inventory(mobile, parsed_trans_data):
|
| 170 |
-
firestore_msg = "Transaction recorded successfully!"
|
| 171 |
-
else:
|
| 172 |
-
firestore_msg = "Sorry, could not record transaction!"
|
| 173 |
-
|
| 174 |
-
elif trans_type == 'sale':
|
| 175 |
-
if create_sale(mobile, parsed_trans_data):
|
| 176 |
-
firestore_msg = "Transaction recorded successfully!"
|
| 177 |
-
else:
|
| 178 |
-
firestore_msg = "Sorry, could not record transaction!"
|
| 179 |
-
|
| 180 |
-
elif intent == 'update': # handle update
|
| 181 |
-
pass
|
| 182 |
-
elif intent == 'delete': # handle delete
|
| 183 |
-
pass
|
| 184 |
-
else:
|
| 185 |
-
firestore_msg = f'The detected intent, {intent}, is not currently supported!'
|
| 186 |
-
|
| 187 |
-
messenger.send_message(message=f"{response},\n\n {firestore_msg}", recipient_id=mobile)
|
| 188 |
|
| 189 |
return "ok"
|
| 190 |
|
| 191 |
if __name__ == '__main__':
|
| 192 |
-
|
|
|
|
| 1 |
+
from flask import Flask, request, make_response
|
| 2 |
import os
|
| 3 |
import logging
|
| 4 |
from dotenv import load_dotenv
|
|
|
|
| 12 |
# load env data
|
| 13 |
load_dotenv()
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# messenger object
|
| 16 |
messenger = WhatsApp(
|
| 17 |
+
os.environ["whatsapp_token"],
|
| 18 |
+
phone_number_id=os.environ["phone_number_id"]
|
| 19 |
+
)
|
| 20 |
|
| 21 |
aai.settings.api_key = os.environ["aai_key"]
|
| 22 |
transcriber = aai.Transcriber()
|
| 23 |
|
| 24 |
+
# Authenticate to Firestore with the JSON account key
|
|
|
|
| 25 |
db = firestore.Client.from_service_account_json("firestore-key.json")
|
| 26 |
|
|
|
|
| 27 |
app = Flask(__name__)
|
| 28 |
|
| 29 |
VERIFY_TOKEN = "30cca545-3838-48b2-80a7-9e43b1ae8ce4"
|
| 30 |
|
|
|
|
| 31 |
client = openai.OpenAI(
|
| 32 |
api_key=os.environ.get("sambanova_api_key"),
|
| 33 |
base_url="https://api.sambanova.ai/v1",
|
| 34 |
)
|
| 35 |
|
| 36 |
# Interactive button
|
|
|
|
| 37 |
def messenger_button(recipient_phone, message, header='Transaction Confirmation', footer='', btn_name='Confirm Details'):
|
| 38 |
messenger.send_button(
|
| 39 |
recipient_id=recipient_phone,
|
|
|
|
| 48 |
"title": "iBank",
|
| 49 |
"rows": [
|
| 50 |
{"id": "confirm", "title": "Record Transaction", "description": ""},
|
| 51 |
+
{"id": "cancel", "title": "Cancel Transaction", "description": ""},
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
],
|
| 53 |
}
|
| 54 |
],
|
|
|
|
| 82 |
}
|
| 83 |
]
|
| 84 |
}
|
| 85 |
+
},
|
| 86 |
)
|
| 87 |
|
| 88 |
+
def respond(query_str: str):
|
| 89 |
response = "hello, I don't have a brain yet"
|
| 90 |
return response
|
| 91 |
|
|
|
|
| 92 |
@app.route("/", methods=["GET", "POST"])
|
| 93 |
def hook():
|
| 94 |
if request.method == "GET":
|
|
|
|
| 103 |
# get message update..
|
| 104 |
data = request.get_json()
|
| 105 |
changed_field = messenger.changed_field(data)
|
| 106 |
+
|
| 107 |
if changed_field == "messages":
|
| 108 |
new_message = messenger.get_mobile(data)
|
| 109 |
if new_message:
|
| 110 |
+
mobile = messenger.get_mobile(data)
|
| 111 |
+
message_type = messenger.get_message_type(data)
|
| 112 |
+
|
|
|
|
| 113 |
if message_type == "text":
|
| 114 |
message = messenger.get_message(data)
|
| 115 |
+
|
| 116 |
elif message_type == "audio":
|
| 117 |
audio = messenger.get_audio(data)
|
| 118 |
audio_id, mime_type = audio["id"], audio["mime_type"]
|
|
|
|
| 122 |
print(audio_filename)
|
| 123 |
print(transcript.text)
|
| 124 |
message = transcript.text
|
| 125 |
+
messenger.send_message(message=f"Transcribed message:\n {message}", recipient_id=mobile)
|
| 126 |
logging.info(f"\nAudio: {audio}\n")
|
| 127 |
+
|
| 128 |
else:
|
| 129 |
+
messenger.send_message(message="Please send me text or audio messages", recipient_id=mobile)
|
| 130 |
return None
|
| 131 |
+
|
| 132 |
+
# Handle greetings
|
| 133 |
+
if message.lower() in ("hi", "hello", "help", "how are you"):
|
| 134 |
+
response = "Hi there! My name is SmartLedger. How can I help you today?"
|
| 135 |
+
messenger.send_message(message=f"{response}", recipient_id=mobile)
|
| 136 |
+
else:
|
| 137 |
+
response = str(generateResponse(message))
|
| 138 |
+
parsed_trans_data = parse_multiple_transactions(response)
|
| 139 |
+
logging.info(f"\nAnswer: {response}\n")
|
| 140 |
+
|
| 141 |
+
intent = parsed_trans_data[0]['intent'].lower()
|
| 142 |
+
trans_type = parsed_trans_data[0]['transaction_type'].lower()
|
| 143 |
+
if intent == 'create':
|
| 144 |
+
if trans_type == 'purchase':
|
| 145 |
+
if create_inventory(mobile, parsed_trans_data):
|
| 146 |
+
firestore_msg = "Transaction recorded successfully!"
|
| 147 |
+
else:
|
| 148 |
+
firestore_msg = "Sorry, could not record transaction!"
|
| 149 |
+
elif trans_type == 'sale':
|
| 150 |
+
if create_sale(mobile, parsed_trans_data):
|
| 151 |
+
firestore_msg = "Transaction recorded successfully!"
|
| 152 |
+
else:
|
| 153 |
+
firestore_msg = "Sorry, could not record transaction!"
|
| 154 |
+
elif intent == 'update':
|
| 155 |
+
pass
|
| 156 |
+
elif intent == 'delete':
|
| 157 |
+
pass
|
| 158 |
else:
|
| 159 |
+
firestore_msg = f'The detected intent, {intent}, is not currently supported!'
|
| 160 |
+
|
| 161 |
+
messenger.send_message(message=f"{response},\n\n {firestore_msg}", recipient_id=mobile)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
return "ok"
|
| 164 |
|
| 165 |
if __name__ == '__main__':
|
| 166 |
+
app.run(debug=True, host="0.0.0.0", port=7860)
|