Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import base64
|
|
@@ -8,6 +10,11 @@ from pydub import AudioSegment
|
|
| 8 |
|
| 9 |
#---------------------------------------------------------------------------------------------------------------------------------
|
| 10 |
def read_audio_file(file_path: str) -> bytes:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
try:
|
| 12 |
with open(file_path, "rb") as audio_file:
|
| 13 |
return audio_file.read()
|
|
@@ -15,6 +22,11 @@ def read_audio_file(file_path: str) -> bytes:
|
|
| 15 |
raise IOError(f"Erreur de lecture du fichier audio : {e}")
|
| 16 |
#---------------------------------------------------------------------------------------------------------------------------------
|
| 17 |
def prepare_config(diarization: bool = False, word_time_offsets: bool = False) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
return {
|
| 19 |
"add_speaker_diarization_to_config": diarization,
|
| 20 |
"enable_word_time_offsets": word_time_offsets,
|
|
@@ -22,6 +34,11 @@ def prepare_config(diarization: bool = False, word_time_offsets: bool = False) -
|
|
| 22 |
}
|
| 23 |
#---------------------------------------------------------------------------------------------------------------------------------
|
| 24 |
def transcribe_audio(url: str, api_key: str, audio_content: bytes, config: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
headers = {"X-Ovh-Application": api_key}
|
| 26 |
|
| 27 |
config_json = json.dumps(config)
|
|
@@ -33,21 +50,29 @@ def transcribe_audio(url: str, api_key: str, audio_content: bytes, config: Dict[
|
|
| 33 |
|
| 34 |
try:
|
| 35 |
response = requests.post(url, files=files, headers=headers)
|
| 36 |
-
response.raise_for_status()
|
| 37 |
return response.json()
|
| 38 |
except requests.exceptions.RequestException as e:
|
| 39 |
raise Exception(f"Erreur lors de la requête à l'API : {e}")
|
| 40 |
#---------------------------------------------------------------------------------------------------------------------------------
|
| 41 |
def process_transcription(transcription: Dict[str, Any]) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
full_transcript = ""
|
| 43 |
for sentence in transcription:
|
| 44 |
for alternative in sentence["alternatives"]:
|
| 45 |
full_transcript += alternative["transcript"] + " "
|
| 46 |
return full_transcript.strip()
|
| 47 |
#---------------------------------------------------------------------------------------------------------------------------------
|
| 48 |
-
# llm_choices = ["Llama-3-70B-Instruct", "Llama-3-8B-Instruct", "Mistral-7B-Instruct", "Mixtral-8x22b-Instruct", "Mixtral-8x7b-instruct"]
|
| 49 |
-
|
| 50 |
def summary(content: str, choice: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
if choice == "Llama-3-70B-Instruct":
|
| 52 |
url = "https://llama-3-70b-instruct.endpoints.kepler.ai.cloud.ovh.net/api/openai_compat/v1/chat/completions"
|
| 53 |
model="Meta-Llama-3-70B-Instruct"
|
|
@@ -96,6 +121,11 @@ def summary(content: str, choice: str) -> str:
|
|
| 96 |
return f"Error: {response.status_code}"
|
| 97 |
#---------------------------------------------------------------------------------------------------------------------------------
|
| 98 |
def choose_language(choice: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
languages = {
|
| 100 |
"fr": "https://nvr-asr-fr-fr.endpoints.kepler.ai.cloud.ovh.net/api/v1/asr/recognize",
|
| 101 |
"en": "https://nvr-asr-en-gb.endpoints.kepler.ai.cloud.ovh.net/api/v1/asr/recognize",
|
|
@@ -107,18 +137,28 @@ def choose_language(choice: str) -> str:
|
|
| 107 |
return languages.get(choice, "https://nvr-asr-fr-fr.endpoints.kepler.ai.cloud.ovh.net/api/v1/asr/recognize")
|
| 108 |
#---------------------------------------------------------------------------------------------------------------------------------
|
| 109 |
def convert_audio_to_16000hz(input_file: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
try:
|
| 111 |
audio = AudioSegment.from_file(input_file)
|
| 112 |
audio = audio.set_channels(1).set_frame_rate(16000)
|
| 113 |
-
output_file = "
|
| 114 |
audio.export(output_file, format="wav")
|
| 115 |
return output_file
|
| 116 |
except Exception as e:
|
| 117 |
raise Exception(f"Erreur lors de la conversion du fichier audio : {e}")
|
| 118 |
#---------------------------------------------------------------------------------------------------------------------------------
|
| 119 |
def transcribe_and_summarize(audio_file, language_choice, llm_choice):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
try:
|
| 121 |
-
api_key = os.environ.get("OVH_API_KEY", "")
|
| 122 |
processed_audio = convert_audio_to_16000hz(audio_file)
|
| 123 |
audio_content = read_audio_file(processed_audio)
|
| 124 |
url = choose_language(language_choice)
|
|
@@ -132,6 +172,7 @@ def transcribe_and_summarize(audio_file, language_choice, llm_choice):
|
|
| 132 |
return f"Une erreur est survenue : {e}", ""
|
| 133 |
#---------------------------------------------------------------------------------------------------------------------------------
|
| 134 |
# Gradio Interface
|
|
|
|
| 135 |
language_choices = ["fr", "en", "en/us", "de", "es", "it"]
|
| 136 |
llm_choices = ["Llama-3-70B-Instruct", "Llama-3-8B-Instruct", "Mistral-7B-Instruct", "Mixtral-8x22b-Instruct", "Mixtral-8x7b-instruct"]
|
| 137 |
|
|
|
|
| 1 |
+
# Import
|
| 2 |
+
#---------------------------------------------------------------------------------------------------------------------------------
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
import base64
|
|
|
|
| 10 |
|
| 11 |
#---------------------------------------------------------------------------------------------------------------------------------
|
| 12 |
def read_audio_file(file_path: str) -> bytes:
|
| 13 |
+
"""
|
| 14 |
+
Function used to read audio file
|
| 15 |
+
input : filepath
|
| 16 |
+
output : data bytes
|
| 17 |
+
"""
|
| 18 |
try:
|
| 19 |
with open(file_path, "rb") as audio_file:
|
| 20 |
return audio_file.read()
|
|
|
|
| 22 |
raise IOError(f"Erreur de lecture du fichier audio : {e}")
|
| 23 |
#---------------------------------------------------------------------------------------------------------------------------------
|
| 24 |
def prepare_config(diarization: bool = False, word_time_offsets: bool = False) -> Dict[str, Any]:
|
| 25 |
+
"""
|
| 26 |
+
Function used to configure transcription
|
| 27 |
+
input : diarization parameter, word time offsets parameter, add word boosting to config parameter
|
| 28 |
+
output : transcription configuration
|
| 29 |
+
"""
|
| 30 |
return {
|
| 31 |
"add_speaker_diarization_to_config": diarization,
|
| 32 |
"enable_word_time_offsets": word_time_offsets,
|
|
|
|
| 34 |
}
|
| 35 |
#---------------------------------------------------------------------------------------------------------------------------------
|
| 36 |
def transcribe_audio(url: str, api_key: str, audio_content: bytes, config: Dict[str, Any]) -> Dict[str, Any]:
|
| 37 |
+
"""
|
| 38 |
+
Function used to transcribe audio
|
| 39 |
+
input : url of the OVHCloud api AI endpoint, api key to access ai endpoint, trancription configuration
|
| 40 |
+
output : response that contains the audio transcription
|
| 41 |
+
"""
|
| 42 |
headers = {"X-Ovh-Application": api_key}
|
| 43 |
|
| 44 |
config_json = json.dumps(config)
|
|
|
|
| 50 |
|
| 51 |
try:
|
| 52 |
response = requests.post(url, files=files, headers=headers)
|
| 53 |
+
response.raise_for_status()
|
| 54 |
return response.json()
|
| 55 |
except requests.exceptions.RequestException as e:
|
| 56 |
raise Exception(f"Erreur lors de la requête à l'API : {e}")
|
| 57 |
#---------------------------------------------------------------------------------------------------------------------------------
|
| 58 |
def process_transcription(transcription: Dict[str, Any]) -> str:
|
| 59 |
+
"""
|
| 60 |
+
Function used to extract audio transcription from api response
|
| 61 |
+
input : response from the OVHcloud api endpoint
|
| 62 |
+
output : transcription text
|
| 63 |
+
"""
|
| 64 |
full_transcript = ""
|
| 65 |
for sentence in transcription:
|
| 66 |
for alternative in sentence["alternatives"]:
|
| 67 |
full_transcript += alternative["transcript"] + " "
|
| 68 |
return full_transcript.strip()
|
| 69 |
#---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
| 70 |
def summary(content: str, choice: str) -> str:
|
| 71 |
+
"""
|
| 72 |
+
Function used to summary the transcription text by using a llm model on OVHcloud api AI endpoint
|
| 73 |
+
input : content to summary, llm choice
|
| 74 |
+
output : transcription text
|
| 75 |
+
"""
|
| 76 |
if choice == "Llama-3-70B-Instruct":
|
| 77 |
url = "https://llama-3-70b-instruct.endpoints.kepler.ai.cloud.ovh.net/api/openai_compat/v1/chat/completions"
|
| 78 |
model="Meta-Llama-3-70B-Instruct"
|
|
|
|
| 121 |
return f"Error: {response.status_code}"
|
| 122 |
#---------------------------------------------------------------------------------------------------------------------------------
|
| 123 |
def choose_language(choice: str) -> str:
|
| 124 |
+
"""
|
| 125 |
+
Function used to choose the language of the audio track
|
| 126 |
+
input : language code
|
| 127 |
+
output : url of the OVHcloud api AI endpoint
|
| 128 |
+
"""
|
| 129 |
languages = {
|
| 130 |
"fr": "https://nvr-asr-fr-fr.endpoints.kepler.ai.cloud.ovh.net/api/v1/asr/recognize",
|
| 131 |
"en": "https://nvr-asr-en-gb.endpoints.kepler.ai.cloud.ovh.net/api/v1/asr/recognize",
|
|
|
|
| 137 |
return languages.get(choice, "https://nvr-asr-fr-fr.endpoints.kepler.ai.cloud.ovh.net/api/v1/asr/recognize")
|
| 138 |
#---------------------------------------------------------------------------------------------------------------------------------
|
| 139 |
def convert_audio_to_16000hz(input_file: str) -> str:
|
| 140 |
+
"""
|
| 141 |
+
Function used to convert audio input file to a the valid input format
|
| 142 |
+
input :audio input file
|
| 143 |
+
output : audio output file with valid format
|
| 144 |
+
"""
|
| 145 |
try:
|
| 146 |
audio = AudioSegment.from_file(input_file)
|
| 147 |
audio = audio.set_channels(1).set_frame_rate(16000)
|
| 148 |
+
output_file = "temp.wav"
|
| 149 |
audio.export(output_file, format="wav")
|
| 150 |
return output_file
|
| 151 |
except Exception as e:
|
| 152 |
raise Exception(f"Erreur lors de la conversion du fichier audio : {e}")
|
| 153 |
#---------------------------------------------------------------------------------------------------------------------------------
|
| 154 |
def transcribe_and_summarize(audio_file, language_choice, llm_choice):
|
| 155 |
+
"""
|
| 156 |
+
Main function of the app
|
| 157 |
+
input :audio input file, language choice, llm choice
|
| 158 |
+
output : audio transcription text, summary of the transcription
|
| 159 |
+
"""
|
| 160 |
try:
|
| 161 |
+
api_key = os.environ.get("OVH_API_KEY", "")
|
| 162 |
processed_audio = convert_audio_to_16000hz(audio_file)
|
| 163 |
audio_content = read_audio_file(processed_audio)
|
| 164 |
url = choose_language(language_choice)
|
|
|
|
| 172 |
return f"Une erreur est survenue : {e}", ""
|
| 173 |
#---------------------------------------------------------------------------------------------------------------------------------
|
| 174 |
# Gradio Interface
|
| 175 |
+
#---------------------------------------------------------------------------------------------------------------------------------
|
| 176 |
language_choices = ["fr", "en", "en/us", "de", "es", "it"]
|
| 177 |
llm_choices = ["Llama-3-70B-Instruct", "Llama-3-8B-Instruct", "Mistral-7B-Instruct", "Mixtral-8x22b-Instruct", "Mixtral-8x7b-instruct"]
|
| 178 |
|