Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import whisper
|
4 |
+
from transformers import pipeline
|
5 |
+
import librosa
|
6 |
+
import soundfile as sf
|
7 |
+
import tempfile
|
8 |
+
|
9 |
+
model = whisper.load_model("base")
|
10 |
+
|
11 |
+
sentiment_analysis = pipeline(
|
12 |
+
"sentiment-analysis",
|
13 |
+
framework="pt",
|
14 |
+
model="SamLowe/roberta-base-go_emotions"
|
15 |
+
)
|
16 |
+
|
17 |
+
def analyze_sentiment(text):
|
18 |
+
results = sentiment_analysis(text)
|
19 |
+
sentiment_results = {
|
20 |
+
result['label']: result['score'] for result in results
|
21 |
+
}
|
22 |
+
return sentiment_results
|
23 |
+
|
24 |
+
def get_sentiment_emoji(sentiment):
|
25 |
+
# Define the mapping of sentiments to emojis
|
26 |
+
emoji_mapping = {
|
27 |
+
"disappointment": "๐",
|
28 |
+
"sadness": "๐ข",
|
29 |
+
"annoyance": "๐ ",
|
30 |
+
"neutral": "๐",
|
31 |
+
"disapproval": "๐",
|
32 |
+
"realization": "๐ฎ",
|
33 |
+
"nervousness": "๐ฌ",
|
34 |
+
"approval": "๐",
|
35 |
+
"joy": "๐",
|
36 |
+
"anger": "๐ก",
|
37 |
+
"embarrassment": "๐ณ",
|
38 |
+
"caring": "๐ค",
|
39 |
+
"remorse": "๐",
|
40 |
+
"disgust": "๐คข",
|
41 |
+
"grief": "๐ฅ",
|
42 |
+
"confusion": "๐",
|
43 |
+
"relief": "๐",
|
44 |
+
"desire": "๐",
|
45 |
+
"admiration": "๐",
|
46 |
+
"optimism": "๐",
|
47 |
+
"fear": "๐จ",
|
48 |
+
"love": "โค๏ธ",
|
49 |
+
"excitement": "๐",
|
50 |
+
"curiosity": "๐ค",
|
51 |
+
"amusement": "๐",
|
52 |
+
"surprise": "๐ฒ",
|
53 |
+
"gratitude": "๐",
|
54 |
+
"pride": "๐ฆ"
|
55 |
+
}
|
56 |
+
return emoji_mapping.get(sentiment, "")
|
57 |
+
|
58 |
+
def display_sentiment_results(sentiment_results, option):
|
59 |
+
sentiment_text = ""
|
60 |
+
for sentiment, score in sentiment_results.items():
|
61 |
+
emoji = get_sentiment_emoji(sentiment)
|
62 |
+
score_percentage = score * 100 # Corrected indentation
|
63 |
+
if option == "Sentiment Only":
|
64 |
+
sentiment_text += f"{sentiment} {emoji}\n"
|
65 |
+
elif option == "Sentiment + Score":
|
66 |
+
sentiment_text += f"{sentiment} {emoji}: {score_percentage:.2f}%\n"
|
67 |
+
return sentiment_text
|
68 |
+
|
69 |
+
def load_and_resample_audio(file_path, target_sample_rate=16000):
|
70 |
+
audio, _ = librosa.load(file_path, sr=target_sample_rate)
|
71 |
+
temp_file_path = '/tmp/resampled_audio.wav'
|
72 |
+
sf.write(temp_file_path, audio, target_sample_rate)
|
73 |
+
return temp_file_path
|
74 |
+
|
75 |
+
def inference(audio_file_path, sentiment_option):
|
76 |
+
resampled_audio_path = load_and_resample_audio(audio_file_path)
|
77 |
+
audio = whisper.load_audio(resampled_audio_path)
|
78 |
+
audio = whisper.pad_or_trim(audio)
|
79 |
+
|
80 |
+
mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
81 |
+
_, probs = model.detect_language(mel)
|
82 |
+
lang = max(probs, key=probs.get)
|
83 |
+
|
84 |
+
options = whisper.DecodingOptions(fp16=False)
|
85 |
+
result = whisper.decode(model, mel, options)
|
86 |
+
|
87 |
+
sentiment_results = analyze_sentiment(result.text)
|
88 |
+
sentiment_output = display_sentiment_results(sentiment_results, sentiment_option)
|
89 |
+
|
90 |
+
return lang.upper(), result.text, sentiment_output
|
91 |
+
|
92 |
+
title = "๐ค Gradio UI"
|
93 |
+
description = "we have deployed our model on Gradio"
|
94 |
+
|
95 |
+
block = gr.Blocks()
|
96 |
+
|
97 |
+
with block:
|
98 |
+
gr.Markdown("# Mood Reader ๐ต๏ธโโ๏ธ")
|
99 |
+
gr.Markdown("Your Words Whisper ๐คซ, But Emotions Shout ๐ข โ Discover What's Truly Behind Every Sentence with Mood Reader ๐ต๏ธโโ๏ธ๐ฌ")
|
100 |
+
with gr.Column():
|
101 |
+
audio = gr.Audio(label="Input Audio", type="filepath")
|
102 |
+
sentiment_option = gr.Radio(choices=["Sentiment Only", "Sentiment + Score"], label="Select an option")
|
103 |
+
transcribe_btn = gr.Button("Transcribe")
|
104 |
+
lang_str = gr.Textbox(label="Language")
|
105 |
+
text = gr.Textbox(label="Transcription")
|
106 |
+
sentiment_output = gr.Textbox(label="Sentiment Analysis Results")
|
107 |
+
|
108 |
+
transcribe_btn.click(
|
109 |
+
inference,
|
110 |
+
inputs=[audio, sentiment_option],
|
111 |
+
outputs=[lang_str, text, sentiment_output]
|
112 |
+
)
|
113 |
+
|
114 |
+
block.launch()
|