Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 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()
|