Sumit404 commited on
Commit
2cf8dcb
·
verified ·
1 Parent(s): 7cf5935

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -14
app.py CHANGED
@@ -1,21 +1,38 @@
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)
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import torch
4
 
5
+ # Load Whisper model and tokenizer
6
+ whisper_model = "openai/whisper-base"
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+ asr = pipeline("automatic-speech-recognition", model=whisper_model, device=device)
9
 
10
+ # Load Sentiment Analysis model
11
+ sentiment_model = "cardiffnlp/twitter-roberta-base-sentiment-latest"
12
+ sentiment_analyzer = pipeline("sentiment-analysis", model=sentiment_model, device=device)
13
 
14
+ def transcribe_and_analyze(audio):
15
+ # Transcribe audio to text
16
+ transcription = asr(audio)["text"]
17
+
18
+ # Analyze sentiment of the transcription
19
+ sentiment = sentiment_analyzer(transcription)[0]
20
+
21
+ return transcription, sentiment["label"], sentiment["score"]
22
+
23
+ # Define Gradio interface
24
+ interface = gr.Interface(
25
+ fn=transcribe_and_analyze,
26
+ inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="Upload or Record Audio"), # Added upload option
27
+ outputs=[
28
+ gr.Textbox(label="Transcription"),
29
+ gr.Textbox(label="Sentiment"),
30
+ gr.Number(label="Confidence Score")
31
+ ],
32
+ title="Real-Time Audio Transcription and Sentiment Analysis - Sumit",
33
+ description="This application transcribes audio input and analyzes the sentiment of the transcribed text.",
34
+ live=True
35
  )
36
 
37
+ if __name__ == "__main__":
38
+ interface.launch()