arham061's picture
Update app.py
538482c
raw
history blame
No virus
991 Bytes
import gradio as gr
from transformers import BertTokenizer, BertForSequenceClassification
import torch
# Load the model and tokenizer from the folder in Hugging Face space
model_folder = "FYP_Model" # Replace with your actual username and model name
tokenizer = BertTokenizer.from_pretrained(model_folder)
model = BertForSequenceClassification.from_pretrained(model_folder)
def classify_audio(audio_file):
# Read the audio file and tokenize it
audio_content = audio_file.read()
inputs = tokenizer(audio_content, return_tensors="pt", truncation=True)
# Perform inference using the loaded model
outputs = model(**inputs)
logits = outputs.logits
predicted_class = torch.argmax(logits).item()
return f"Predicted class: {predicted_class}"
# Gradio Interface
iface = gr.Interface(
fn=classify_audio,
inputs=gr.Audio(type="file", label="Upload or Record Audio"),
outputs=gr.Textbox(),
live=True,
)
# Launch the Gradio interface
iface.launch()