File size: 4,348 Bytes
00ae0ce
defc213
 
 
00ae0ce
defc213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a29c8e
defc213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b1154e
defc213
 
 
8b1154e
defc213
 
 
 
8b1154e
defc213
 
 
 
 
 
 
 
 
 
8b1154e
defc213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b1154e
defc213
 
 
 
8b1154e
defc213
 
 
 
 
 
 
 
 
 
 
 
 
00ae0ce
986b8c7
defc213
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import gradio as gr
import numpy as np
from textblob import TextBlob
import speech_recognition as sr

class SentimentAnalyzer:
    def __init__(self):
        self.recognizer = sr.Recognizer()
    
    def audio_to_text(self, audio):
        """Convert audio to text using speech recognition"""
        try:
            # Get audio data from Gradio input
            sample_rate, audio_data = audio
            
            # Convert audio data to audio file format that speech_recognition can use
            import io
            import scipy.io.wavfile as wav
            byte_io = io.BytesIO()
            wav.write(byte_io, sample_rate, audio_data.astype(np.int16))
            byte_io.seek(0)
            
            # Use speech recognition
            with sr.AudioFile(byte_io) as source:
                audio_data = self.recognizer.record(source)
                text = self.recognizer.recognize_google(audio_data)
                return text
        except Exception as e:
            return f"Error in speech recognition: {str(e)}"
    
    def analyze_sentiment(self, text):
        """Analyze sentiment using TextBlob"""
        try:
            blob = TextBlob(text)
            # Get polarity (-1 to 1) and subjectivity (0 to 1)
            polarity = blob.sentiment.polarity
            subjectivity = blob.sentiment.subjectivity
            
            # Determine sentiment category
            if polarity > 0:
                sentiment = "Positive"
            elif polarity < 0:
                sentiment = "Negative"
            else:
                sentiment = "Neutral"
            
            # Format results
            results_text = f"""
Detected Text: "{text}"

Analysis Results:
- Overall Sentiment: {sentiment}
- Polarity Score: {polarity:.2f} (-1 to +1)
- Subjectivity Score: {subjectivity:.2f} (0 to 1)
"""
            
            # Prepare plot data
            plot_data = {
                "labels": ["Polarity", "Subjectivity"],
                "values": [polarity * 100, subjectivity * 100]  # Convert to percentage for visualization
            }
            
            return results_text, plot_data
            
        except Exception as e:
            return f"Error in sentiment analysis: {str(e)}", None

def create_interface():
    analyzer = SentimentAnalyzer()
    
    def process_audio(audio):
        if audio is None:
            return "Please provide an audio input.", None
        
        # Convert audio to text
        text = analyzer.audio_to_text(audio)
        if text.startswith("Error"):
            return text, None
        
        # Analyze sentiment
        return analyzer.analyze_sentiment(text)

    # Create Gradio interface
    with gr.Blocks() as interface:
        gr.Markdown("# 🎤 Speech Sentiment Analysis")
        gr.Markdown("""
        Speak or upload an audio file to analyze its emotional content.
        The system will convert speech to text and analyze the sentiment.
        """)
        
        with gr.Row():
            with gr.Column():
                audio_input = gr.Audio(
                    label="Upload or Record Audio",
                    type="numpy",
                    sources=["microphone", "upload"]
                )
                analyze_btn = gr.Button("Analyze Sentiment")
            
            with gr.Column():
                output_text = gr.Textbox(
                    label="Analysis Results",
                    lines=8
                )
                output_plot = gr.BarPlot(
                    title="Sentiment Scores",
                    x_title="Metrics",
                    y_title="Score (%)"
                )
        
        analyze_btn.click(
            fn=process_audio,
            inputs=[audio_input],
            outputs=[output_text, output_plot]
        )
        
        gr.Markdown("""
        ### How to Use:
        1. Click the microphone button to record or upload an audio file
        2. Click "Analyze Sentiment" to process
        3. View the results showing:
           - Detected text from speech
           - Overall sentiment (Positive/Negative/Neutral)
           - Polarity score (-100% to +100%)
           - Subjectivity score (0% to 100%)
        """)
    
    return interface

if __name__ == "__main__":
    demo = create_interface()
    demo.launch(share=True)