Spaces:
Runtime error
Runtime error
Cld commited on
Commit ·
9f63569
1
Parent(s): 3d1c620
add files
Browse files- app.py +58 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
| 5 |
+
|
| 6 |
+
model_id = 'ClaudianoLeonardo/bert-finetuned_news_classifier-portuguese'
|
| 7 |
+
tokenizer_classifier = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
+
model_classifier = AutoModelForSequenceClassification.from_pretrained(model_id)
|
| 9 |
+
|
| 10 |
+
model_id2 = "ClaudianoLeonardo/whisper-finetuned-tiny-ptv2"
|
| 11 |
+
|
| 12 |
+
# Carregar modelos do Hugging Face
|
| 13 |
+
whisper_model = pipeline('automatic-speech-recognition', model = model_id2)
|
| 14 |
+
|
| 15 |
+
text_classification_model = AutoModelForSequenceClassification.from_pretrained(model_id)
|
| 16 |
+
text_classification_tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 17 |
+
|
| 18 |
+
id2label = {0:'economia', 1:'esportes', 2:'famosos', 3:'politica', 4:'tecnologia'}
|
| 19 |
+
|
| 20 |
+
def get_text(logits):
|
| 21 |
+
sigmoid = torch.nn.Sigmoid()
|
| 22 |
+
probs = sigmoid(logits.squeeze().cpu())
|
| 23 |
+
predictions = np.zeros(probs.shape)
|
| 24 |
+
predictions[np.where(probs >= 0.5)] = 1
|
| 25 |
+
predicted_labels = [id2label[idx] for idx, label in enumerate(predictions) if label == 1.0]
|
| 26 |
+
return predicted_labels[0]
|
| 27 |
+
|
| 28 |
+
# Função para realizar a inferência
|
| 29 |
+
def inference(audio):
|
| 30 |
+
# Realizar inferência no modelo Whisper para reconhecimento de fala
|
| 31 |
+
# Obter texto da saída do modelo Whisper
|
| 32 |
+
try:
|
| 33 |
+
sr, y = audio
|
| 34 |
+
except:
|
| 35 |
+
return "Erro ao carregar o áudio ou insira um áudio válido"
|
| 36 |
+
|
| 37 |
+
y = y.astype(np.float32)
|
| 38 |
+
y /= np.max(np.abs(y))
|
| 39 |
+
transcribed_text = whisper_model({"sampling_rate": sr, "raw": y})["text"]
|
| 40 |
+
|
| 41 |
+
# Realizar inferência no modelo de classificação de texto
|
| 42 |
+
text_input = text_classification_tokenizer(transcribed_text, return_tensors="pt", padding=True)
|
| 43 |
+
text_output = text_classification_model(**text_input)
|
| 44 |
+
# Obter a classe predita
|
| 45 |
+
predicted_class = get_text(text_output["logits"])
|
| 46 |
+
|
| 47 |
+
return f"Texto transcrito: {transcribed_text}\nClasse predita: {predicted_class}"
|
| 48 |
+
|
| 49 |
+
# Criar interface gráfica com Gradio
|
| 50 |
+
iface = gr.Interface(
|
| 51 |
+
fn=inference,
|
| 52 |
+
inputs=gr.Audio(),
|
| 53 |
+
outputs="text",
|
| 54 |
+
live=True
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# Executar a interface
|
| 58 |
+
iface.launch(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torchaudio
|
| 3 |
+
gradio
|
| 4 |
+
torch
|