Spaces:
Runtime error
Runtime error
import gradio as gr | |
from transformers import pipeline | |
from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
import json | |
import os | |
import torch | |
data_dict = {} | |
with open('./intents.json', 'r') as file: | |
data = json.load(file) | |
intents_dict = data | |
tokenizer = AutoTokenizer.from_pretrained("roberta-base") | |
model = AutoModelForSequenceClassification.from_pretrained("./") | |
def preprocess(text): | |
inputs = tokenizer(text, padding=True, truncation=True, return_tensors="pt") | |
return inputs | |
def postprocess(outputs): | |
logits = outputs.logits | |
predicted_labels = logits.argmax(dim=1).tolist() | |
return predicted_labels | |
def predict(text): | |
inputs = preprocess(text) | |
with torch.no_grad(): | |
outputs = model(**inputs) | |
predicted_labels = postprocess(outputs) | |
ans = intents_dict[str(predicted_labels[0])] | |
return ans | |
from transformers import pipeline | |
p = pipeline(model="openai/whisper-medium") | |
def transcribe(text,audio): | |
if audio: | |
t = p(audio)['text'] | |
ans = predict(t) | |
elif text: | |
ans = predict(text) | |
else: | |
ans = "please give input" | |
return ans | |
get_intent = gr.Interface(fn = transcribe, | |
inputs=[gr.Textbox(label="Enter Text Input", type="text"),gr.Audio(source="microphone", type="filepath")], | |
outputs="text").launch() | |