covidbot / app.py
waleedgeo's picture
Update app.py
d92a42f
import random
import gradio as gr
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow import keras
import tensorflow as tf
from sklearn.preprocessing import LabelEncoder
import string
import json
import time
import pandas as pd
import numpy as np
model = tf.keras.models.load_model('saved_model/my_model')
with open('covid_responses.json', 'rb') as f:
responses = json.load(f)
data = pd.read_csv('data.csv')
tokenizer = Tokenizer(num_words=2000)
tokenizer.fit_on_texts(data['inputs'])
le = LabelEncoder()
le.fit_transform(data['tags'])
def model_output(prediction_input):
#removing punctuation and converting to lowercase
prediction_input = [letters.lower() for letters in prediction_input if letters not in string.punctuation]
prediction_input = ''.join(prediction_input)
texts_p = []
texts_p.append(prediction_input)
#tokenizing and padding
prediction_input = tokenizer.texts_to_sequences(texts_p)
prediction_input = np.array(prediction_input).reshape(-1)
prediction_input = pad_sequences([prediction_input],18)
#getting output from model
output = model.predict(prediction_input)
output = output.argmax()
#finding the right tag and predicting
response_tag = le.inverse_transform([output])[0]
final_response = str(random.choice(responses[response_tag]))
return final_response
dialog_app = gr.Interface(model_output,
gr.Textbox(placeholder="Enter your question"),
"text",
examples=[["Hello"],["What is COVID-19?"], ["What is the treatment?"]],
title="COVID-19 Chatbot",
description="Ask your questions about COVID-19",
)
dialog_app.launch()