Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,78 +1,90 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
|
|
3 |
|
4 |
-
def
|
5 |
-
"""
|
6 |
try:
|
7 |
-
#
|
|
|
|
|
|
|
|
|
8 |
transcriber = pipeline(
|
9 |
-
"automatic-speech-recognition",
|
10 |
-
model="openai/whisper-
|
|
|
11 |
)
|
12 |
|
13 |
-
# Load
|
14 |
-
|
15 |
"sentiment-analysis",
|
16 |
-
model="
|
|
|
17 |
)
|
18 |
|
19 |
-
return transcriber,
|
20 |
except Exception as e:
|
21 |
-
|
|
|
22 |
|
23 |
-
def
|
24 |
-
"""
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
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 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
),
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
|
|
|
73 |
if __name__ == "__main__":
|
74 |
-
interface = create_interface()
|
75 |
interface.launch(
|
76 |
-
|
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 |
)
|