Boltz79 commited on
Commit
0a29c8e
·
verified ·
1 Parent(s): fe57b21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -77
app.py CHANGED
@@ -1,92 +1,53 @@
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
- sources=["microphone", "upload"], # Changed from source to sources
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()
 
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 a sentiment analysis model
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
  try:
13
+ # Step 1: Transcribe audio to text using Whisper
14
+ transcription = whisper(audio_file)["text"]
 
 
 
15
 
16
+ # Step 2: Analyze sentiment of the transcription
17
+ sentiment_result = sentiment_analyzer(transcription)[0]
18
 
19
+ # Prepare the output
20
+ output = {
21
  "transcription": transcription,
22
  "sentiment": sentiment_result["label"],
23
+ "confidence": round(sentiment_result["score"], 4)
24
  }
25
+ return output
26
  except Exception as e:
27
+ return {"error": str(e)}
 
 
 
 
28
 
29
+ # Gradio Interface
30
+ def gradio_interface(audio):
31
+ if audio is None:
32
+ return "Please record or upload an audio file."
33
+ result = analyze_call(audio)
34
+ if "error" in result:
35
+ return f"Error: {result['error']}"
36
+ return (
37
+ f"**Transcription:** {result['transcription']}\n\n"
38
+ f"**Sentiment:** {result['sentiment']}\n\n"
39
+ f"**Confidence:** {result['confidence']}"
 
 
 
 
 
 
 
 
 
 
 
40
  )
41
 
42
+ # Create Gradio app
43
+ interface = gr.Interface(
44
+ fn=gradio_interface,
45
+ inputs=gr.Audio(source="microphone", type="filepath", label="Record or Upload Audio"),
46
+ outputs=gr.Textbox(label="Analysis Result", lines=5),
47
+ title="Real-Time Call Analysis",
48
+ description="Record or upload audio to analyze tone and sentiment in real time.",
49
+ live=False # Set to False to avoid constant re-runs
50
+ )
51
 
52
+ # Launch the app
53
+ interface.launch()