DrishtiSharma commited on
Commit
fd15a95
1 Parent(s): 2624173

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import librosa
3
+ from transformers import AutoFeatureExtractor, pipeline
4
+
5
+
6
+ def load_and_fix_data(input_file, model_sampling_rate):
7
+ speech, sample_rate = librosa.load(input_file)
8
+ if len(speech.shape) > 1:
9
+ speech = speech[:, 0] + speech[:, 1]
10
+ if sample_rate != model_sampling_rate:
11
+ speech = librosa.resample(speech, sample_rate, model_sampling_rate)
12
+ return speech
13
+
14
+
15
+ feature_extractor = AutoFeatureExtractor.from_pretrained("jonatasgrosman/wav2vec2-large-xlsr-53-spanish")
16
+ sampling_rate = feature_extractor.sampling_rate
17
+
18
+ asr = pipeline("automatic-speech-recognition", model="jonatasgrosman/wav2vec2-large-xlsr-53-spanish")
19
+
20
+
21
+ def predict_and_ctc_lm_decode(input_file):
22
+ speech = load_and_fix_data(input_file, sampling_rate)
23
+ transcribed_text = asr(speech, chunk_length_s=5, stride_length_s=1)["text"]
24
+ pipe1 = pipeline("sentiment-analysis", model = "finiteautomata/beto-sentiment-analysis")
25
+ sentiment = pipe1(transcribed_text)
26
+ sentiment={dic["label"]: dic["score"] for dic in sentiment}
27
+ pipe2 = pipeline("text-classification", model = "hackathon-pln-es/twitter_sexismo-finetuned-robertuito-exist2021")
28
+ sexism_detection = pipe2(transcribed_text)
29
+ sexism_detection={dic["label"]: dic["score"] for dic in sexism_detection}
30
+ #sexism_detection = np.where(sexism_detection['label']== 0, 'No Sexista', 'Sexista')
31
+ pipe3 = pipeline("text-classification", model = "hackathon-pln-es/twitter_sexismo-finetuned-robertuito-exist2021")
32
+ harassment_detection = pipe3(transcribed_text)
33
+ harassment_detection={dic["label"]: dic["score"] for dic in harassment_detection}
34
+ #harassment_detection = np.where(harassment_detection['label']== 0, 'No Harassment', 'Harassment')
35
+ return sentiment
36
+ #sexism_detection, harassment_detection
37
+
38
+ gr.Interface(
39
+ predict_and_ctc_lm_decode,
40
+ inputs=[
41
+ gr.inputs.Audio(source="microphone", type="filepath", label="Record your audio")
42
+ ],
43
+ #outputs=[gr.outputs.Label(num_top_classes=2),gr.outputs.Label(num_top_classes=2), gr.outputs.Label(num_top_classes=2)],
44
+ outputs=[gr.outputs.Label(num_top_classes=2)],
45
+ examples=[["respiracion_happiness.wav"]],
46
+ title="Sentiment Analysis of Spanish Transcribed Audio",
47
+ description="This is a Gradio demo for Sentiment Analysis of Transcribed Spanish Audio",
48
+ layout="horizontal",
49
+ theme="huggingface",
50
+ ).launch(enable_queue=True, cache_examples=True)