Spaces:
Sleeping
Sleeping
Fix master api call
Browse files
main.py
CHANGED
|
@@ -3,7 +3,7 @@ from fastapi.responses import Response
|
|
| 3 |
import logging
|
| 4 |
import uvicorn
|
| 5 |
from twilio.rest import Client as TW_Client
|
| 6 |
-
import requests
|
| 7 |
from gradio_client import Client as GR_Client, handle_file
|
| 8 |
from helper_functions import get_secret
|
| 9 |
|
|
@@ -30,42 +30,21 @@ AURA_URL = get_secret("AURA_URL")
|
|
| 30 |
|
| 31 |
client = TW_Client(account_sid, auth_token)
|
| 32 |
|
| 33 |
-
# gr_client = GR_Client(AURA_URL, hf_token=HUG_TOKEN)
|
| 34 |
-
|
| 35 |
app = FastAPI()
|
| 36 |
|
| 37 |
-
|
| 38 |
-
# New route to serve the JSON file from /aura_response path
|
| 39 |
-
# @app.get("/aura_response/{filename}")
|
| 40 |
-
# async def get_file(filename: str):
|
| 41 |
-
# file_path = os.path.join(os.getcwd(), filename)
|
| 42 |
-
# if os.path.exists(file_path):
|
| 43 |
-
# return Response(content=open(file_path, "rb").read(), media_type="text/richtext")
|
| 44 |
-
# return {"error": "File not found"}
|
| 45 |
-
|
| 46 |
-
# def call_aura(audio_path):
|
| 47 |
-
# logger.info(f"Calling AURA with audio file: {audio_path}")
|
| 48 |
-
# try:
|
| 49 |
-
# result = gr_client.predict(
|
| 50 |
-
# audio=handle_file(audio_path),
|
| 51 |
-
# llm_name="deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
|
| 52 |
-
# api_name="/get_aura_response"
|
| 53 |
-
# )
|
| 54 |
-
# except Exception as e:
|
| 55 |
-
# eror_msg = f"An unexpected error occurred: {e}"
|
| 56 |
-
# logger.error(eror_msg)
|
| 57 |
-
# raise eror_msg
|
| 58 |
-
# # logger.info(f"AURA response: {result}")
|
| 59 |
-
# return result[0]
|
| 60 |
-
|
| 61 |
-
def call_aura_master(url, payload):
|
| 62 |
headers = {
|
| 63 |
-
"Content-Type": "application/json",
|
| 64 |
"Authorization": f"Bearer {HUG_TOKEN}"
|
| 65 |
}
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
try:
|
| 68 |
-
response = requests.post(url,
|
| 69 |
response.raise_for_status()
|
| 70 |
logger.info(f"Post request successfull")
|
| 71 |
return response
|
|
@@ -74,14 +53,14 @@ def call_aura_master(url, payload):
|
|
| 74 |
raise e
|
| 75 |
|
| 76 |
|
| 77 |
-
def call_add_message(message):
|
| 78 |
url = f"{AURA_URL}/add_message"
|
| 79 |
payload = {
|
| 80 |
"message": message,
|
| 81 |
-
"history": []
|
| 82 |
}
|
| 83 |
|
| 84 |
-
response = call_aura_master(url, payload)
|
| 85 |
history = response.json()["history"]
|
| 86 |
return history
|
| 87 |
|
|
@@ -89,15 +68,15 @@ def call_add_message(message):
|
|
| 89 |
def call_bot(history, system_prompt):
|
| 90 |
url = f"{AURA_URL}/bot"
|
| 91 |
payload = {
|
| 92 |
-
"history": history,
|
| 93 |
"system_prompt": system_prompt
|
| 94 |
}
|
| 95 |
response = call_aura_master(url, payload)
|
| 96 |
-
return response
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
-
@app.post("/
|
| 101 |
async def receive_audio(request: Request):
|
| 102 |
data = await request.form()
|
| 103 |
|
|
@@ -117,52 +96,41 @@ async def receive_audio(request: Request):
|
|
| 117 |
|
| 118 |
print(data.get('MediaUrl0'))
|
| 119 |
print(data.get("MediaContentType0"))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
if media_url:
|
| 122 |
-
|
| 123 |
-
|
| 124 |
|
| 125 |
# # Save audio file
|
| 126 |
-
|
| 127 |
-
# # filename = f"whatsapp_audio.{file_extension}"
|
| 128 |
-
|
| 129 |
# filename = f"whatsapp_audio.{file_extension}"
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
"text": data.get('Body'),
|
| 135 |
-
"files": [media_url]
|
| 136 |
-
}
|
| 137 |
-
else:
|
| 138 |
-
last_message = {
|
| 139 |
-
"text": data.get('Body'),
|
| 140 |
-
"files": []
|
| 141 |
-
}
|
| 142 |
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
-
|
| 145 |
|
| 146 |
-
|
|
|
|
| 147 |
|
| 148 |
print(history)
|
| 149 |
|
| 150 |
# Filter file elements out of history
|
| 151 |
history = [h for h in history if isinstance(h["content"], str)]
|
| 152 |
|
| 153 |
-
|
| 154 |
history,
|
| 155 |
system_prompt="Contexte: Tu es AURA, un expert en prospection et en stratégie commerciale, capable d'analyser les besoins des clients et d'optimiser les approches de vente pour maximiser les résultats.",
|
| 156 |
)
|
| 157 |
-
|
| 158 |
-
print(result)
|
| 159 |
-
|
| 160 |
-
message = client.messages.create(
|
| 161 |
-
from_=from_whatsapp_number,
|
| 162 |
-
body=result[:1580], # 1600 charactères max.
|
| 163 |
-
# media_url='https://34ca-64-43-47-200.ngrok-free.app/aura_response/aura_response.json',
|
| 164 |
-
to=to_whatsapp_number
|
| 165 |
-
)
|
| 166 |
|
| 167 |
# if media_url:
|
| 168 |
# media_type = data.get("MediaContentType0")
|
|
@@ -206,7 +174,20 @@ async def receive_audio(request: Request):
|
|
| 206 |
|
| 207 |
# os.remove(filename)
|
| 208 |
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
if __name__ == "__main__":
|
| 212 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 3 |
import logging
|
| 4 |
import uvicorn
|
| 5 |
from twilio.rest import Client as TW_Client
|
| 6 |
+
import requests, json
|
| 7 |
from gradio_client import Client as GR_Client, handle_file
|
| 8 |
from helper_functions import get_secret
|
| 9 |
|
|
|
|
| 30 |
|
| 31 |
client = TW_Client(account_sid, auth_token)
|
| 32 |
|
|
|
|
|
|
|
| 33 |
app = FastAPI()
|
| 34 |
|
| 35 |
+
def call_aura_master(url, payload, file=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
headers = {
|
| 37 |
+
# "Content-Type": "application/json",
|
| 38 |
"Authorization": f"Bearer {HUG_TOKEN}"
|
| 39 |
}
|
| 40 |
|
| 41 |
+
if file:
|
| 42 |
+
logger.info(f"Calling {url} with file: {file}")
|
| 43 |
+
else:
|
| 44 |
+
logger.info(f"Calling {url} with payload: {payload}")
|
| 45 |
+
|
| 46 |
try:
|
| 47 |
+
response = requests.post(url, data=payload, headers=headers, files=file)
|
| 48 |
response.raise_for_status()
|
| 49 |
logger.info(f"Post request successfull")
|
| 50 |
return response
|
|
|
|
| 53 |
raise e
|
| 54 |
|
| 55 |
|
| 56 |
+
def call_add_message(message, file=None):
|
| 57 |
url = f"{AURA_URL}/add_message"
|
| 58 |
payload = {
|
| 59 |
"message": message,
|
| 60 |
+
"history": json.dumps([])
|
| 61 |
}
|
| 62 |
|
| 63 |
+
response = call_aura_master(url, payload, file)
|
| 64 |
history = response.json()["history"]
|
| 65 |
return history
|
| 66 |
|
|
|
|
| 68 |
def call_bot(history, system_prompt):
|
| 69 |
url = f"{AURA_URL}/bot"
|
| 70 |
payload = {
|
| 71 |
+
"history": json.dumps(history),
|
| 72 |
"system_prompt": system_prompt
|
| 73 |
}
|
| 74 |
response = call_aura_master(url, payload)
|
| 75 |
+
return response
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
+
@app.post("/webhook_whatsapp")
|
| 80 |
async def receive_audio(request: Request):
|
| 81 |
data = await request.form()
|
| 82 |
|
|
|
|
| 96 |
|
| 97 |
print(data.get('MediaUrl0'))
|
| 98 |
print(data.get("MediaContentType0"))
|
| 99 |
+
print(data.get("MediaFileExtension0"))
|
| 100 |
+
print(data.get("Body"))
|
| 101 |
+
|
| 102 |
+
files = None
|
| 103 |
|
| 104 |
if media_url:
|
| 105 |
+
auth = (account_sid, auth_token)
|
| 106 |
+
response = requests.get(media_url, auth=auth)
|
| 107 |
|
| 108 |
# # Save audio file
|
| 109 |
+
file_extension = data.get("MediaContentType0").split("/")[-1]
|
|
|
|
|
|
|
| 110 |
# filename = f"whatsapp_audio.{file_extension}"
|
| 111 |
+
|
| 112 |
+
filename = f"whatsapp_audio.{file_extension}"
|
| 113 |
+
with open(filename, "wb") as audio_file:
|
| 114 |
+
audio_file.write(response.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
files = {
|
| 117 |
+
"file": open(filename, "rb")
|
| 118 |
+
}
|
| 119 |
|
| 120 |
+
history = call_add_message(data.get("Body"), files)
|
| 121 |
|
| 122 |
+
if not history:
|
| 123 |
+
raise ValueError("History is empty")
|
| 124 |
|
| 125 |
print(history)
|
| 126 |
|
| 127 |
# Filter file elements out of history
|
| 128 |
history = [h for h in history if isinstance(h["content"], str)]
|
| 129 |
|
| 130 |
+
response = call_bot(
|
| 131 |
history,
|
| 132 |
system_prompt="Contexte: Tu es AURA, un expert en prospection et en stratégie commerciale, capable d'analyser les besoins des clients et d'optimiser les approches de vente pour maximiser les résultats.",
|
| 133 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
# if media_url:
|
| 136 |
# media_type = data.get("MediaContentType0")
|
|
|
|
| 174 |
|
| 175 |
# os.remove(filename)
|
| 176 |
|
| 177 |
+
if response.status_code != 200:
|
| 178 |
+
message = client.messages.create(
|
| 179 |
+
from_=from_whatsapp_number,
|
| 180 |
+
body="Une erreur s'est produite lors du traitement de votre message. Veuillez réessayer plus tard.",
|
| 181 |
+
to=to_whatsapp_number
|
| 182 |
+
)
|
| 183 |
+
else:
|
| 184 |
+
message = client.messages.create(
|
| 185 |
+
from_=from_whatsapp_number,
|
| 186 |
+
body=response.text[:1599],
|
| 187 |
+
to=to_whatsapp_number
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
return Response(content=json.dumps({"sid": message.sid, "status": message.status, "body": message.body}), media_type="application/json")
|
| 191 |
|
| 192 |
if __name__ == "__main__":
|
| 193 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|