Seidazymov Adil
commited on
Commit
·
21f6ca3
1
Parent(s):
6ef792a
Final
Browse files
app.py
CHANGED
|
@@ -2,10 +2,26 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from gradio_client import Client, file
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def detect_language(file_path):
|
| 7 |
-
|
| 8 |
-
result = client.predict(param_0=file(file_path), api_name="/predict")
|
| 9 |
language_result = result["label"].split(": ")[1]
|
| 10 |
if language_result.lower() in ["russian", "belarussian", "ukrainian"]:
|
| 11 |
selected_language = "russian"
|
|
@@ -13,44 +29,55 @@ def detect_language(file_path):
|
|
| 13 |
selected_language = "kazakh"
|
| 14 |
return selected_language
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
|
| 47 |
gradio_app = gr.Interface(
|
| 48 |
-
fn=
|
| 49 |
-
inputs=gr.Audio(sources="upload", type="filepath"),
|
| 50 |
-
outputs="
|
| 51 |
-
title="File Upload Transcription",
|
| 52 |
-
description="Upload an audio file to determine language."
|
| 53 |
)
|
|
|
|
| 54 |
|
| 55 |
-
if __name__ == "__main__":
|
| 56 |
-
gradio_app.launch()
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from gradio_client import Client, file
|
| 4 |
|
| 5 |
+
language_classifier = Client("adrien-alloreview/speechbrain-lang-id-voxlingua107-ecapa")
|
| 6 |
+
transcriber = Client("tensorlake/audio-extractors")
|
| 7 |
+
emotion_detector = pipeline(
|
| 8 |
+
"audio-classification",
|
| 9 |
+
model="HowMannyMore/wav2vec2-lg-xlsr-ur-speech-emotion-recognition",
|
| 10 |
+
)
|
| 11 |
+
model_name_rus = "IlyaGusev/rubertconv_toxic_clf"
|
| 12 |
+
toxic_detector = pipeline(
|
| 13 |
+
"text-classification",
|
| 14 |
+
model=model_name_rus,
|
| 15 |
+
tokenizer=model_name_rus,
|
| 16 |
+
framework="pt",
|
| 17 |
+
max_length=512,
|
| 18 |
+
truncation=True,
|
| 19 |
+
device=0,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
|
| 23 |
def detect_language(file_path):
|
| 24 |
+
result = language_classifier.predict(param_0=file(file_path), api_name="/predict")
|
|
|
|
| 25 |
language_result = result["label"].split(": ")[1]
|
| 26 |
if language_result.lower() in ["russian", "belarussian", "ukrainian"]:
|
| 27 |
selected_language = "russian"
|
|
|
|
| 29 |
selected_language = "kazakh"
|
| 30 |
return selected_language
|
| 31 |
|
| 32 |
+
|
| 33 |
+
def request_gradio(file_path, language):
|
| 34 |
+
try:
|
| 35 |
+
result = transcriber.predict(
|
| 36 |
+
audio_filepath=file(file_path),
|
| 37 |
+
task="transcribe",
|
| 38 |
+
batch_size=24,
|
| 39 |
+
chunk_length_s=30,
|
| 40 |
+
sampling_rate=16000,
|
| 41 |
+
language=language,
|
| 42 |
+
num_speakers=2,
|
| 43 |
+
min_speakers=2,
|
| 44 |
+
max_speakers=2,
|
| 45 |
+
assisted=False,
|
| 46 |
+
api_name="/transcribe",
|
| 47 |
+
)
|
| 48 |
+
return result
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return None
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def detect_emotion(audio):
|
| 54 |
+
res = emotion_detector(audio)
|
| 55 |
+
emotion_with_max_score = res[0]["label"]
|
| 56 |
+
return emotion_with_max_score
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def detect_toxic_local(text_whisper):
|
| 60 |
+
res = toxic_detector([text_whisper])[0]["label"]
|
| 61 |
+
if res == "toxic":
|
| 62 |
+
return True
|
| 63 |
+
if res == "neutral":
|
| 64 |
+
return False
|
| 65 |
+
else:
|
| 66 |
+
return None
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def assessment(file_path):
|
| 70 |
+
language = detect_language(file_path)
|
| 71 |
+
result_text = request_gradio(file_path, language)
|
| 72 |
+
result_emotion = detect_emotion(result_text)
|
| 73 |
+
result_toxic = detect_toxic_local(result_text)
|
| 74 |
+
return {"emotion": result_emotion, "toxic": result_toxic}
|
| 75 |
|
| 76 |
|
| 77 |
gradio_app = gr.Interface(
|
| 78 |
+
fn=assessment,
|
| 79 |
+
inputs=gr.Audio(sources=["upload"], type="filepath"),
|
| 80 |
+
outputs="json"
|
|
|
|
|
|
|
| 81 |
)
|
| 82 |
+
gradio_app.launch()
|
| 83 |
|
|
|
|
|
|