File size: 5,927 Bytes
c13a669 d588f01 c13a669 d588f01 c13a669 d588f01 c13a669 d588f01 c13a669 d588f01 c13a669 d588f01 c13a669 d588f01 c13a669 d588f01 c13a669 d588f01 c13a669 d588f01 c13a669 d588f01 c13a669 d588f01 c13a669 4fc596d d588f01 c13a669 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
import gradio as gr
import numpy as np
import pandas as pd
from save_prediction import save_data_with_prediction
import datetime
import plotly.express as px
# global variables
data = pd.DataFrame()
last_term = ""
terms = []
shared_output = None
def create_plot(data):
today = datetime.date.today()
year = today.year - 1
fig = px.scatter(data, x='Year', y='norm_publications_count', color='Term',
title='Scatter Plot with Color and Symbols')
fig.update_layout(
title="Outbreak in ",
xaxis_title="year",
yaxis_title="normalized publication count",
)
# Add a horizontal line at y=15
fig.add_shape(type="line", x0=year + 0.5, x1=year + 0.5, y0=0, y1=data['norm_publications_count'].max(),
line=dict(color="blue", width=2, dash="dot"))
return fig
def add_term_to_plot(custom_term,choice):
global shared_output
global last_term
global terms
global data # Indicate that you want to work with the global 'data' variable
term=""
print("choice")
print(choice)
print("last_term")
print(terms)
if not custom_term or custom_term in terms:
if not choice:
#raise gr.Error("You didn't insert new term")
return create_plot(data)
if choice in terms:
#raise gr.Error("Your choice is already in the graph")
return create_plot(data)
else:
term = choice
print ("term is choice "+term)
elif not choice or choice in terms:
if custom_term in terms:
#raise gr.Error("The term you inserted is already in the graph")
return create_plot(data)
else:
term = custom_term
print ("term is custom_term "+term)
#if both new
if not term:
#raise gr.Error("you inserted new terms in both options, the custom term is shown")
term = custom_term
print(term)
if len(terms)>10:
#raise gr.Error("The maximum terms number is 10")
return create_plot(data)
else:
last_term = term
terms = terms+[term]
(print(terms))
# get year
today = datetime.date.today()
year = today.year - 1
no_space_term = term.replace(" ", "_")
print(no_space_term)
path = "model_data/data_with_predictions_" + no_space_term + str(year) + ".csv"
try:
save_data_with_prediction(no_space_term)
except:
raise gr.Error("There is no data about your term in Pubmed. "
"Please enter another term and the trend graph will be displayed")
new_term_df = pd.read_csv(path)
data = pd.concat([data, new_term_df], ignore_index=True) # Concatenate DataFrames
data = data[data['Year'] >= (year-40)]
fig = create_plot(data)
shared_output = fig
# update_choice2.update()
return fig
def delete_term(term):
global shared_output
global terms
global data
if term in terms:
terms.remove(term)
data = data[data["Term"] != term]
fig = create_plot(data)
shared_output = fig
return fig
else:
raise gr.Error(term + " is not exist in the graph!")
def clear_all():
global shared_output
global terms
global data
if len(terms) > 0:
terms = []
data = pd.DataFrame(columns=["Term","Year","norm_publications_count","Data"])
fig = create_plot(data)
shared_output = fig
return fig
else:
raise gr.Error("The trends graph is already empty")
return None
predefined_terms = [(term, term) for term in np.unique(pd.read_csv("training_data_all.csv")["Term"].to_numpy())]
description= "Welcome to the predictor of trends in science!\n\n\n" \
"This tool predicts the popularity of fields in science for the next 6 years.\n" \
"Get predictions of scientific popularity for any term!\n"\
"Up to 10 terms can be inserted into the same graph.\n\n" \
"Popularity of a term is defined as the number of publications in PubMed per year for this term, \n" \
"normalized to 100, 000 publications..\n\n"\
"For details of the model and methodology see our paper. If you use us, please cite us!:\n"\
"Ofer D, Kaufman H, Linial, M. Whats next? Forecasting scientific research trends. Helyion (Dec 2023).\n Available from:\n"\
"https://doi.org/10.1016/j.heliyon.2023.e23781\n\n"\
"contact us at:\n"\
"Hadasa.kaufman@mail.huji.ac.il\n\n\n"\
"Developed by Hadasa Kaufman & Dan Ofer"
with gr.Blocks() as demo:
# Create a Row component to divide the interface into two columns
with gr.Row():
# Create a Column for the left side (description)
with gr.Column():
gr.Image("logo_SciTrends.png")
gr.Text(description,label="App Description")
# Create a Column for the right side (input components and plot)
with gr.Column():
favicon="logo_SciTrends.png" # Specify the path to your logo image
txt1 = gr.components.Textbox(label="Insert a term")
choice1 = gr.components.Dropdown(label="or choose an example term", choices=predefined_terms)
btn = gr.Button(value="Submit")
out1 = gr.Plot(label="plot")
btn.click(add_term_to_plot, inputs=[txt1, choice1], outputs=out1 )
remove_term = gr.components.Textbox(label="Insert a term to remove")
btn = gr.Button(value="Remove term")
btn.click(delete_term, inputs=remove_term, outputs=out1)
btn = gr.Button(value="clear all")
btn.click(clear_all, outputs=out1)
live = True
if __name__ == "__main__":
demo.launch(share=True)
|