import os import numpy as np import gradio as gr import stanza from simpletransformers.classification import ClassificationModel, ClassificationArgs import preprocessor as p def clean_text(text): text = text.replace("#", "") return p.clean(text) def softmax(x): return np.exp(x) / np.sum(np.exp(x), axis=0) def number_to_sentiment(number): sentiments = { '0': 'Negative', '1': 'Neutral', '2': 'Positive' } return sentiments[str(number)] def number_to_topic(number): topics = { '0': 'Abortions', '1': 'Taiwan', '2': 'Afghanistan', '3': 'Insurance', '4': 'Undefined' } return topics[str(number)] def text_processing(text): results = nlp(text) text = clean_text(text) number_of_sentiments = 0 number_of_sentences = 0 for i, sentence in enumerate(results.sentences): number_of_sentiments += int(sentence.sentiment) number_of_sentences += 1 sentiment = int(round(number_of_sentiments/number_of_sentences)) sentiment = number_to_sentiment(sentiment) predictions, raw_outputs = model.predict(text) print(predictions[0], raw_outputs[0]) softmax_pred = softmax(raw_outputs[0]) if softmax_pred.max() > 0.90: topic = number_to_topic(softmax_pred.argmax()) print(softmax_pred.argmax()) else: print(4) topic = number_to_topic(4) return f'Text topic: {topic}, text sentiment: {sentiment}' if __name__ == "__main__": eval_model_args = ClassificationArgs(max_seq_length=128, use_multiprocessing_for_evaluation=False, eval_batch_size=1) model = ClassificationModel( "xlnet", "./", use_cuda=False, args=eval_model_args ) stanza.download('en') nlp = stanza.Pipeline('en', processors='sentiment,tokenize,mwt', tokenize_no_ssplit=True) with gr.Blocks() as demo: with gr.Tab("Get text topic and sentiment"): text_input = gr.Textbox(label='Input text', placeholder='Put your text here') text_output = gr.Textbox(label='Output', placeholder="Topic and sentiment of the text") text_button = gr.Button("Run processing") text_button.click(text_processing, inputs=text_input, outputs=text_output) demo.launch()