Spaces:
Sleeping
Sleeping
| 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']) | |