Commit
ยท
3b16fc4
1
Parent(s):
7fb34b7
Create app.py
Browse files
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 |
+
''')
|