File size: 2,144 Bytes
0e992cf
73b0df5
0e992cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73b0df5
 
 
 
 
 
 
 
 
 
 
 
 
 
64d03c0
0e992cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73b0df5
35a6d84
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import streamlit as st
import inflect
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 sentence is {sentiment} with a probability of {probability:.2f}."

def number_to_words(number):
    p = inflect.engine()
    # Split the number into integer and decimal parts
    integer_part, decimal_part = divmod(number, 1)
    # Convert integer part to words
    words = p.number_to_words(int(integer_part))
    # Handle the decimal part
    if decimal_part > 0:
        decimal_part = round(decimal_part * 100)  # Convert to two decimal places
        decimal_words = p.number_to_words(int(decimal_part))
        words += f" point {decimal_words.replace('-', ' ')}"
    return words

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...')
    prob=number_to_words(probability)
    sentiment_text_for_audio = f"The sentence is {sentiment}, with a probability of {prob}."
    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'])