Seidazymov Adil commited on
Commit
96acb32
1 Parent(s): bcb110b
Files changed (1) hide show
  1. app.py +35 -22
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
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")
@@ -21,13 +22,17 @@ toxic_detector = pipeline(
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"
28
- else:
29
- selected_language = "kazakh"
30
- return selected_language
 
 
 
 
31
 
32
 
33
  def request_gradio(file_path, language):
@@ -47,31 +52,40 @@ def request_gradio(file_path, language):
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(
@@ -80,4 +94,3 @@ gradio_app = gr.Interface(
80
  outputs="json"
81
  )
82
  gradio_app.launch()
83
-
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  from gradio_client import Client, file
4
+ import json
5
 
6
  language_classifier = Client("adrien-alloreview/speechbrain-lang-id-voxlingua107-ecapa")
7
  transcriber = Client("tensorlake/audio-extractors")
 
22
 
23
 
24
  def detect_language(file_path):
25
+ try:
26
+ result = language_classifier.predict(param_0=file(file_path), api_name="/predict")
27
+ language_result = result["label"].split(": ")[1]
28
+ if language_result.lower() in ["russian", "belarussian", "ukrainian"]:
29
+ selected_language = "russian"
30
+ else:
31
+ selected_language = "kazakh"
32
+ return selected_language
33
+ except Exception as e:
34
+ print(f"Language detection failed: {e}")
35
+ return None
36
 
37
 
38
  def request_gradio(file_path, language):
 
52
  )
53
  return result
54
  except Exception as e:
55
+ print(f"Transcription failed: {e}")
56
  return None
57
 
58
 
59
  def detect_emotion(audio):
60
+ try:
61
+ res = emotion_detector(audio)
62
+ emotion_with_max_score = res[0]["label"]
63
+ return emotion_with_max_score
64
+ except Exception as e:
65
+ print(f"Emotion detection failed: {e}")
66
+ return None
67
 
68
 
69
  def detect_toxic_local(text_whisper):
70
+ try:
71
+ res = toxic_detector([text_whisper])[0]["label"]
72
+ if res == "toxic":
73
+ return True
74
+ elif res == "neutral":
75
+ return False
76
+ else:
77
+ return None
78
+ except Exception as e:
79
+ print(f"Toxicity detection failed: {e}")
80
  return None
81
 
82
 
83
  def assessment(file_path):
84
+ language = detect_language(file_path) or "unknown"
85
+ result_text = request_gradio(file_path, language) or ""
86
+ result_emotion = detect_emotion(result_text) or "unknown"
87
+ result_toxic = detect_toxic_local(result_text) or False
88
+ return json.dumps({"emotion": result_emotion, "toxic": result_toxic})
89
 
90
 
91
  gradio_app = gr.Interface(
 
94
  outputs="json"
95
  )
96
  gradio_app.launch()