Skip to content

Commit

Permalink
Merge pull request #109 from SunbirdAI/whatsapp_class
Browse files Browse the repository at this point in the history
Removing audio auto detection
  • Loading branch information
yigagilbert authored Jan 10, 2025
2 parents ae204c1 + ff7f8f6 commit 9c1529e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 92 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ __pycache__/
*.py[cod]
test.db
.coverage
.pylintrc
env/
key.json

Expand Down
121 changes: 46 additions & 75 deletions app/inference_services/whatsapp_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Any, List, Dict, Union
import requests
from datetime import datetime
from fastapi import APIRouter, HTTPException
from fastapi import HTTPException
from dotenv import load_dotenv
from requests_toolbelt import MultipartEncoder
import runpod
Expand All @@ -32,35 +32,6 @@ def __init__(self, token: str, phone_number_id: str):
"Content-Type": "application/json",
}

# def send_message(self, recipient_id: str, message: str, preview_url: bool = True) -> Union[Dict, None]:
# url = f"{self.base_url}/{self.phone_number_id}/messages"
# data = {
# "messaging_product": "whatsapp",
# "to": recipient_id,
# "text": {"preview_url": preview_url, "body": message},
# }
# logging.info(f"Sending message to {recipient_id}")
# response = requests.post(url, headers=self.headers, json=data)
# if response.status_code == 200:
# logging.info("Message sent successfully.")
# return response.json()
# else:
# logging.error(f"Failed to send message: {response.text}")
# return None

# def download_media(self, media_url: str, save_path: str) -> Union[str, None]:
# response = requests.get(media_url, headers=self.headers)
# if response.status_code == 200:
# with open(save_path, "wb") as file:
# file.write(response.content)
# logging.info(f"Media downloaded successfully to {save_path}.")
# return save_path
# else:
# logging.error(f"Failed to download media: {response.text}")
# return None

#######################################################################

def download_whatsapp_audio(self, url, access_token):
try:
# Generate a random string and get the current timestamp
Expand All @@ -79,7 +50,7 @@ def download_whatsapp_audio(self, url, access_token):
with open(local_audio_path, "wb") as f:
f.write(response.content)

logging.info(f"Whatsapp audio download was successfull: {local_audio_path}")
logging.info(f"Whatsapp audio download was successfull: {local_audio_path}") # pylint: disable=logging-fstring-interpolation
return local_audio_path
else:
raise HTTPException(status_code=500, detail="Failed to download audio file")
Expand Down Expand Up @@ -1267,51 +1238,51 @@ def handle_openai_message(
# Step 5: Initialize the Runpod endpoint for transcription
endpoint = runpod.Endpoint(os.getenv("RUNPOD_ENDPOINT_ID"))

logging.info("Audio data found for langauge detection")
data = {
"input": {
"task": "auto_detect_audio_language",
"audio_file": blob_name,
}
}

start_time = time.time()

try:
logging.info("Audio file ready for langauge detection")
audio_lang_response = call_endpoint_with_retry(endpoint, data)
except TimeoutError as e:

logging.error("Job timed out %s", str(e))
raise HTTPException(
status_code=503, detail="Service unavailable due to timeout."
) from e

except ConnectionError as e:

logging.error("Connection lost: %s", str(e))
raise HTTPException(
status_code=503, detail="Service unavailable due to connection error."
) from e

end_time = time.time()
logging.info(
"Audio language auto detection response: %s ",
audio_lang_response.get("detected_language"),
)

# Calculate the elapsed time
elapsed_time = end_time - start_time
logging.info(
"Audio language auto detection elapsed time: %s seconds", elapsed_time
)

audio_language = audio_lang_response.get("detected_language")
# logging.info("Audio data found for langauge detection")
# data = {
# "input": {
# "task": "auto_detect_audio_language",
# "audio_file": blob_name,
# }
# }

# start_time = time.time()

# try:
# logging.info("Audio file ready for langauge detection")
# audio_lang_response = call_endpoint_with_retry(endpoint, data)
# except TimeoutError as e:

# logging.error("Job timed out %s", str(e))
# raise HTTPException(
# status_code=503, detail="Service unavailable due to timeout."
# ) from e

# except ConnectionError as e:

# logging.error("Connection lost: %s", str(e))
# raise HTTPException(
# status_code=503, detail="Service unavailable due to connection error."
# ) from e

# end_time = time.time()
# logging.info(
# "Audio language auto detection response: %s ",
# audio_lang_response.get("detected_language"),
# )

# # Calculate the elapsed time
# elapsed_time = end_time - start_time
# logging.info(
# "Audio language auto detection elapsed time: %s seconds", elapsed_time
# )

# audio_language = audio_lang_response.get("detected_language")
request_response = {}

if audio_language in language_mapping:
if target_language in language_mapping:
# Language is in the mapping
logging.info("Language detected in audio is %s", audio_language)
logging.info("Language detected in audio is %s", target_language)
else:
# Language is not in our scope
return "Audio Language not detected"
Expand All @@ -1334,8 +1305,8 @@ def handle_openai_message(
{
"input": {
"task": "transcribe",
"target_lang": audio_language,
"adapter": audio_language,
"target_lang": target_language,
"adapter": target_language,
"audio_file": blob_name, # Corrected to pass local file path
"recognise_speakers": False,
}
Expand Down
18 changes: 1 addition & 17 deletions app/routers/tasks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import datetime
import json
import logging
import mimetypes
import os
import re
import shutil
import time
import uuid

import requests
import runpod
from dotenv import load_dotenv
from fastapi import APIRouter, Depends, File, Form, HTTPException, Request, UploadFile
Expand All @@ -22,25 +18,13 @@
stop_after_attempt,
wait_exponential,
)
from twilio.rest import Client
from werkzeug.utils import secure_filename

from app.crud.audio_transcription import create_audio_transcription
from app.crud.monitoring import log_endpoint
from app.deps import get_current_user, get_db
from app.inference_services.openai_script import (
classify_input,
get_completion_from_messages,
get_guide_based_on_classification,
is_json,
)
from app.inference_services.user_preference import (
get_user_last_five_messages,
get_user_preference,
save_message,
save_translation,
save_user_preference,
update_feedback,
get_user_preference
)
from app.schemas.tasks import (
AudioDetectedLanguageResponse,
Expand Down

0 comments on commit 9c1529e

Please sign in to comment.