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