Boltz79 commited on
Commit
dc5c04c
·
verified ·
1 Parent(s): a6d8351

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -32
app.py CHANGED
@@ -1,39 +1,92 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load Whisper for speech-to-text
5
- whisper = pipeline("automatic-speech-recognition", model="openai/whisper-medium")
6
-
7
- # Load DistilBERT for sentiment analysis
8
- sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
9
-
10
- # Function to process audio and analyze tone
11
- def analyze_call(audio_file):
12
- # Step 1: Transcribe audio to text
13
- transcription = whisper(audio_file)["text"]
 
 
 
 
 
 
 
14
 
15
- # Step 2: Analyze sentiment of the transcription
16
- sentiment_result = sentiment_analyzer(transcription)[0]
 
 
 
 
17
 
18
- return {
19
- "transcription": transcription,
20
- "sentiment": sentiment_result["label"],
21
- "confidence": sentiment_result["score"]
22
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- # Gradio Interface
25
- interface = gr.Interface(
26
- fn=analyze_call,
27
- inputs=gr.Audio(source="microphone", type="filepath"),
28
- outputs=[
29
- gr.Textbox(label="Transcription"),
30
- gr.Textbox(label="Sentiment"),
31
- gr.Textbox(label="Confidence")
32
- ],
33
- live=True, # Enable real-time processing
34
- title="Real-Time Call Analysis",
35
- description="Upload or record audio to analyze tone and sentiment."
36
- )
37
 
38
- # Launch the app
39
- interface.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ def create_speech_analyzer():
5
+ # Initialize models with error handling
6
+ try:
7
+ # Load Whisper model for speech recognition
8
+ transcriber = pipeline(
9
+ "automatic-speech-recognition",
10
+ model="openai/whisper-medium",
11
+ max_new_tokens=128
12
+ )
13
+
14
+ # Load sentiment analysis model
15
+ sentiment_model = pipeline(
16
+ "sentiment-analysis",
17
+ model="distilbert-base-uncased-finetuned-sst-2-english"
18
+ )
19
+
20
+ return transcriber, sentiment_model
21
 
22
+ except Exception as e:
23
+ raise RuntimeError(f"Error loading models: {str(e)}")
24
+
25
+ def analyze_speech(audio_file):
26
+ """
27
+ Analyze speech audio for transcription and sentiment.
28
 
29
+ Args:
30
+ audio_file: Path to audio file or audio data
31
+
32
+ Returns:
33
+ dict: Contains transcription, sentiment and confidence score
34
+ """
35
+ try:
36
+ # Get model instances
37
+ transcriber, sentiment_model = create_speech_analyzer()
38
+
39
+ # Transcribe audio
40
+ transcription = transcriber(audio_file)["text"]
41
+
42
+ # Analyze sentiment
43
+ sentiment_result = sentiment_model(transcription)[0]
44
+
45
+ return {
46
+ "transcription": transcription,
47
+ "sentiment": sentiment_result["label"],
48
+ "confidence": f"{sentiment_result['score']:.2%}"
49
+ }
50
+
51
+ except Exception as e:
52
+ return {
53
+ "transcription": f"Error processing audio: {str(e)}",
54
+ "sentiment": "ERROR",
55
+ "confidence": "0%"
56
+ }
57
+
58
+ def create_interface():
59
+ """Create and configure the Gradio interface"""
60
+ return gr.Interface(
61
+ fn=analyze_speech,
62
+ inputs=gr.Audio(
63
+ source="microphone",
64
+ type="filepath",
65
+ label="Upload or Record Audio"
66
+ ),
67
+ outputs=[
68
+ gr.Textbox(label="Transcription"),
69
+ gr.Textbox(label="Sentiment Analysis"),
70
+ gr.Textbox(label="Confidence Score")
71
+ ],
72
+ title="Real-Time Speech Sentiment Analyzer",
73
+ description="""
74
+ This tool transcribes speech and analyzes its sentiment in real-time.
75
+ Upload an audio file or record directly through your microphone.
76
+ """,
77
+ theme=gr.themes.Soft(),
78
+ examples=[], # Add example audio files here if desired
79
+ cache_examples=True
80
+ )
81
 
82
+ def main():
83
+ # Create and launch the interface
84
+ interface = create_interface()
85
+ interface.launch(
86
+ share=True, # Enable sharing via public URL
87
+ debug=True, # Enable debug mode for better error messages
88
+ server_name="0.0.0.0" # Allow external connections
89
+ )
 
 
 
 
 
90
 
91
+ if __name__ == "__main__":
92
+ main()