imljls's picture
Create app.py
0e992cf verified
raw
history blame
1.61 kB
import streamlit as st
from transformers import pipeline
st.set_page_config(page_title="Text to Sentiment Analysis Audio", page_icon="🦜")
st.header("Text to Sentiment Analysis Audio")
# Define the sentiment analysis pipeline
senti_ana_pipeline = pipeline("text-classification", model="imljls/gpt_review_senti_1")
# Define the text-to-speech pipeline (replace 'your-tts-model' with the actual model name)
text_audio_pipeline = pipeline("text-to-audio", model="Matthijs/mms-tts-eng")
def analyze_sentiment(text):
result = senti_ana_pipeline(text)[0]
sentiment = result['label']
probability = result['score']
return sentiment, probability
def format_sentiment_text(sentiment, probability):
return f"The sentiment of the text is {sentiment} with a probability of {probability:.2f}."
def text_to_audio(text):
audio = text_audio_pipeline(text)
return audio
input_text = st.text_input("Enter text for sentiment analysis:")
if input_text:
# Stage 1: Analyze sentiment
st.text('Analyzing sentiment...')
sentiment, probability = analyze_sentiment(input_text)
sentiment_text = format_sentiment_text(sentiment, probability)
st.write(sentiment_text)
# Stage 2: Convert text to audio
st.text('Generating audio...')
sentiment_text_for_audio = f"The sentiment of the input text is {sentiment}, and the probability is {probability:.2f}."
audio_data = text_to_audio(sentiment_text_for_audio)
# Play button
if st.button("Play Audio"):
st.audio(audio_data['audio'], format="audio/wav", start_time=0, sample_rate=audio_data['sampling_rate'])