import gradio as gr import hopsworks from datasets import load_dataset import pandas as pd project = hopsworks.login() fs = project.get_feature_store() dataset_api = project.get_dataset_api() dataset = load_dataset("torileatherman/sentiment_analysis_batch_predictions", split='train') predictions_df = pd.DataFrame(dataset) grouped_predictions = predictions_df.groupby(predictions_df.Sentiment) #positive_preds = grouped_predictions.get_group(2) #neutral_preds = grouped_predictions.get_group(1) negative_preds = grouped_predictions.get_group(0) predictions_df['Sentiment'] = predictions_df['Sentiment'].map({0: 'Negative', 1: 'Neutral', 2: 'Positive'}) def article_selection(sentiment): if sentiment == "Positive": predictions = negative_preds predictions_df_url0 = predictions['Url'].iloc[0] predictions_df_url1 = predictions['Url'].iloc[1] predictions_df_url2 = predictions['Url'].iloc[2] return predictions_df_url0, predictions_df_url1, predictions_df_url2 elif sentiment == "Negative": predictions = negative_preds predictions_df_url0 = predictions['Url'].iloc[0] predictions_df_url1 = predictions['Url'].iloc[1] predictions_df_url2 = predictions['Url'].iloc[2] return predictions_df_url0, predictions_df_url1, predictions_df_url2 else: predictions = negative_preds predictions_df_url0 = predictions['Url'].iloc[0] predictions_df_url1 = predictions['Url'].iloc[1] predictions_df_url2 = predictions['Url'].iloc[2] return predictions_df_url0, predictions_df_url1, predictions_df_url2 def manual_label(): # Selecting random row from batch data random_sample = predictions_df.sample() random_headline = random_sample['Headline_string'].iloc[0] random_prediction = random_sample['Sentiment'].iloc[0] return random_headline, random_prediction def thanks(sentiment): return f"""Thank you for making our model better!""" description1 = ''' This application recommends news articles depending on the sentiment of the headline. Enter your preference of what type of news articles you would like recommended to you today: Positive, Negative, or Neutral. ''' description2 = ''' This application will show you a random news headline and our predicted sentiment. In order to improve our model, mark the real sentiment of this headline! ''' suggestion_demo = gr.Interface( fn=article_selection, title = 'Recommending News Articles', inputs = gr.Dropdown(["Positive","Negative","Neutral"], label="What type of news articles would you like recommended?"), outputs = [gr.Textbox(label="Recommended News Articles"),gr.Textbox(),gr.Textbox()], description = description1 ) with gr.Blocks() as manual_label_demo: generate_btn = gr.Button('Show me a headline!') generate_btn.click(fn=manual_label, outputs=[gr.Textbox(label="News Headline"),gr.Textbox(label="Our Predicted Sentiment")]) manual_label_demo1 = gr.Interface( fn=thanks, title="Manually Label a News Article", inputs=[gr.Textbox(label = "Paste in URL of news article here."), gr.Dropdown(["Positive","Negative","Neutral"], label="Select the sentiment of the news article.")], outputs = gr.Textbox(label="Output"), description = description2 ) demo = gr.TabbedInterface([suggestion_demo, manual_label_demo], ["Get recommended news articles", "Help improve our model"]) demo.launch()