Spaces:
Runtime error
Runtime error
Update app/main.py
Browse files- app/main.py +52 -53
app/main.py
CHANGED
|
@@ -2,82 +2,77 @@ from twilio.rest import Client
|
|
| 2 |
import yaml
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
-
|
| 6 |
-
import
|
| 7 |
-
import
|
|
|
|
| 8 |
from langchain.embeddings import OpenAIEmbeddings
|
| 9 |
from langchain_community.vectorstores import Chroma
|
| 10 |
from helper import retrieve_relevant_context, generate_response_with_context
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
from pathlib import Path
|
| 14 |
file = Path(__file__).resolve()
|
| 15 |
parent, root = file.parent, file.parents[1]
|
| 16 |
sys.path.append(str(root))
|
| 17 |
-
print("str(root)
|
| 18 |
-
print("parent
|
| 19 |
-
|
| 20 |
-
print("CWD :",os.getcwd())
|
| 21 |
|
| 22 |
# Load relevant API Keys
|
| 23 |
file_path = parent / 'Config/API_KEYS.yml'
|
| 24 |
-
|
| 25 |
-
# Define the persist directory
|
| 26 |
persist_directory = str(parent / 'vector_db/chroma_v01/')
|
| 27 |
-
|
| 28 |
-
print("
|
| 29 |
-
print("persist_directory :",str(persist_directory))
|
| 30 |
-
|
| 31 |
|
| 32 |
with open(file_path, 'r') as file:
|
| 33 |
api_keys = yaml.safe_load(file)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
# Extract openai username and key
|
| 37 |
openai_key = api_keys['OPEN_AI']['Key']
|
| 38 |
-
|
| 39 |
os.environ["OPENAI_API_KEY"] = openai_key
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# Extract openai username and key
|
| 44 |
account_sid = api_keys['TWILIO']['account_sid']
|
| 45 |
auth_token = api_keys['TWILIO']['auth_token']
|
| 46 |
-
|
| 47 |
-
account_sid
|
| 48 |
-
auth_token = auth_token
|
| 49 |
-
|
| 50 |
-
print("====account_sid:=====",account_sid)
|
| 51 |
-
# Define the persist directory
|
| 52 |
-
# persist_directory = './vector_db/chroma_v01'
|
| 53 |
|
| 54 |
# Initialize the embeddings model
|
| 55 |
embedding_model = OpenAIEmbeddings()
|
| 56 |
|
| 57 |
-
### Vectorstores
|
| 58 |
-
from langchain_community.vectorstores import Chroma
|
| 59 |
-
|
| 60 |
# Load the Chroma vector store
|
| 61 |
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_model)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
#setup Twilio client
|
| 65 |
client = Client(account_sid, auth_token)
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
print("flask app is running")
|
| 81 |
app = Flask(__name__)
|
| 82 |
|
| 83 |
@app.route("/whatsapp", methods=['GET', 'POST'])
|
|
@@ -85,9 +80,8 @@ def incoming_sms():
|
|
| 85 |
"""Send a dynamic reply to an incoming text message"""
|
| 86 |
# Get the message the user sent our Twilio number
|
| 87 |
body = request.values.get('Body', None)
|
| 88 |
-
print("body
|
| 89 |
|
| 90 |
-
##### Process incoming text #############
|
| 91 |
incoming_msg = body.strip()
|
| 92 |
if not incoming_msg:
|
| 93 |
return str(MessagingResponse())
|
|
@@ -96,15 +90,20 @@ def incoming_sms():
|
|
| 96 |
retrieved_texts = retrieve_relevant_context(vectordb, incoming_msg)
|
| 97 |
context = "\n".join(retrieved_texts)
|
| 98 |
response = generate_response_with_context(incoming_msg, context)
|
| 99 |
-
print("response
|
| 100 |
-
##### Process incoming text Done #############
|
| 101 |
-
|
| 102 |
|
| 103 |
# Start our TwiML response
|
| 104 |
resp = MessagingResponse()
|
| 105 |
-
print("TwiML resp
|
| 106 |
resp.message(response)
|
| 107 |
return str(resp)
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import yaml
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
+
import sys
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from flask import Flask, request, redirect
|
| 8 |
+
from twilio.twiml.messaging_response import MessagingResponse
|
| 9 |
from langchain.embeddings import OpenAIEmbeddings
|
| 10 |
from langchain_community.vectorstores import Chroma
|
| 11 |
from helper import retrieve_relevant_context, generate_response_with_context
|
| 12 |
+
from pyngrok import ngrok
|
| 13 |
|
| 14 |
+
# Setting up paths
|
|
|
|
| 15 |
file = Path(__file__).resolve()
|
| 16 |
parent, root = file.parent, file.parents[1]
|
| 17 |
sys.path.append(str(root))
|
| 18 |
+
print("str(root):", str(root))
|
| 19 |
+
print("parent:", parent)
|
| 20 |
+
print("CWD:", os.getcwd())
|
|
|
|
| 21 |
|
| 22 |
# Load relevant API Keys
|
| 23 |
file_path = parent / 'Config/API_KEYS.yml'
|
|
|
|
|
|
|
| 24 |
persist_directory = str(parent / 'vector_db/chroma_v01/')
|
| 25 |
+
print("file_path:", file_path)
|
| 26 |
+
print("persist_directory:", str(persist_directory))
|
|
|
|
|
|
|
| 27 |
|
| 28 |
with open(file_path, 'r') as file:
|
| 29 |
api_keys = yaml.safe_load(file)
|
| 30 |
|
| 31 |
+
# Extract OpenAI key
|
|
|
|
| 32 |
openai_key = api_keys['OPEN_AI']['Key']
|
|
|
|
| 33 |
os.environ["OPENAI_API_KEY"] = openai_key
|
| 34 |
|
| 35 |
+
# Extract Twilio credentials
|
|
|
|
|
|
|
| 36 |
account_sid = api_keys['TWILIO']['account_sid']
|
| 37 |
auth_token = api_keys['TWILIO']['auth_token']
|
| 38 |
+
twilio_whatsapp_number = api_keys['TWILIO']['whatsapp_number']
|
| 39 |
+
print("====account_sid:=====", account_sid)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# Initialize the embeddings model
|
| 42 |
embedding_model = OpenAIEmbeddings()
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
# Load the Chroma vector store
|
| 45 |
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_model)
|
| 46 |
|
| 47 |
+
# Setup Twilio client
|
|
|
|
| 48 |
client = Client(account_sid, auth_token)
|
| 49 |
|
| 50 |
+
# Example to send a WhatsApp message
|
| 51 |
+
def send_whatsapp_message(to_number, message):
|
| 52 |
+
"""
|
| 53 |
+
Send a WhatsApp message using Twilio.
|
| 54 |
+
|
| 55 |
+
:param to_number: str, recipient's WhatsApp number in the format 'whatsapp:+1234567890'
|
| 56 |
+
:param message: str, message text to send
|
| 57 |
+
"""
|
| 58 |
+
from_number = f'whatsapp:{twilio_whatsapp_number}'
|
| 59 |
+
to_number = f'whatsapp:{to_number}'
|
| 60 |
+
|
| 61 |
+
message = client.messages.create(
|
| 62 |
+
body=message,
|
| 63 |
+
from_=from_number,
|
| 64 |
+
to=to_number
|
| 65 |
+
)
|
| 66 |
+
print(f"Message sent with SID: {message.sid}")
|
| 67 |
+
|
| 68 |
+
# Example usage
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
recipient_number = '+91-9108843322' # Replace with the recipient's WhatsApp number
|
| 71 |
+
text_message = 'Hello from Twilio WhatsApp!'
|
| 72 |
+
send_whatsapp_message(recipient_number, text_message)
|
| 73 |
|
| 74 |
+
# Flask app setup
|
| 75 |
+
print("Flask app is running")
|
|
|
|
| 76 |
app = Flask(__name__)
|
| 77 |
|
| 78 |
@app.route("/whatsapp", methods=['GET', 'POST'])
|
|
|
|
| 80 |
"""Send a dynamic reply to an incoming text message"""
|
| 81 |
# Get the message the user sent our Twilio number
|
| 82 |
body = request.values.get('Body', None)
|
| 83 |
+
print("body:", body)
|
| 84 |
|
|
|
|
| 85 |
incoming_msg = body.strip()
|
| 86 |
if not incoming_msg:
|
| 87 |
return str(MessagingResponse())
|
|
|
|
| 90 |
retrieved_texts = retrieve_relevant_context(vectordb, incoming_msg)
|
| 91 |
context = "\n".join(retrieved_texts)
|
| 92 |
response = generate_response_with_context(incoming_msg, context)
|
| 93 |
+
print("response:", response)
|
|
|
|
|
|
|
| 94 |
|
| 95 |
# Start our TwiML response
|
| 96 |
resp = MessagingResponse()
|
| 97 |
+
print("TwiML resp:", resp)
|
| 98 |
resp.message(response)
|
| 99 |
return str(resp)
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
+
# Start ngrok tunnel
|
| 103 |
+
ngrok_tunnel = ngrok.connect(5000)
|
| 104 |
+
print(f"ngrok tunnel 'http' URL: {ngrok_tunnel.public_url}")
|
| 105 |
+
|
| 106 |
+
# Print the ngrok URL so you can set it in Twilio webhook
|
| 107 |
+
print("ngrok URL:", ngrok_tunnel.public_url + "/whatsapp")
|
| 108 |
+
|
| 109 |
+
app.run(host='0.0.0.0', port=5000, debug=True)
|