Spaces:
Runtime error
Runtime error
Update app/main.py
Browse files- app/main.py +82 -82
app/main.py
CHANGED
|
@@ -1,82 +1,82 @@
|
|
| 1 |
-
from twilio.rest import Client
|
| 2 |
-
import yaml
|
| 3 |
-
import json
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
import yaml
|
| 7 |
-
import json
|
| 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 |
-
# Load relevant API Keys
|
| 13 |
-
file_path = '
|
| 14 |
-
|
| 15 |
-
with open(file_path, 'r') as file:
|
| 16 |
-
api_keys = yaml.safe_load(file)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# Extract openai username and key
|
| 20 |
-
openai_key = api_keys['OPEN_AI']['Key']
|
| 21 |
-
|
| 22 |
-
os.environ["OPENAI_API_KEY"] = openai_key
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Extract openai username and key
|
| 27 |
-
account_sid = api_keys['TWILIO']['account_sid']
|
| 28 |
-
auth_token = api_keys['TWILIO']['auth_token']
|
| 29 |
-
|
| 30 |
-
account_sid = account_sid
|
| 31 |
-
auth_token = auth_token
|
| 32 |
-
|
| 33 |
-
# Define the persist directory
|
| 34 |
-
persist_directory = '
|
| 35 |
-
|
| 36 |
-
# Initialize the embeddings model
|
| 37 |
-
embedding_model = OpenAIEmbeddings()
|
| 38 |
-
|
| 39 |
-
### Vectorstores
|
| 40 |
-
from langchain_community.vectorstores import Chroma
|
| 41 |
-
|
| 42 |
-
# Load the Chroma vector store
|
| 43 |
-
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_model)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
#setup Twilio client
|
| 47 |
-
client = Client(account_sid, auth_token)
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
from flask import Flask, request, redirect
|
| 51 |
-
from twilio.twiml.messaging_response import MessagingResponse
|
| 52 |
-
print("flask app is running")
|
| 53 |
-
app = Flask(__name__)
|
| 54 |
-
|
| 55 |
-
@app.route("/whatsapp", methods=['GET', 'POST'])
|
| 56 |
-
def incoming_sms():
|
| 57 |
-
"""Send a dynamic reply to an incoming text message"""
|
| 58 |
-
# Get the message the user sent our Twilio number
|
| 59 |
-
body = request.values.get('Body', None)
|
| 60 |
-
print("body :",body)
|
| 61 |
-
|
| 62 |
-
##### Process incoming text #############
|
| 63 |
-
incoming_msg = body.strip()
|
| 64 |
-
if not incoming_msg:
|
| 65 |
-
return str(MessagingResponse())
|
| 66 |
-
|
| 67 |
-
# Generate response using the RAG-powered system
|
| 68 |
-
retrieved_texts = retrieve_relevant_context(vectordb, incoming_msg)
|
| 69 |
-
context = "\n".join(retrieved_texts)
|
| 70 |
-
response = generate_response_with_context(incoming_msg, context)
|
| 71 |
-
print("response :",response)
|
| 72 |
-
##### Process incoming text Done #############
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
# Start our TwiML response
|
| 76 |
-
resp = MessagingResponse()
|
| 77 |
-
print("TwiML resp :", resp)
|
| 78 |
-
resp.message(response)
|
| 79 |
-
return str(resp)
|
| 80 |
-
|
| 81 |
-
if __name__ == "__main__":
|
| 82 |
-
app.run(port=5000, debug=True)
|
|
|
|
| 1 |
+
from twilio.rest import Client
|
| 2 |
+
import yaml
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
import yaml
|
| 7 |
+
import json
|
| 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 |
+
# Load relevant API Keys
|
| 13 |
+
file_path = '../Config/API_KEYS.yml'
|
| 14 |
+
|
| 15 |
+
with open(file_path, 'r') as file:
|
| 16 |
+
api_keys = yaml.safe_load(file)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Extract openai username and key
|
| 20 |
+
openai_key = api_keys['OPEN_AI']['Key']
|
| 21 |
+
|
| 22 |
+
os.environ["OPENAI_API_KEY"] = openai_key
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# Extract openai username and key
|
| 27 |
+
account_sid = api_keys['TWILIO']['account_sid']
|
| 28 |
+
auth_token = api_keys['TWILIO']['auth_token']
|
| 29 |
+
|
| 30 |
+
account_sid = account_sid
|
| 31 |
+
auth_token = auth_token
|
| 32 |
+
|
| 33 |
+
# Define the persist directory
|
| 34 |
+
persist_directory = '../vector_db/chroma_v01'
|
| 35 |
+
|
| 36 |
+
# Initialize the embeddings model
|
| 37 |
+
embedding_model = OpenAIEmbeddings()
|
| 38 |
+
|
| 39 |
+
### Vectorstores
|
| 40 |
+
from langchain_community.vectorstores import Chroma
|
| 41 |
+
|
| 42 |
+
# Load the Chroma vector store
|
| 43 |
+
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_model)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
#setup Twilio client
|
| 47 |
+
client = Client(account_sid, auth_token)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
from flask import Flask, request, redirect
|
| 51 |
+
from twilio.twiml.messaging_response import MessagingResponse
|
| 52 |
+
print("flask app is running")
|
| 53 |
+
app = Flask(__name__)
|
| 54 |
+
|
| 55 |
+
@app.route("/whatsapp", methods=['GET', 'POST'])
|
| 56 |
+
def incoming_sms():
|
| 57 |
+
"""Send a dynamic reply to an incoming text message"""
|
| 58 |
+
# Get the message the user sent our Twilio number
|
| 59 |
+
body = request.values.get('Body', None)
|
| 60 |
+
print("body :",body)
|
| 61 |
+
|
| 62 |
+
##### Process incoming text #############
|
| 63 |
+
incoming_msg = body.strip()
|
| 64 |
+
if not incoming_msg:
|
| 65 |
+
return str(MessagingResponse())
|
| 66 |
+
|
| 67 |
+
# Generate response using the RAG-powered system
|
| 68 |
+
retrieved_texts = retrieve_relevant_context(vectordb, incoming_msg)
|
| 69 |
+
context = "\n".join(retrieved_texts)
|
| 70 |
+
response = generate_response_with_context(incoming_msg, context)
|
| 71 |
+
print("response :",response)
|
| 72 |
+
##### Process incoming text Done #############
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# Start our TwiML response
|
| 76 |
+
resp = MessagingResponse()
|
| 77 |
+
print("TwiML resp :", resp)
|
| 78 |
+
resp.message(response)
|
| 79 |
+
return str(resp)
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
app.run(port=5000, debug=True)
|