|
import os |
|
os.system("pip install git+https://github.com/openai/whisper.git") |
|
import gradio as gr |
|
import whisper |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
from transformers import pipeline |
|
|
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment-latest") |
|
model_nlp = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment-latest") |
|
|
|
|
|
|
|
model = whisper.load_model("small") |
|
|
|
|
|
def inference_audio(audio): |
|
audio = whisper.load_audio(audio) |
|
audio = whisper.pad_or_trim(audio) |
|
|
|
mel = whisper.log_mel_spectrogram(audio).to(model.device) |
|
|
|
_, probs = model.detect_language(mel) |
|
|
|
options = whisper.DecodingOptions(fp16 = False) |
|
result = whisper.decode(model, mel, options) |
|
|
|
return result.text |
|
|
|
def inference_text(audio): |
|
text =inference_audio(audio) |
|
|
|
sentiment_task = pipeline("sentiment-analysis", model=model_nlp, tokenizer=tokenizer) |
|
res=sentiment_task(text)[0] |
|
|
|
return res['label'],res['score'] |
|
|
|
audio = gr.Audio( |
|
label="Input Audio", |
|
show_label=False, |
|
source="microphone", |
|
type="filepath" |
|
) |
|
|
|
|
|
app=gr.Interface(title="Sentiment Audio Analysis",fn=inference_text,inputs=[audio], outputs=["text","text"]) |
|
|
|
|
|
|
|
app.launch() |