Sumit404 commited on
Commit
e33dc1a
·
verified ·
1 Parent(s): 2cc23ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -46
app.py CHANGED
@@ -1,51 +1,67 @@
1
  import gradio as gr
2
- from speech_to_text import SpeechToText
3
- from sentiment_analysis import SentimentAnalyzer
4
- import os
5
-
6
- # Initialize models
7
- stt = SpeechToText()
8
- sentiment_analyzer = SentimentAnalyzer()
9
-
10
- def process_audio(audio_path):
11
- try:
12
- # Get transcription
13
- transcription = stt.transcribe(audio_path)
14
-
15
- # Analyze sentiment
16
- sentiment = sentiment_analyzer.analyze(transcription)
17
-
18
- return {
19
- "transcription": transcription,
20
- "sentiment": sentiment
21
- }
22
- except Exception as e:
23
- return {
24
- "transcription": f"Error: {str(e)}",
25
- "sentiment": [{"label": "ERROR", "score": 0.0}]
26
- }
27
 
28
- # Create Gradio interface
29
- def create_interface():
30
- with gr.Blocks() as demo:
31
- gr.Markdown("# Voice Intelligence Demo")
32
-
33
- with gr.Row():
34
- audio_input = gr.Audio(source="microphone", type="filepath")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- with gr.Row():
37
- transcription_output = gr.Textbox(label="Transcription")
38
- sentiment_output = gr.JSON(label="Sentiment Analysis")
 
 
 
 
39
 
40
- audio_input.change(
41
- fn=process_audio,
42
- inputs=[audio_input],
43
- outputs=[transcription_output, sentiment_output]
44
- )
45
-
46
- return demo
47
-
48
- # Launch the app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  if __name__ == "__main__":
50
- demo = create_interface()
51
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
+ import numpy as np
4
+ import librosa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ class VoiceIntelligence:
7
+ def __init__(self):
8
+ # Initialize lightweight sentiment analyzer
9
+ try:
10
+ self.sentiment_analyzer = pipeline(
11
+ "sentiment-analysis",
12
+ model="distilbert-base-uncased-finetuned-sst-2-english",
13
+ max_length=512,
14
+ truncation=True
15
+ )
16
+ except Exception as e:
17
+ print(f"Error initializing sentiment analyzer: {e}")
18
+ self.sentiment_analyzer = None
19
+
20
+ def process_audio(self, audio_path):
21
+ try:
22
+ if audio_path is None:
23
+ return "No audio provided", {"error": "No audio input"}
24
+
25
+ # Basic audio processing
26
+ y, sr = librosa.load(audio_path, sr=16000)
27
 
28
+ # Simple voice activity detection
29
+ intervals = librosa.effects.split(y, top_db=20)
30
+ if len(intervals) == 0:
31
+ return "No speech detected", {"error": "No speech detected"}
32
+
33
+ # For demo, return simple confirmation
34
+ text = "Speech detected - Demo Mode"
35
 
36
+ # Analyze sentiment
37
+ if self.sentiment_analyzer:
38
+ try:
39
+ sentiment = self.sentiment_analyzer(text)
40
+ return text, sentiment[0]
41
+ except Exception as e:
42
+ return text, {"label": "ERROR", "score": 0.0}
43
+ else:
44
+ return text, {"label": "NEUTRAL", "score": 0.5}
45
+
46
+ except Exception as e:
47
+ return f"Error processing audio: {str(e)}", {"error": str(e)}
48
+
49
+ # Initialize the system
50
+ system = VoiceIntelligence()
51
+
52
+ # Create Gradio interface
53
+ demo = gr.Interface(
54
+ fn=system.process_audio,
55
+ inputs=gr.Audio(source="microphone", type="filepath"),
56
+ outputs=[
57
+ gr.Textbox(label="Transcription"),
58
+ gr.JSON(label="Sentiment")
59
+ ],
60
+ title="Voice Intelligence Demo",
61
+ description="Record audio to see transcription and sentiment analysis (Demo Mode)",
62
+ examples=None,
63
+ cache_examples=False
64
+ )
65
+
66
  if __name__ == "__main__":
67
+ demo.launch()