Boltz79 commited on
Commit
b921a8f
·
verified ·
1 Parent(s): 986b8c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -59
app.py CHANGED
@@ -1,78 +1,90 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
- def create_tone_analyzer():
5
- """Initialize the speech-to-text and tone analysis models"""
6
  try:
7
- # Load Whisper for speech recognition
 
 
 
 
8
  transcriber = pipeline(
9
- "automatic-speech-recognition",
10
- model="openai/whisper-small"
 
11
  )
12
 
13
- # Load RoBERTa for tone analysis
14
- tone_analyzer = pipeline(
15
  "sentiment-analysis",
16
- model="cardiffnlp/twitter-roberta-base-sentiment-latest"
 
17
  )
18
 
19
- return transcriber, tone_analyzer
20
  except Exception as e:
21
- raise RuntimeError(f"Failed to load models: {str(e)}")
 
22
 
23
- def analyze_tone(audio_file):
24
- """Analyze the tone of speech"""
25
- try:
26
- transcriber, tone_analyzer = create_tone_analyzer()
27
-
28
- # Convert speech to text
29
- text = transcriber(audio_file)["text"]
30
-
31
- # Analyze tone
32
- result = tone_analyzer(text)[0]
33
-
34
- # Convert model output to human-readable format
35
- tone_mapping = {
36
- 'LABEL_0': 'Negative',
37
- 'LABEL_1': 'Neutral',
38
- 'LABEL_2': 'Positive'
39
- }
40
-
41
- tone = tone_mapping.get(result['label'], result['label'])
42
- confidence = result['score']
43
-
44
- return {
45
- "tone": f"{tone}",
46
- "confidence": f"{confidence:.1%}"
47
- }
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  except Exception as e:
50
- return {
51
- "tone": "Error",
52
- "confidence": f"Failed to analyze: {str(e)}"
53
- }
54
 
55
- def create_interface():
56
- """Create the Gradio interface"""
57
- return gr.Interface(
58
- fn=analyze_tone,
59
- inputs=gr.Audio(
60
- sources=["microphone", "upload"],
61
- type="filepath",
62
- label="Record or Upload Audio"
63
- ),
64
- outputs=[
65
- gr.Textbox(label="Detected Tone"),
66
- gr.Textbox(label="Confidence")
67
- ],
68
- title="Speech Tone Analyzer",
69
- description="Record or upload audio to analyze its emotional tone.",
70
- theme=gr.themes.Soft()
71
- )
72
 
 
73
  if __name__ == "__main__":
74
- interface = create_interface()
75
  interface.launch(
76
- share=True,
77
- server_name="0.0.0.0"
 
 
78
  )
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import torch
4
 
5
+ def load_models():
6
+ """Load and verify models with error checking"""
7
  try:
8
+ # Check CUDA availability
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ print(f"Using device: {device}")
11
+
12
+ # Load a smaller Whisper model for better compatibility
13
  transcriber = pipeline(
14
+ "automatic-speech-recognition",
15
+ model="openai/whisper-tiny",
16
+ device=device
17
  )
18
 
19
+ # Load a simpler sentiment model
20
+ sentiment = pipeline(
21
  "sentiment-analysis",
22
+ model="distilbert-base-uncased-finetuned-sst-2-english",
23
+ device=device
24
  )
25
 
26
+ return transcriber, sentiment
27
  except Exception as e:
28
+ print(f"Error loading models: {str(e)}")
29
+ return None, None
30
 
31
+ def analyze_audio(audio_path):
32
+ """
33
+ Analyze audio tone with robust error handling
34
+ """
35
+ if audio_path is None:
36
+ return "Please provide an audio input", "No audio detected"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ try:
39
+ # Load models
40
+ transcriber, sentiment = load_models()
41
+ if transcriber is None or sentiment is None:
42
+ return "Error loading models", "Model initialization failed"
43
+
44
+ # Transcribe with error checking
45
+ try:
46
+ result = transcriber(audio_path)
47
+ text = result["text"]
48
+ if not text.strip():
49
+ return "No speech detected", "Empty transcription"
50
+ except Exception as e:
51
+ return f"Transcription error: {str(e)}", "Failed to process audio"
52
+
53
+ # Analyze sentiment
54
+ try:
55
+ sentiment_result = sentiment(text)[0]
56
+ tone = sentiment_result["label"]
57
+ confidence = f"{sentiment_result['score']:.2%}"
58
+ return tone, confidence
59
+ except Exception as e:
60
+ return f"Sentiment analysis error: {str(e)}", "Analysis failed"
61
+
62
  except Exception as e:
63
+ return f"Unexpected error: {str(e)}", "Process failed"
 
 
 
64
 
65
+ # Create interface with simplified components
66
+ interface = gr.Interface(
67
+ fn=analyze_audio,
68
+ inputs=gr.Audio(
69
+ sources=["microphone", "upload"],
70
+ type="filepath",
71
+ ),
72
+ outputs=[
73
+ gr.Textbox(label="Tone"),
74
+ gr.Textbox(label="Confidence Level")
75
+ ],
76
+ title="Simple Speech Tone Analyzer",
77
+ description="Record or upload audio to analyze its tone. The analysis may take a few moments.",
78
+ examples=None,
79
+ cache_examples=False,
80
+ theme=gr.themes.Base(),
81
+ )
82
 
83
+ # Launch with specific parameters for better stability
84
  if __name__ == "__main__":
 
85
  interface.launch(
86
+ debug=True,
87
+ server_name="0.0.0.0",
88
+ server_port=7860,
89
+ share=True
90
  )