imljls commited on
Commit
0e992cf
1 Parent(s): 771e787

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+
4
+ st.set_page_config(page_title="Text to Sentiment Analysis Audio", page_icon="🦜")
5
+ st.header("Text to Sentiment Analysis Audio")
6
+
7
+ # Define the sentiment analysis pipeline
8
+ senti_ana_pipeline = pipeline("text-classification", model="imljls/gpt_review_senti_1")
9
+
10
+ # Define the text-to-speech pipeline (replace 'your-tts-model' with the actual model name)
11
+ text_audio_pipeline = pipeline("text-to-audio", model="Matthijs/mms-tts-eng")
12
+
13
+ def analyze_sentiment(text):
14
+ result = senti_ana_pipeline(text)[0]
15
+ sentiment = result['label']
16
+ probability = result['score']
17
+ return sentiment, probability
18
+
19
+ def format_sentiment_text(sentiment, probability):
20
+ return f"The sentiment of the text is {sentiment} with a probability of {probability:.2f}."
21
+
22
+ def text_to_audio(text):
23
+ audio = text_audio_pipeline(text)
24
+ return audio
25
+
26
+ input_text = st.text_input("Enter text for sentiment analysis:")
27
+
28
+ if input_text:
29
+ # Stage 1: Analyze sentiment
30
+ st.text('Analyzing sentiment...')
31
+ sentiment, probability = analyze_sentiment(input_text)
32
+ sentiment_text = format_sentiment_text(sentiment, probability)
33
+ st.write(sentiment_text)
34
+
35
+ # Stage 2: Convert text to audio
36
+ st.text('Generating audio...')
37
+ sentiment_text_for_audio = f"The sentiment of the input text is {sentiment}, and the probability is {probability:.2f}."
38
+ audio_data = text_to_audio(sentiment_text_for_audio)
39
+
40
+ # Play button
41
+ if st.button("Play Audio"):
42
+ st.audio(audio_data['audio'], format="audio/wav", start_time=0, sample_rate=audio_data['sampling_rate'])