Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -5,7 +5,7 @@ from dotenv import load_dotenv
|
|
5 |
from heyoo import WhatsApp
|
6 |
import assemblyai as aai
|
7 |
import openai
|
8 |
-
from utility import generateResponse, parse_multiple_transactions, create_inventory, create_sale
|
9 |
from google.cloud import firestore
|
10 |
|
11 |
|
@@ -89,6 +89,42 @@ 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":
|
@@ -112,6 +148,14 @@ def hook():
|
|
112 |
|
113 |
if message_type == "text":
|
114 |
message = messenger.get_message(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
elif message_type == "audio":
|
117 |
audio = messenger.get_audio(data)
|
@@ -122,45 +166,17 @@ def hook():
|
|
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 |
-
|
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 |
-
|
162 |
|
163 |
return "ok"
|
164 |
|
165 |
if __name__ == '__main__':
|
166 |
-
app.run(debug=True, host="0.0.0.0", port=7860)
|
|
|
5 |
from heyoo import WhatsApp
|
6 |
import assemblyai as aai
|
7 |
import openai
|
8 |
+
from utility import generateResponse, parse_multiple_transactions, create_inventory, create_sale, read_datalake
|
9 |
from google.cloud import firestore
|
10 |
|
11 |
|
|
|
89 |
response = "hello, I don't have a brain yet"
|
90 |
return response
|
91 |
|
92 |
+
def process_user_msg(message, mobile):
|
93 |
+
response = str(generateResponse(message))
|
94 |
+
parsed_trans_data = parse_multiple_transactions(response)
|
95 |
+
logging.info(f"\nAnswer: {response}\n")
|
96 |
+
|
97 |
+
intent = parsed_trans_data[0]['intent'].lower()
|
98 |
+
trans_type = parsed_trans_data[0]['transaction_type'].lower()
|
99 |
+
|
100 |
+
if intent == 'create':
|
101 |
+
if trans_type == 'purchase':
|
102 |
+
if create_inventory(mobile, parsed_trans_data):
|
103 |
+
firestore_msg = "Transaction recorded successfully!"
|
104 |
+
else:
|
105 |
+
firestore_msg = "Sorry, could not record transaction!"
|
106 |
+
|
107 |
+
elif trans_type == 'sale':
|
108 |
+
if create_sale(mobile, parsed_trans_data):
|
109 |
+
firestore_msg = "Transaction recorded successfully!"
|
110 |
+
else:
|
111 |
+
firestore_msg = "Sorry, could not record transaction!"
|
112 |
+
|
113 |
+
elif intent == 'update':
|
114 |
+
pass
|
115 |
+
|
116 |
+
elif intent == 'delete':
|
117 |
+
pass
|
118 |
+
|
119 |
+
elif intent == 'read':
|
120 |
+
response = str(read_datalake(mobile, message))
|
121 |
+
firestore_msg = response
|
122 |
+
|
123 |
+
else:
|
124 |
+
firestore_msg = f'The detected intent, {intent}, is not currently supported!'
|
125 |
+
|
126 |
+
return firestore_msg
|
127 |
+
|
128 |
@app.route("/", methods=["GET", "POST"])
|
129 |
def hook():
|
130 |
if request.method == "GET":
|
|
|
148 |
|
149 |
if message_type == "text":
|
150 |
message = messenger.get_message(data)
|
151 |
+
# Handle greetings
|
152 |
+
if message.lower() in ("hi", "hello", "help", "how are you"):
|
153 |
+
response = "Hi there! My name is SmartLedger. How can I help you today?"
|
154 |
+
messenger.send_message(message=f"{response}", recipient_id=mobile)
|
155 |
+
else:
|
156 |
+
firestore_msg = process_user_msg(message, mobile)
|
157 |
+
|
158 |
+
messenger.send_message(message=f"{firestore_msg}", recipient_id=mobile)
|
159 |
|
160 |
elif message_type == "audio":
|
161 |
audio = messenger.get_audio(data)
|
|
|
166 |
print(audio_filename)
|
167 |
print(transcript.text)
|
168 |
message = transcript.text
|
|
|
169 |
logging.info(f"\nAudio: {audio}\n")
|
170 |
+
firestore_msg = process_user_msg(message, mobile)
|
171 |
+
messenger.send_message(message=f"{firestore_msg}", recipient_id=mobile)
|
172 |
|
|
|
|
|
|
|
173 |
|
|
|
|
|
|
|
|
|
174 |
else:
|
175 |
+
messenger.send_message(message="Please send me text or audio messages", recipient_id=mobile)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
+
|
178 |
|
179 |
return "ok"
|
180 |
|
181 |
if __name__ == '__main__':
|
182 |
+
app.run(debug=True, host="0.0.0.0", port=7860)
|