shrimantasatpati commited on
Commit
3b16fc4
ยท
1 Parent(s): 7fb34b7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -0
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import whisper
3
+ from transformers import pipeline
4
+ from streamlit_mic_recorder import mic_recorder
5
+ import wave
6
+ import numpy as np
7
+ import os
8
+
9
+ temp_audio_file_path = "./output.wav"
10
+
11
+ # Streamlit app structure
12
+ st.title("๐ŸŽค English ASR ๐Ÿ’ฌ")
13
+ # Load models
14
+ model = whisper.load_model("base")
15
+ st.write("Whisper Model Loaded!")
16
+ sentiment_analysis = pipeline("sentiment-analysis", framework="pt", model="SamLowe/roberta-base-go_emotions")
17
+
18
+ st.write("Record your voice, and play the recorded audio:")
19
+ audio=mic_recorder(start_prompt="โ–ถ๏ธ",stop_prompt="๐Ÿ›‘",key='recorder')
20
+ # audio=mic_recorder(start_prompt="Start",stop_prompt="Stop",key='recorder')
21
+
22
+ if audio:
23
+ st.audio(audio['bytes'])
24
+
25
+ audio_bytes = audio["bytes"]
26
+ # Set the audio file parameters
27
+ sample_width = audio["sample_width"] # 2 bytes per sample for 16-bit PCM
28
+ sample_rate = audio["sample_rate"] # 44.1 kHz sample rate
29
+ num_channels = 1 # 1 channel for mono, 2 for stereo
30
+
31
+ # Create a new wave file and write the audio bytes
32
+ with wave.open(temp_audio_file_path, 'w') as wave_file:
33
+ wave_file.setnchannels(num_channels)
34
+ wave_file.setsampwidth(sample_width)
35
+ wave_file.setframerate(sample_rate)
36
+ wave_file.writeframes(audio_bytes)
37
+
38
+ def analyze_sentiment(text):
39
+ results = sentiment_analysis(text)
40
+ sentiment_results = {result['label']: result['score'] for result in results}
41
+ return sentiment_results
42
+
43
+ def get_sentiment_emoji(sentiment):
44
+ # Define the emojis corresponding to each sentiment
45
+ emoji_mapping = {
46
+ "disappointment": "๐Ÿ˜ž",
47
+ "sadness": "๐Ÿ˜ข",
48
+ "annoyance": "๐Ÿ˜ ",
49
+ "neutral": "๐Ÿ˜",
50
+ "disapproval": "๐Ÿ‘Ž",
51
+ "realization": "๐Ÿ˜ฎ",
52
+ "nervousness": "๐Ÿ˜ฌ",
53
+ "approval": "๐Ÿ‘",
54
+ "joy": "๐Ÿ˜„",
55
+ "anger": "๐Ÿ˜ก",
56
+ "embarrassment": "๐Ÿ˜ณ",
57
+ "caring": "๐Ÿค—",
58
+ "remorse": "๐Ÿ˜”",
59
+ "disgust": "๐Ÿคข",
60
+ "grief": "๐Ÿ˜ฅ",
61
+ "confusion": "๐Ÿ˜•",
62
+ "relief": "๐Ÿ˜Œ",
63
+ "desire": "๐Ÿ˜",
64
+ "admiration": "๐Ÿ˜Œ",
65
+ "optimism": "๐Ÿ˜Š",
66
+ "fear": "๐Ÿ˜จ",
67
+ "love": "โค๏ธ",
68
+ "excitement": "๐ŸŽ‰",
69
+ "curiosity": "๐Ÿค”",
70
+ "amusement": "๐Ÿ˜„",
71
+ "surprise": "๐Ÿ˜ฒ",
72
+ "gratitude": "๐Ÿ™",
73
+ "pride": "๐Ÿฆ"
74
+ }
75
+ return emoji_mapping.get(sentiment, "")
76
+
77
+ def display_sentiment_results(sentiment_results, option):
78
+ sentiment_text = ""
79
+ for sentiment, score in sentiment_results.items():
80
+ emoji = get_sentiment_emoji(sentiment)
81
+ if option == "Sentiment Only":
82
+ sentiment_text += f"{sentiment} {emoji}\n"
83
+ elif option == "Sentiment + Score":
84
+ sentiment_text += f"{sentiment} {emoji}: {score}\n"
85
+ return sentiment_text
86
+
87
+ def inference(ans, sentiment_option):
88
+ sentiment_results = analyze_sentiment(ans)
89
+ sentiment_output = display_sentiment_results(sentiment_results, sentiment_option)
90
+ return sentiment_output
91
+
92
+ # Sentiment Option Radio
93
+ sentiment_option = st.radio("Select an option", ["Sentiment Only", "Sentiment + Score"], index=0)
94
+
95
+ # Button to trigger the processing
96
+ if st.button("Get sentiments"):
97
+ st.write("Transcribing Audio...")
98
+ result = model.transcribe(temp_audio_file_path)
99
+ ans = result["text"]
100
+ st.write(ans)
101
+
102
+ # Call the inference function with inputs and get outputs
103
+ sentiment_output_value = inference(ans, sentiment_option)
104
+ st.write(sentiment_output_value)
105
+
106
+ # Add a footer
107
+ st.markdown('''
108
+ Whisper Model by [OpenAI](https://github.com/openai/whisper)
109
+ ''')