Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,67 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
import
|
| 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 |
-
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
if __name__ == "__main__":
|
| 50 |
-
demo
|
| 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()
|
|
|