Shreyansh49 commited on
Commit
4c391c9
ยท
verified ยท
1 Parent(s): c713358

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -0
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()