Sumit404 commited on
Commit
6c80fbe
·
verified ·
1 Parent(s): 46e70e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -59
app.py CHANGED
@@ -1,67 +1,21 @@
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()
 
1
  import gradio as gr
2
  from transformers import pipeline
 
 
3
 
4
+ # Load sentiment analysis model
5
+ sentiment = pipeline("sentiment-analysis")
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ # Function to analyze sentiment
8
+ def get_sentiment(input_text):
9
+ return sentiment(input_text)[0]['label']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  # Create Gradio interface
12
+ iface = gr.Interface(
13
+ fn=get_sentiment,
14
+ inputs="text",
15
+ outputs="text",
16
+ title="Sentiment Analysis",
17
+ description="Get Sentiment (Negative/Positive) for the given input"
 
 
 
 
 
18
  )
19
 
20
+ # Launch the app
21
+ iface.launch(share=True)