Sumit404 commited on
Commit
159db2c
·
verified ·
1 Parent(s): c68497b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -30
app.py CHANGED
@@ -1,32 +1,51 @@
 
 
 
1
  import os
2
- from fastapi import FastAPI, UploadFile, File
3
- import tempfile
4
 
5
- # Ensure Whisper is installed
6
- os.system("pip install openai-whisper torch transformers")
7
-
8
- import whisper
9
- from transformers import pipeline
10
-
11
- app = FastAPI()
12
-
13
- # Load Models
14
- speech_model = whisper.load_model("base")
15
- sentiment_analyzer = pipeline("sentiment-analysis")
16
-
17
- @app.post("/analyze/")
18
- async def analyze(audio: UploadFile = File(...)):
19
- with tempfile.NamedTemporaryFile(delete=False) as temp:
20
- temp.write(await audio.read())
21
- temp_path = temp.name
22
-
23
- # Transcribe Speech
24
- transcription = speech_model.transcribe(temp_path)["text"]
25
-
26
- # Analyze Sentiment
27
- sentiment = sentiment_analyzer(transcription)
28
-
29
- return {
30
- "transcription": transcription,
31
- "sentiment": sentiment[0]
32
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()