File size: 1,614 Bytes
0e992cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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'])