TopicDeTextor / app.py
gonzalocordova's picture
feat: adding examples in the UI
05ebd8a
import gradio as gr
import NLPutils as nlp
import numpy as np
model = nlp.load_model()
print("MODEL LOADED")
topic_keywords_embed = nlp.get_topic_keywords_embed(model)
topic_centroids = nlp.get_topic_centroids(topic_keywords_embed)
topic_to_id = {'ComputerScience': 0,
'Arts': 1,
'Astronomy': 2,
'Biology': 3,
'Business': 4,
'Chemistry': 5,
'Math&Statistics': 6,
'Philosophy&Ethics': 7,
'Gender': 8,
'Politics&Society': 9,
'Psychology': 10,
'Literature': 11,
'Pharma': 12,
'Physics': 13}
id_to_topic = {v: k for k, v in topic_to_id.items()}
stop_words = nlp.get_stopwords()
def softmax(x):
"""Compute softmax values for each sets of scores in x."""
return np.exp(x) / np.sum(np.exp(x), axis=0)
def predict_fn(text):
"""
This function will predict the class of an image
:param image_path: The path of the image
:param raw_output: If True, it will return the raw output of the model
:return: Tuple (real class, predicted class, probability)
"""
text = [text]
text = nlp.remove_stopwords(list(nlp.sent_to_words([nlp.clean(doc) for doc in text])), stop_words)
probs = nlp.sentence_cats_probs(text[0], topic_centroids, model, topic_to_id)
# Dictionary with topic as key and probability as value
result = dict()
for i in range(len(probs)):
result[id_to_topic[i]] = probs[i]
# sort result by value (highest to lowest)
result = dict(sorted(result.items(), key=lambda item: item[1], reverse=True))
# get top 3 topics dict
top3 = dict(list(result.items())[:3])
ids = list(top3.keys())
probs = list(top3.values())
# apply softmax to top 3 topics
probs = softmax(probs)
# update top 3 topics dict with softmax probabilities
print(probs)
print(ids)
top3 = dict(zip(ids, probs))
print(top3)
return {id : round(top3[id], 2) for id in top3}
examples = [
"Biologists have sought to study and classify the various forms of life, from prokaryotic organisms such as archaea and bacteria to eukaryotic organisms such as protists, fungi, plants, and animals.",
"Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation via the off-side rule. Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly procedural), object-oriented and functional programming.",
"One notable example of Latino involvement in far-right racist politics can be found in the early 1950s, also in the Dallas area. Pete Garcia was working with the South Dallas Adjustment League to prevent Black Texans from moving into the community. Mr. Garcia, a 26-year-old machinist, placed “For Whites Only” signs in the yards of white neighborhoods. Along with other members of the S.D.A.L., he menaced a white man who threatened to sell a home in a white neighborhood to a Black family, and he was indicted on a charge of bombing the home of another Black family who had dared to purchase a home in a white neighborhood.",
"Cosmology is the study of the vast universe, which includes galaxies, nebulae, black holes, and supernovae, all held together by the force of gravity, as planets and stars continue to form and evolve."
]
gr.Interface(predict_fn, gr.inputs.Textbox(label="Input Text"), outputs="label", examples=examples).launch()