def truncate_10(n,up_down): temp = floor(log10(n)) if up_down == "up": round_op = lambda x: ceil(x) else: round_op = lambda x: floor(x) return float(round_op(n/10**temp)*(10**temp)) #%% scope = st.sidebar.selectbox("Scope of Analysis:", ("World","Spain")) #scope = "Spain" with st.spinner('Data is being loaded...'): time.sleep(3) df_covid19_region,region_title,regions,regions_def = hp.get_data(scope) #%% Streamlit inputs %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ################################################################################################ st.title("DataViz App Covid-19 🦠") st.header("Analysis of "+scope+" data") viz_option = st.sidebar.selectbox("Visualisation: ", ("Map","cumulative", "day delta")) multiselection = st.sidebar.multiselect( "Choose the regions:", regions, default=regions_def )
if bool(logged_in_data) and logged_in_data['username'] == USERNAME and logged_in_data['password'] == PASSWORD: session_state.isLoggedIn = True rerun() if session_state.isLoggedIn: st.header("TEST GPT3 LinkedIn") st.subheader("HOW TO START") st.markdown(":small_red_triangle_down: Put the name, the company and the time of the user and then press the predict button.") genre = st.sidebar.radio("OPTIONS",('Link', 'Upload csv file')) if genre == 'Link': session_state.fl2 = [] linkd = st.text_input("put the name from linkedin profile") if linkd: if st.button('Scrap'): p =linkd.find("w.") with st.spinner('Wait...'): session_state.x = scrap([linkd[(p+2):]]) st.success('Done!') else: input_buffer = st.file_uploader("Upload a file", type=("csv")) if input_buffer: dfa= pd.read_csv(input_buffer, sep=',',prefix=None) dfa.columns = ['LINKS'] p1 = dfa["LINKS"].values.tolist() value = [] for j in p1: p =j.find("w.") value.append(j[(p+2):]) if st.button('Scrap'): with st.spinner('Wait...'):
import datetime as dt import re import pandas as pd import streamlit as st from flair.data import Sentence from flair.models import TextClassifier from twitterscraper import query_tweets st.title('Twitter Sentiment Analysis') st.subheader("An ML app to detect the sentiments behind the tweets using Flair -a state-of-art NLP library and a query finder that crawls for specified tweets") with st.spinner("Loading Classification Model....."): classifier = TextClassifier.load('model-saves/final-model.pt') allowed_chars = ' AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789~`!@#$%^&*()-=_+[]{}|;:",./<>?' punct = '!?,.@#' maxlen = 280 def preprocess(text): return ''.join([' ' + char + ' ' if char in punct else char for char in [char for char in re.sub(r'http\S+', 'http', text, flags=re.MULTILINE) if char in allowed_chars]])[:maxlen] st.subheader("Single tweet classification") tweet_input = st.text_input('PUT YOUR TWEET :- ') if tweet_input != '': sentence = Sentence(preprocess(tweet_input)) with st.spinner('Predicting...'): classifier.predict(sentence)
def main(): # DO NOT REMOVE the 'Recommender System' option below, however, # you are welcome to add more options to enrich your app. page_options = ["Recommender System", "Solution Overview"] # ------------------------------------------------------------------- # ----------- !! THIS CODE MUST NOT BE ALTERED !! ------------------- # ------------------------------------------------------------------- page_selection = st.sidebar.selectbox("Choose Option", page_options) if page_selection == "Recommender System": # Header contents st.write('# Movie Recommender Engine') st.write('### EXPLORE Data Science Academy Unsupervised Predict') st.image('resources/imgs/Image_header.png', use_column_width=True) # Recommender System algorithm selection sys = st.radio( "Select an algorithm", ('Content Based Filtering', 'Collaborative Based Filtering')) # User-based preferences st.write('### Enter Your Three Favorite Movies') movie_1 = st.selectbox('First Option', title_list[14930:15200]) movie_2 = st.selectbox('Second Option', title_list[25055:25255]) movie_3 = st.selectbox('Third Option', title_list[21100:21200]) fav_movies = [movie_1, movie_2, movie_3] # Perform top-10 movie recommendation generation if sys == 'Content Based Filtering': if st.button("Recommend"): try: with st.spinner('Crunching the numbers...'): top_recommendations = content_model( movie_list=fav_movies, top_n=10) st.title("We think you'll like:") for i, j in enumerate(top_recommendations): st.subheader(str(i + 1) + '. ' + j) except: st.error("Oops! Looks like this algorithm does't work.\ We'll need to fix it!") if sys == 'Collaborative Based Filtering': if st.button("Recommend"): try: with st.spinner('Crunching the numbers...'): top_recommendations = collab_model( movie_list=fav_movies, top_n=10) st.title("We think you'll like:") for i, j in enumerate(top_recommendations): st.subheader(str(i + 1) + '. ' + j) except: st.error("Oops! Looks like this algorithm does't work.\ We'll need to fix it!") # ------------------------------------------------------------------- # ------------- SAFE FOR ALTERING/EXTENSION ------------------- if page_selection == "Solution Overview": st.title("Solution Overview") st.write( "I tried to approach this task with an open mind. While having to juggle a full time job, studies and being a parent of 6 it wasnt easy to find the time to do what needed to be done. None the less I am happy that I managed to get this app deployed to my S3 bucket and running on my instance" )
<a href="{target_url}" target="_blank"> <img src="data:image/{img_format};base64,{bin_str}" width = "75" /> </a>''' return html_code png_html = get_img_with_href('n.png', 'https://www.jimisinith.com/about') col1, col2, col3 = st.sidebar.beta_columns([3,7,1]) with col1: st.write("") with col2: st.markdown(png_html, unsafe_allow_html=True) with col3: st.write("") with st.spinner('Analysis in progress...'): with st.spinner('Step 1: PCA and K-means clustering in progress...'): # Import prices company_prices = pd.read_csv('https://2e6635b7-3bcd-4481-b953-7c05e476bfef.usrfiles.com/ugd/2e6635_70eef857c10f4c0b9ad14991299a4f97.csv') company_prices.set_index('date', inplace = True) # Import returns company_returns = pd.read_csv('https://2e6635b7-3bcd-4481-b953-7c05e476bfef.usrfiles.com/ugd/2e6635_adf17d52a2e74ccd9e86f8edff4721b9.csv') company_returns.set_index('date', inplace = True) # Import company names company_names = pd.read_csv('https://2e6635b7-3bcd-4481-b953-7c05e476bfef.usrfiles.com/ugd/2e6635_e91fd49e64fd4932b7ed31d4070f72f2.csv') company_names.drop('Unnamed: 0', axis = 1, inplace = True) company_names.set_index('Symbol', inplace = True)
def main(): df = load_data(equity_list) sl_initial_amount = 10000 st.sidebar.markdown("### Set parameters for simulation") sl_leverage = st.sidebar.slider('Leverage', 0.0, 1.0, 1.0) sl_start_dt = st.sidebar.date_input( 'Choose investment start date', date(start_date_year, start_date_month, start_date_day), date(start_date_year, start_date_month, start_date_day)) sl_end_dt = st.sidebar.date_input( 'Choose investment end date', (end_date - timedelta(days=2)), date(start_date_year, start_date_month, start_date_day)) sl_select_tickers = st.sidebar.multiselect( "Select tickers to compare performance", equity_list.Name.tolist(), equity_list.Name.tolist()[:3]) if (np.datetime64(sl_start_dt) > np.datetime64(sl_end_dt)): st.write(f""" _Error: Start Date greater than End Date_ * Investment Start Date = {sl_start_dt} * Investment End Date = {sl_end_dt} Please fix the dates before proceeding """) elif (np.datetime64(sl_start_dt) < (date(start_date_year, start_date_month, start_date_day))): st.write(f""" _Error: Please select a later start date_ * Investment Start Date={sl_start_dt} * Possible Start Date={start_date} Please fix the dates before proceeding """) elif (np.datetime64(sl_end_dt) > np.datetime64(end_date)): st.write(f""" _Error: End Date should be yesterday or earlier_ * Investment End Date = {sl_end_dt} Please fix the dates before proceeding """) else: df_slice = df.loc[(df.index >= np.datetime64(sl_start_dt)) & (df.index <= np.datetime64(sl_end_dt))] st.write(f""" ### Parameters * Initial Amount = {locale.currency(sl_initial_amount, grouping=True)} * Leverage = {sl_leverage} * Investment Start Date = {sl_start_dt} * Investment End Date = {sl_end_dt} """) # cleanup data for adjusted change values stock_analyze_df = df_slice.iloc[:, :len(equity_list)].copy() stock_analyze_df.columns = stock_analyze_df.columns.droplevel() stock_analyze_df = stock_analyze_df.fillna(method="ffill", inplace=False) # calc change percentage stock_analyze_pc_df = stock_analyze_df.apply( lambda x: (x - x.shift(1)) / x.shift(1)) stock_analyze_pc_df = stock_analyze_pc_df.fillna(0) # run simulation on button press if st.sidebar.button("Run Simulation", "run-exp-btn"): gain_data = run_experiment(sl_initial_amount, sl_leverage, stock_analyze_pc_df) stock_gain_df = pd.DataFrame(gain_data) stock_gain_df["date"] = stock_analyze_pc_df.index with st.spinner("Running Simulation..."): plot_avgs(stock_gain_df, sl_select_tickers)
def show(session_state): """Run this function for showing the sentence embedding section in the app """ NLU_MODEL_NAMES = ["bert", "electra", "elmo", "glove", "xlnet", "albert"] # SIDEBAR st.sidebar.write(""" -------------- # Setup *Start here to select your project setup. You can choose between document or sentence embeddings and a vast variety of pre trained nlp models* """) st.sidebar.header("Step 1") model_names = st.sidebar.multiselect( "Select one or more models", NLU_MODEL_NAMES, session_state.sent_embed_selected_model_names.split( ) # Remember selection ) session_state.sent_embed_selected_model_names = ' '.join(model_names) st.sidebar.header("Step 2") INPUT_FORMATS = ["sentence", "document"] session_state.sent_embed_input_format = st.sidebar.radio( "Choose between sentence or document input before select calculate.", INPUT_FORMATS, index=int( np.where( np.array(INPUT_FORMATS) == session_state. sent_embed_input_format)[0][0]) # Remember selection ) # st.sidebar.header("Step 3") # LABELED_OPTIONS = ["Labeled data", "Unlabeled data"] # session_state.sent_embed_is_labeled = st.sidebar.radio( # "Are the data labeled?" # ,LABELED_OPTIONS # ,index=int(np.where(np.array(LABELED_OPTIONS) == session_state.sent_embed_is_labeled)[0][0]) # Remember selection # ) # st.sidebar.write("*Note: Select 'Labeled data' in order to hue the t-sne plot.*") # MAIN PAGE st.title("Sentence or Document Embeddings with NLU") st.info( "This is a comparison of some of the embedding developments of [**John Snow Lab**](https://nlu.johnsnowlabs.com/). \ Here you can find **BERT**, **ALBERT**, **ELMO**, **ELECTRA**, **XLNET** and **GLOVE** embeddings in one output. " "You can download the output or use the result for NLP model selection." ) st.write(""" ## References - [BERT Paper](https://arxiv.org/pdf/1810.04805.pdf) - [ALBERT Paper](https://openreview.net/forum?id=H1eA7AEtvS) - [ELMO Paper](https://arxiv.org/abs/1802.05365) - [ELECTRA Paper](https://arxiv.org/abs/2003.10555) - [XLNET Paper](https://arxiv.org/pdf/1906.08237.pdf) - [GLOVE Paper](https://nlp.stanford.edu/pubs/glove.pdf) """) # Load the nlu models: show just if # a) at least one model is selected OR # b) at least one model has been loaded if session_state.sent_embed_selected_model_names or session_state.sent_embed_loaded_model_names: st.header("Load a model") btn_load = st.button("Download selected model(s) from AWS", key="btn_load") # Case: at least one model is already loaded AND download button is seleced without any model selection if btn_load and not session_state.sent_embed_selected_model_names: with st.spinner( "**Warning**: No model selected. Please select first at least one embedding model from the sidebar!" ): time.sleep(3) btn_load = False # Case: selected model already loaded AND download button is selected if btn_load and (session_state.sent_embed_selected_model_names == session_state.sent_embed_loaded_model_names): with st.spinner( "**Info**: Selected models '{}' already loaded. Stop request." .format(session_state.sent_embed_selected_model_names)): time.sleep(3) btn_load = False # Case: load selected model if btn_load: with st.spinner( "Download started this may take some minutes ... :coffee:" ): session_state.sent_embed_pipe = nlu.load( session_state.sent_embed_selected_model_names) session_state.sent_embed_loaded_model_names = ' '.join( model_names) # Reset results if exist: txt input session_state.sent_embed_csv_input = pd.DataFrame() session_state.sent_embed_csv_out = pd.DataFrame() session_state.sent_embed_csv_is_predicted = False session_state.sent_embed_csv_label_column_name = "-" session_state.sent_embed_csv_txt_column_name = "-" # Run data input Flow: just if at least one model is loaded; if session_state.sent_embed_loaded_model_names: st.success("**Info**: loaded models are: {} ".format( session_state.sent_embed_loaded_model_names)) ######################## # Flow: csv input flow # ######################## st.header("Get Embeddings from CSV file here!") uploaded_file = st.file_uploader("Choose a CSV file to upload", type="csv") # st.write("DEBUG:", uploaded_file) # st.write("DEBUG:", session_state.sent_embed_csv_input) # No file selected if uploaded_file is None: st.info("Upload a CSV.") # Clear cache: User removed seletced file if len(session_state.sent_embed_csv_input) > 0: session_state.sent_embed_csv_input = pd.DataFrame() # After file selection, read CSV one time if uploaded_file and len(session_state.sent_embed_csv_input) == 0: session_state.sent_embed_csv_input = pd.read_csv( uploaded_file, sep=",", header=[0], encoding="utf-8", dtype="unicode") # After CSV has been loaded: if len(session_state.sent_embed_csv_input) > 0: st.write(session_state.sent_embed_csv_input) # Map Column st.write('**Map Column**') COLUMNS_NAMES = [ "-" ] + session_state.sent_embed_csv_input.columns.tolist() session_state.sent_embed_csv_txt_column_name = st.selectbox( "Select text column", COLUMNS_NAMES, index=int( np.where( np.array(COLUMNS_NAMES) == session_state.sent_embed_csv_txt_column_name)[0] [0]) # Remember selection ) # if session_state.sent_embed_is_labeled == "Labeled data": # session_state.sent_embed_csv_label_column_name = st.selectbox("Select label column" # ,COLUMNS_NAMES # ,index = int(np.where(np.array(COLUMNS_NAMES) == session_state.sent_embed_csv_label_column_name)[0][0]) # Remember selection # ) # Get prediction # NOTE: btn_pred state not cached for single prediction (state will return to false after one time trigger) if session_state.sent_embed_csv_txt_column_name != "-": btn_pred = st.button("Calculate", key="btn_predict") if btn_pred: session_state.sent_embed_csv_is_predicted = False with st.spinner("Calculation started ... :coffee:"): session_state.sent_embed_csv_out = session_state.sent_embed_pipe.predict( session_state.sent_embed_csv_input.doc.tolist( ), output_level=session_state. sent_embed_input_format, positions=True) session_state.sent_embed_csv_is_predicted = True if session_state.sent_embed_csv_is_predicted: st.success("Calculation done!") # Results st.header("Visualize Embeddings for the first 10 input") st.dataframe(session_state.sent_embed_csv_out.head(10)) # # Draw Subplots # st.header("t-SNE plot for each embeddings") # predictions = session_state.sent_embed_csv_out # EMBED_COL_NAMES = [c for c in predictions.columns if c.endswith("_embeddings")] # Infer the embedding column names # n_plots = len(EMBED_COL_NAMES) # fig, axs = plt.subplots(ncols = 2 if n_plots == 4 else min(n_plots, 3) , nrows = 1 if n_plots <= 3 else 2 ) # subplot_idx_dict = {} # subplot_idx_dict[2] = [0, 1] # subplot_idx_dict[3] = [0, 1, 2] # subplot_idx_dict[4] = [(0,0), (0,1), (1,0), (1,1)] # subplot_idx_dict[5] = [(0,0), (0,1), (0,2), (1,0), (1,1)] # subplot_idx_dict[6] = [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)] # for idx, emb_c in enumerate(EMBED_COL_NAMES): # t_embedd = get_tsne_df( # predictions = predictions # ,embd_columns = emb_c # ,hue_column = session_state.sent_embed_csv_input[session_state.sent_embed_csv_label_column_name] if (session_state.sent_embed_is_labeled == "Labeled data") else None) # if n_plots == 1: # ax = axs # elif n_plots in [2,3]: # 1 row # ax = axs[subplot_idx_dict[n_plots][idx]] # else: # row and column # subpl_r, subpl_c = subplot_idx_dict[n_plots][idx] # ax = axs[subpl_r][subpl_c] # ax = sns.scatterplot(data = t_embedd, x = 'x', y = 'y', ax = ax, c = t_embedd.index.tolist(), s = 100) # ax.set_title('T-SNE {}'.format(emb_c)) # st.pyplot(fig) st.header("Download Embedding Table") link = get_table_download_link( session_state.sent_embed_csv_out) st.write(link, unsafe_allow_html=True)
def app(): st.title('Traffic Speed Prediction') st.write("> Let's try predicting some traffic speed!") current_dir = dirname(dirname(abspath(__file__))) d = dirname(dirname(abspath(__file__))) print(d) dd = dirname(dirname(dirname(abspath(__file__)))) print(dd) ddd = os.path.join(dd, 'truncated_data') print(ddd) # Sidebar -------------------------------------------------------------------------------- st.sidebar.title("Model Options") st.sidebar.write("Select your prediction options below") st.sidebar.write( "> There are a few components to this model to indicate how much past data you want to input, and how far in the future you want to predict until." ) # Input Timesteps st.sidebar.subheader("Select Input Timesteps") st.sidebar.write( "How much past data to input into the model for prediction") # input_timestep_options = {1: "1 (5 minutes)", 2: "2 (10 minutes)", 3: "3 (15 minutes) - optimal"} input_timestep_options = {8: "8 (40 minutes) - default"} num_input_timesteps = st.sidebar.selectbox( "Number of Input Timesteps", options=list(input_timestep_options.keys()), format_func=lambda x: input_timestep_options[x]) # Output Timesteps st.sidebar.subheader("Select Output Timesteps") st.sidebar.write("How far do you want to predict the traffic speeds") output_timestep_options = { 1: "1 (5 minutes)", 2: "2 (10 minutes)", 3: "3 (15 minutes)", 4: "4 (20 minutes)" } num_output_timesteps = st.sidebar.selectbox( "Number of Output Timesteps", options=list(output_timestep_options.keys()), format_func=lambda x: output_timestep_options[x], index=3) # ----------------------------------------------------------------------------------- # Main Content -------------------------------------------------------------------------------- sample_zip_path = os.path.join(current_dir, 'data', 'sample', 'input.zip') st.write("### 1. Download Sample Input Files") st.write( "Here's a sample input file with the format that is required for the model prediction. You can download this, change the data and upload the zip file below." ) download_local_button(sample_zip_path, 'input.zip', 'Download files') st.write("___________________________") st.write("### 2. Upload Input Files") st.write("Please upload the zip file with the correct format below") zip_file = st.file_uploader("Upload file", type="zip") if zip_file is not None: # file_details = {'file_name': zip_file.name, 'file_type': zip_file.type} # Saving File saved_zip_path = os.path.join(current_dir, 'data', zip_file.name) with open(saved_zip_path, 'wb') as f: f.write(zip_file.getbuffer()) with ZipFile(saved_zip_path, 'r') as zip: # printing all the contents of the zip file zip.printdir() # extracting all the files print('Extracting all the files now...') unzip_path = os.path.join(current_dir, 'data', 'raw') zip.extractall(path=unzip_path) print('Done!') st.success('File Uploaded! You can now predict traffic speeds') # Predict Traffic Speeds here if st.button("Predict Traffic Speeds", key='predict'): with st.spinner("Please wait for prediction results...."): st.write('## Results') results, A, X, metadata = predict(num_timesteps_input=8, num_timesteps_output=4) # Display Metadata st.write('#### Metadata') metadata_expander = st.beta_expander("Click to expand", expanded=False) with metadata_expander: st.write( "Here's the metadata of the input data you have uploaded" ) df = pd.DataFrame(metadata).transpose() st.write(df) download_button(df, 'metadata.csv', 'Download metadata') # Display Results st.write('#### Predictions') predictions_expander = st.beta_expander("Click to expand", expanded=False) with predictions_expander: def loc_to_linestring(loc): coordArr = loc.split() coordArr = [float(coord) for coord in coordArr] return LineString([coordArr[1::-1], coordArr[3:1:-1]]) def plotGeoPerformance(metadata, speedbands): df = pd.DataFrame(metadata).transpose() df["speedbands"] = speedbands loc = df["start_pos"] + " " + df["end_pos"] linestrings = loc.apply(loc_to_linestring) gdf = gpd.GeoDataFrame(df, geometry=linestrings, crs="EPSG:4326") gdf = gdf.to_crs('EPSG:3857') fig, ax = plt.subplots(figsize=(10, 10)) gdf.plot(ax=ax, column="speedbands", legend=True, cmap="OrRd", legend_kwds={'label': 'speedbands'}) ax.set_xlabel("Longitude") ax.set_ylabel("Latitude") ctx.add_basemap(ax) plt.savefig("currentPrediction.png") timestep_speedbands = results.reshape( results.shape[2], results.shape[1]) plotGeoPerformance( metadata, timestep_speedbands[num_output_timesteps - 1]) st.write( "Below is a graph of the predicted traffic speedbands plotted on the roads of this geographical map. The colours of the roads represent the varying speedband numbers." ) st.image("currentPrediction.png") st.write( "Below is a table of the predicted traffic speedbands for the respective roads. Please refer to the metadata above for the index mappings" ) results = results[:, :, num_output_timesteps - 1] results = pd.DataFrame(results) st.write(results) download_button(results, 'predictions.csv', 'Download predictions')
def progress(prog_text): with st.spinner(prog_text): time.sleep(3)
def write(state): if state.task == "Regression": from pycaret.regression import predict_model, finalize_model,save_model elif state.task == "Classification": from pycaret.classification import predict_model, finalize_model,save_model else: from pycaret.clustering import predict_model,save_model def online_predict(model, input_df,target_type): """make prediction on online data Args: model (object): a trained model input_df (pd.DataFrame): the input dataframe for predicitons target_type (str): the type of training target Returns: str: predcition """ prediction_df = predict_model(model, data=input_df) if target_type == 'Regression' or target_type == 'Classification': predictions = prediction_df['Label'][0] else: predictions = prediction_df['Cluster'][0] return predictions if state.trained_model is not None: st.header("Make a Prediction on Given Input or Upload a File.") add_selectbox = st.sidebar.selectbox( "How would you like to predict?", ("Online", "Batch", "SaveModel") ) X_before_preprocess = state.X_before_preprocess target_name = state.y_before_preprocess ignore_columns = state.ignore_columns trained_model = state.trained_model if add_selectbox == "Online": with st.spinner("Predicting ..."): input_df = retrieve_train_element(X_before_preprocess, target_name, ignore_columns,state.task) output = "" if st.button("Predict"): output = online_predict(trained_model, input_df,state.task) output = str(output) st.success(f'The Prediction is **{output}**') if add_selectbox == 'Batch': file_upload = st.file_uploader('Upload csv file for prediciton', type=["csv", "xlsx"]) if file_upload is not None: file_extension = file_upload.name.split('.')[1] if file_extension == "csv": data = pd.read_csv(file_upload) else: data = pd.read_excel(file_upload) predictions = predict_model(trained_model, data=data) st.write(predictions) is_download = st.checkbox("Do You Want to Download the Prediction File?", value=False) if is_download: file_extension = st.selectbox("Choose Csv or Excel File to Download", options=[".csv",".xlsx"]) file_name = st.text_input("File Name",value="prediction",key=1) if file_name: href = download_button(predictions, file_name, "Download",file_extension) st.markdown(href, unsafe_allow_html=True) else: st.error("File Name cannot be empty!") if add_selectbox == "SaveModel": is_download = st.checkbox("Do You Want to Download the Model?", value=False) if is_download: file_name = st.text_input("File Name",value="",key=2) is_finalize = st.checkbox("Do You Want to Finalize the Model (not for clustering)?", value=False) if file_name: if is_finalize: finalized_model = finalize_model(trained_model) _,name = save_model(finalized_model, file_name) else: _,name = save_model(trained_model, file_name) with open(name, "rb") as f: e = joblib.load(f) href = download_button(e, file_name, "Download",".pkl",pickle_it=True) st.markdown(href, unsafe_allow_html=True) remove_cache = st.checkbox("Remove the Cache?", value=False) if remove_cache: p = Path(".").glob("*.pkl") for filename in p: filename.unlink() if len(list(p)) == 0: st.success("Delete the Cache File from Local Filesystem!") st.balloons() else: st.error("Please Give a File Name first!") else: st.error("Please Train a Model first!")
st.sidebar.write('----') st.sidebar.write( '*Review the source code [here](https://github.com/hmedrano/erddap-status-dashboard)*' ) st.sidebar.write('*By [Favio Medrano](https://agildev.mx/en)*') # # MAIN try: customCSS() titleDashboard() erddapurl, reloadActivated = serverURLWidget() with st.spinner('Loading metrics..'): sdf, statusPageLink = getStatusData(gremote, erddapurl, force=reloadActivated) showGenerals(sdf) plotMLDTimeseries(sdf) plotResponsesSvsF(sdf) plotMajorMinorTD(sdf) failed2LoadDatasets(sdf) showCredits(statusPageLink) except Exception as e: st.error(""" An error occurred :( : %s """ % str(e))
PSI_interaction_energy = st.sidebar.text_input("PSI-LHCII interaction (KT)",0) try: PSI_interaction_energy = float(PSI_interaction_energy) except: st.write("Unable to convert PSI-LHCII interaction energy to float") EXPERIMENT = "Custom_" + str(GRANA_SIZE )+ "_" + str(Stacking_Interaction_Energy)+"_"+str(LHCII_Binding_Interaction_Energy)+"_"+str(PSI_interaction_energy) Run_Analysis = st.sidebar.checkbox("Run Post Simulation Analysis") Run_Network_Analysis = st.sidebar.checkbox("Run Network Analysis") # width of grana, nm. Number_of_iterations = 11000001 # number of Monte Carlo steps, Note that data is only collected after 10M iterations. today = date.today() DATE = str(date.today().year) + "_" + str(date.today().month)+"_" + str(date.today().day)# a reference date in which the simulations are run. if st.button("Run Simulation"): with st.spinner('Running Simulation'): POPULATION1, POPULATION2 = TM.Run_Simulation(GRANA_SIZE,DATE,EXPERIMENT,Number_of_iterations,Stacking_Interaction_Energy,LHCII_Binding_Interaction_Energy,PSI_interaction_energy) if Run_Analysis: with st.spinner('Running Post-Simulation Analysis'): TM.Run_analysis(GRANA_SIZE,DATE,EXPERIMENT) if Run_Network_Analysis: with st.spinner('Running Networn Analysis'): TM.Run_graph_antenna_analysis(GRANA_SIZE,DATE,EXPERIMENT,PSII=True,PSI=True)
def sweet_16_individual_brackets(score_array, bracket_matrix, current_score_array, bracket_index, selected_outcome_matrix, likelihood_array): winner_dict = {} round_array = generate_round_array(16) for i in range(15): winner_dict[i] = None for i in range(0, 8): print(i) game_dict = all_games_dict[i] # winner = optional_winner_selectbox(game_name=game_dict['name'], team0=game_dict['team0'], # team1=game_dict['team1'], i=i, index=2) option_list = [game_dict['team0'], game_dict['team1'], 'None'] index = index_list_2021[i] if index == -1: index = len(option_list) - 1 winner = optional_winner_selectbox(game_name=game_dict['name'], game_type=game_type_dict[round_array[i]], option_list=option_list, i=-1, index=index) if winner == 'None': winner = None winner_dict[i] = winner for i in range(8, 15): print(i) game_dict = all_games_dict[i] print(game_dict) team0 = winner_dict[game_dict['team0']] team1 = winner_dict[game_dict['team1']] print(team0) print(team1) print(game_dict['name'].format(team0=team0, team1=team1)) print(team0 is not None) print(team1 is not None) if team0 is not None or team1 is not None: option_list = [] if team0 is None: team0 = 'Other' else: option_list.append(team0) if team1 is None: team1 = 'Other' else: option_list.append(team1) option_list.append('None') index = index_list_2021[i] if index == -1: index = len(option_list) - 1 winner = optional_winner_selectbox(game_name=game_dict['name'].format(team0=team0, team1=team1), game_type=game_type_dict[round_array[i]], option_list=option_list, i=-1, index=index) if winner == 'None': winner = None winner_dict[i] = winner # score_array = np.array([4,8,16,32]) for i in range(15): winner_key = winner_dict[i] if winner_key is not None: winner_key = winner_key.lower() winner_id = team_id_dict[winner_key] winner_idx = winner_id * 4 + round_array[i] winner_value = score_array[round_array[i]] print(selected_outcome_matrix.shape) selected_outcomes = selected_outcome_matrix[winner_idx, :] == winner_value likelihood_array = likelihood_array[selected_outcomes] selected_outcome_matrix = selected_outcome_matrix[:, selected_outcomes] print(selected_outcome_matrix.shape) st.subheader('Who should I root for?') with st.spinner('Calculating Outcomes...'): total_outcomes = selected_outcome_matrix.shape[1] bracket_pool_scores = load_or_generate_bracket_pool_scores(static_path, bracket_matrix, selected_outcome_matrix, current_score_array, force_generation=False) # df_money_chances = print_money_chances(bracket_list, bracket_pool_scores) likelihood_array = likelihood_array / np.sum(likelihood_array) # df_money_chances = print_money_chances(bracket_list, bracket_pool_scores[bracket_index, :], likelihood_array) sweet_16_case_dict = print_sweet_16_case_probabilities(bracket_index, bracket_matrix, bracket_pool_scores, selected_outcome_matrix, likelihood_array) game = 0 count_tracker = 0 # # team0_dict['win_paths'] = team_0_win_count # team0_dict['win_paths_delta'] = team_0_win_count - base_total_paths # team0_dict['win_percent'] = team_0_win_count / total_outcomes * 100 # team0_dict['win_percent_delta'] = team0_dict['win_percent'] - base_paths_percent # team0_dict['win_likelihood'] = np.sum(team_0_likeliood) * 100 # team0_dict['win_likelihood_delta'] = team0_dict['win_likelihood'] - base_likelihood # st.write('<p style="color:green">THIS TEXT WILL BE RED</p>', unsafe_allow_html=True)6 for key in sweet_16_case_dict.keys(): if count_tracker == 0: st.subheader('Game {game}: {game_name}'.format( game=game, game_name=all_games_dict[game]['name'] )) win_string = 'Win paths left if {team_name} wins: {win_paths} (Change: {win_paths_delta})'.format( team_name=key, win_paths=sweet_16_case_dict[key]['win_paths'], win_paths_delta=sweet_16_case_dict[key]['win_paths_delta'], ) st.write(win_string) if sweet_16_case_dict[key]['win_percent_delta'] > 0: color = 'green' elif sweet_16_case_dict[key]['win_percent_delta'] < 0: color = 'red' else: color = 'black' win_string = 'Win path percentages left if {team_name} wins: {win_percent:2f}% (Change: <font style="color:{color}">{win_percent_delta:2f}%</font>)'.format( team_name=key, win_percent=sweet_16_case_dict[key]['win_percent'], win_percent_delta=sweet_16_case_dict[key]['win_percent_delta'], color=color ) # st.write(win_string) print(win_string) st.markdown(win_string, unsafe_allow_html=True) if sweet_16_case_dict[key]['win_likelihood_delta'] > 0: color = 'green' elif sweet_16_case_dict[key]['win_likelihood_delta'] < 0: color = 'red' else: color = 'black' win_string = 'Win likelihood left if {team_name} wins: {win_likelihood:2f}% (Change: <font style="color:{color}">{win_likelihood_delta:2f}%</font>)'.format( team_name=key, win_likelihood=sweet_16_case_dict[key]['win_likelihood'], win_likelihood_delta=sweet_16_case_dict[key]['win_likelihood_delta'], color=color ) print(win_string) st.write(win_string, unsafe_allow_html=True) game += count_tracker if count_tracker == 0: count_tracker = 1 else: count_tracker = 0
) filename = st.text_input('Enter a filename:', 'dataset1.csv') #@st.cache def get_data(): return pd.read_csv(filename) # Initialize empty dataframe tweet_data = pd.DataFrame({'text': [], 'HS': []}) if st.button("Classify Data Set"): with st.spinner('Detecting Hate Speech...'): df = get_data() tweets = df['text'][0:1000] for tweet in tweets: if tweet in ('', ' '): continue vect_tweet = hs_tfidf.transform([tweet]).toarray() prediction = hsmod_clf.predict(vect_tweet) tweet_data = tweet_data.append({ 'text': tweet, 'HS': prediction[0] }, ignore_index=True) # Hate speech word cloud
def run_transformers(): """ run transformer """ # load global vars load_global_vars() # upload file uploaded_file = upload_file() if not isinstance(uploaded_file, st.uploaded_file_manager.UploadedFile): st.error("Select pdf file!") return # get topic and sent directroy output_topic_dir, output_sent_dir = input_pipeline_dir(topic=TOPIC_DIR, sent=SENTIMENT_DIR) if len(output_topic_dir) == 0 or len(output_sent_dir) == 0: st.error('Enter directory for the topic and sentiment model.') return # get topics topics, allow_multi_topics = define_topics(topics=TOPICS, multi_topic=MULTI_TOPICS) if len(topics) == 0: st.error('Enter at least one possible topic to see predictions.') return # preprocessing options st.sidebar.subheader("Preprocessing settings") # select number of paragraphs sentences n_text_selected = st.sidebar.slider( "Select the fraction of paragraphs/senteces to be processed", value=0.05, min_value=0.01, max_value=1.0) # choose how to split text_split = st.sidebar.selectbox( 'How to split text', ('Make paragraphs', 'Make sentences'), ) with st.spinner("Preprocessing data..."): articles = wrapper_preprocess_steps(uploaded_file=uploaded_file, text_split=text_split) with st.beta_expander("Show processed paragraphs"): st.write(articles) st.sidebar.subheader("Output calculations settings") weighted_by_words = st.sidebar.checkbox( "Weight predictions by number of words", value=True) # subsetting n_texts = int(len(articles) * n_text_selected) # make predictions with st.spinner("Predicting..."): predictions_all = wrapper_predictions( output_topic_dir=output_topic_dir, output_sent_dir=output_sent_dir, articles=articles, n_texts=n_texts, topics=topics, allow_multi_topics=allow_multi_topics) # format output format_output(predictions_all=predictions_all, weighted_by_words=weighted_by_words, n_texts=n_texts, articles=articles)
def hedge_dataframe(call, class_name, n): with st.spinner(f"Working on {class_name.__name__}"): portfolio = pd.DataFrame() # pull in data for all child data = pd.DataFrame() for baby in call.parent.children: tick = class_name(baby).calculate(call.parent.start_date) data = data.append(tick) # st.write(data) data = data.dropna(how='all') for date in data.columns: # For Each date in data.columns if data[date].isnull().all(): portfolio = portfolio.append({ "Date": date, f"Momentum": 0 }, ignore_index=True) continue data = data.sort_values(by=[date], ascending=False) upper_decile = data[date].head(n).copy() lower_decile = data[date].tail(n).copy() stock_in_decile = len(upper_decile) for i in upper_decile.index: loc = call.parent.fetch_child_by_name(i) try: val = call.parent.children[loc[0]].data["Adj Close"][date] except: logger.warning( f"Date Not Found : {date}. The data is {call.parent.children[loc[0]].name}" ) val = 0 upper_decile[i] *= (val / stock_in_decile) for i in lower_decile.index: loc = call.parent.fetch_child_by_name(i) try: val = call.parent.children[loc[0]].data["Adj Close"][date] except: logger.warning( f"Date Not Found : {date}. The data is {call.parent.children[loc[0]].name}" ) val = 0 lower_decile[i] *= (val / stock_in_decile) factor = sum(upper_decile) - sum(lower_decile) portfolio = portfolio.append({ "Date": date, f"Momentum": factor }, ignore_index=True) portfolio = portfolio.set_index("Date") # st.write(portfolio) maxValuesObj = portfolio.max(axis=1) maxValuesObj = maxValuesObj.rename("Momentum") return maxValuesObj
``` **Note**: `@streamlit.cache` requires that the function output depends *only* on its input arguments. For example, you can cache calls to API endpoints, but only do so if the data you get won't change. """ ) st.subheader("Spinners") st.write("A visual way of showing long computation is with a spinner:") def lengthy_computation(): pass # noop for demsontration purposes. with st.echo(): with st.spinner("Computing something time consuming..."): lengthy_computation() st.header("Animation") st.write( """ Every Streamlit method (except `st.write`) returns a handle which can be used for animation. Just call your favorite Streamlit function (e.g. `st.xyz()`) on the handle (e.g. `handle.xyz()`) and it will update that point in the app. Additionally, you can use `add_rows()` to append numpy arrays or DataFrames to existing elements. """ )
def write(): def update(parent, gov): gov.population[loc[0]] = parent gov.dump("data/User1.pkl") def create_checkbox(name, code, *args, **kwargs): agree = st.checkbox(name) if agree: return code(*args, **kwargs) """Used to write the page in the app.py file""" with st.spinner(""): logger.info("Hedging Page Opened") st.write(f"# Factor Construction") def checks(): global tracker, gov # The pkl file should be there for a particular user logger.info("Finding Government") gov, flag = Government.load_government("data/User1.pkl") if not flag: st.error("User Profile Not Found. Go to Input") logger.warning("User Profile not found.") st.stop() # The tracker should be set logger.info("Checking if tracker is set or not") tracker = gov.get_tracker() if not tracker: st.error("Tracker Not set. Go to Input") logger.warning("Tracker Not found.") st.stop() checks() loc = fetch_tracker(tracker, gov) parent = gov.population[loc[0]] def create_factor(): # Create Factors call = Caller(govt=gov, location=loc) call.divide_time() call.pull_market() parent.Factors["BAB"] = hedge(call, 50, Beta) update(parent, gov) call = Caller(govt=gov, location=loc) parent.Factors["Momentum"] = hedge_dataframe(call, Momentum, 50) update(parent, gov) create_checkbox("Create factors if not created", create_factor) if len(parent.Factors) != 0: result = pd.concat(parent.Factors.values(), axis=1) result.fillna(0, inplace=True) st.write(result) def final_regression(result, name="AAPL"): child_loc = parent.fetch_child_by_name(name) child = parent.children[child_loc[0]] if not child: st.error("Child Not found") y = child.data["Adj Close"] mask = (y.index >= min(result.index)) & (y.index <= max( result.index)) y = y[mask] # st.write(y) # handling y and x indexes intersection = list(set(y.index) & set(result.index)) result = result[(result.index.isin(intersection))] # st.write(result) model = sm.OLS(y, result) # fit model and print results regression = model.fit() for i in parent.Factors.keys(): st.write( f"{name} is exposed to {i} by {np.round(float(regression.params[i]*100),1)} %" ) create_checkbox("Apply Regression", final_regression, result=result)
if warning_msg: st.write(warning_msg) st.write( f"Neste momento, existem {round(number_of_servers_sel, 3)} vezes mais cidadãos a chegar do que a sair do sistema." ) st.write( "Significa que o sistema teria uma fila que cresce infinitamente, tornando-se maior quanto maior for o tempo de simulação, levando a um aumento dos tempos de espera." ) # Set simulation parameters sim = Simulation(**params) # Run simulation! if 0 not in can_submit: with st.spinner('Aguarde um pouco...'): Q = sim.simulate_waiting_time() st.markdown('## Resultados 🎲') # Extract simulation results server_busy_time = Q.transitive_nodes recs = Q.get_all_records() # filter warm-up time of t mins # if sim.number_of_servers > 1: # warm_up_time = 60 # else: # warm_up_time = 0 # warm_up_time = 60
def hedge(call, n, class_name): # n represents number of ticker in a decile portfolio = pd.DataFrame() with st.spinner( f"Buying/Selling Stocks based on {class_name.__name__}. Please Wait... It might take long" ): for chunk_num in range(len( call.time_chunks)): # For a specific time Frame parser = Parser(call) # create a parser object with st.spinner(f"Working on Time Frame {chunk_num}"): # Fetch data for specific time frame data = parser.parse_multiple(chunk_num, class_name) data = data.dropna(how='all') for date in data.columns: # For Each date in data.columns data = data.sort_values(by=[date], ascending=False) upper_decile = data[date].head(n).copy() lower_decile = data[date].tail(n).copy() stock_in_decile = len(upper_decile) for i in upper_decile.index: loc = call.parent.fetch_child_by_name(i) try: val = call.parent.children[ loc[0]].data["Adj Close"][date] except: logger.warning( f"Date Not Found : {date}. The data is {call.parent.children[loc[0]].name}" ) val = 0 upper_decile[i] *= (val / stock_in_decile) for i in lower_decile.index: loc = call.parent.fetch_child_by_name(i) try: val = call.parent.children[ loc[0]].data["Adj Close"][date] except: logger.warning( f"Date Not Found : {date}. The data is {call.parent.children[loc[0]].name}" ) val = 0 lower_decile[i] *= (val / stock_in_decile) factor = sum(upper_decile.fillna(0)) - sum( lower_decile.fillna(0)) st.write(upper_decile) st.write(lower_decile) st.write(factor) st.write("-----") # st.write(sum(upper_decile)) portfolio = portfolio.append( { "Date": date, f"Bab@t{chunk_num}": factor }, ignore_index=True) portfolio = portfolio.set_index("Date") # st.write(portfolio) portfolio.fillna(0, inplace=True) maxValuesObj = portfolio.max(axis=1) maxValuesObj = maxValuesObj.rename("BAB") st.write(maxValuesObj) # st.write("------") return maxValuesObj
) # Title with st.beta_container(): st.write(""" # Robotodio 👁 🔪 *Analizando la agresividad en Twitter* """) st.markdown("---") # Sidebar st.sidebar.title('Introduce una cuenta de Twitter') target = st.sidebar.text_input("sin @") # Reloj with st.spinner('Tened paciencia y tendréis ciencia...⏳'): # ------------------------------------------------------------------------------ # Twitter extractor and processor # ------------------------------------------------------------------------------ # ----------------------- Tweet downloader ------------------------------------- def tweets_iterator(target, n_items): ''' Returns an iterator of tweets. Parameters: target (str): The user name of the Twitter account. n_items (int): Number of tweets downloaded.
cols[0].markdown('#### Unique Values :') cols[1].markdown(f"# {dataframe[col].unique().size}") cols[2].markdown('#### Type :') cols[3].markdown(f"## {t}") sidebar = st.sidebar sidebar.header('Choose Your Option') options = [ 'View Dataset', 'Analyze By Country', 'Analyze by life Expectancy', 'Analyze By Gender', 'Analyze By Year', 'About' ] choice = sidebar.selectbox(options=options, label="Choose Action") if choice == options[0]: with st.spinner("Loading Data..."): st.header('Raw dataset') viewDataset('datasets/lifeExpectancyAtBirth.csv') elif choice == options[1]: with st.spinner("Loading Analysis..."): st.subheader('Top and bottom 20 life expectancy') st.image('images/analysisByCountry.png') st.subheader('Countries vs life expectancy') st.image('images/analysisByCountry1.png') st.image('images/analysisByRegion.png') st.image('images/analysisByRegion1.png') st.image('images/analysisByRegion2.png') st.image('images/analysisByRegion3.png') elif choice == options[2]: with st.spinner("Loading Analysis..."): st.image('images/analysisByExpectancy.png')
#token_words stem_sentence=[] spanishStemmer=SnowballStemmer("spanish",ignore_stopwords=True) for word in token_words: stem_sentence.append(spanishStemmer.stem(word)) stem_sentence.append(" ") return "".join(stem_sentence) if comentario != '': # Pre-process #sentence = vect.transform(stemfraseesp(comentario)) sentence=stemfraseesp(comentario) dato = [sentence] texto_vec=vect.transform(dato) # Make predictions with st.spinner('Predicting...'): clase=classifier.predict_proba(texto_vec)[:,0]>=0.3 if clase == True: st.image('negativo.png', width=300) #st.write(':anguished:') else: st.image('positivo.png', width=300) #st.write(':sunglasses:')
def app_run(): """ Returns ------- Run App """ #load Module class # Create a text element and let the reader know the data is loading. with st.spinner(text='In progress'): #data_load_state = st.text('Loading data & model...') # Notify the reader that the data was successfully loaded. __model = Model() st.success('Model Ready') #Dataset & zones df = __model.dataset_preprocessed #Parking Space Price parking_price_df =( df.loc[df['parkingSpacePrice']>1000]. groupby('district').mean()['parkingSpacePrice']) #Title & Header Image st.title('Valuta la tua casa a Varese-Monza') st.subheader("Scopri il valore di mercato della tua casa " "comodo e facile con un click") st.image('./data/Header Varese.jpg',use_column_width=True) st.subheader ("Usiamo un algoritmo di Machine Learning su %s immobili" % len(df)) #Parameters st.subheader("Imposta i parametri dell'immobile") #City city = st.selectbox('City',['Monza','Varese'],index = 0) selector_city = 'MO - ' if city == 'Monza': selector_city = 'MO - ' else: selector_city = 'VA - ' #Zone zonesList = ( df.loc[df['district'].str.startswith (selector_city)]['district'].unique().tolist() ) #Replace prefix zonesList = [i.replace(selector_city,'') for i in zonesList] district = st.selectbox('Zona', zonesList, index = 0) #Property Type List propertyTypelist = __model.propertyTypeList propertyType = st.selectbox('Tipo', propertyTypelist, index = 0) #Conversiont to model variables propertyType = __model.propertyTypeConverter(propertyType) #Rest of parameters size = st.number_input('Metri Quadri', min_value=10, max_value=5000, value = 100 ) rooms = st.slider('Locali', min_value = 1, max_value = 10, value = 3) #Conversiont to model variables #roomsCat = __model.roomsCategory(rooms) if rooms >= 4: roomsCat = 4 else: roomsCat = rooms #Bathrooms bathrooms = st.slider('Bagni', min_value = 1, max_value = 10, value = 2 ) #Conversiont to model variables if bathrooms >= 2: bathroomsCat = 2 else: bathroomsCat = bathrooms #Status italiano status_it = __model.statusList status = st.radio('Stato',status_it, index = 1) #Conversiont to model variables statusOutput = 'good' if status == "Da ristrutturare": statusOutput = 'renew' elif status == "Buono": statusOutput = 'good' elif status == "Nuova Costruzione": statusOutput = 'newdevelopment' #Extra Feautures #parkingBox = st.checkbox('Posto Auto - Box', value = 0) #garden = st.checkbox('Giardino- Terrazzo', value = 0) #swimming_pool = st.checkbox('Piscina', value = 0) #Parking Space District Selected try: parking_space = int( parking_price_df.loc[parking_price_df.index== (selector_city+district)].values[0]) except: parking_space = 0 #Button to value button = st.button('Valuta') if button: value_model = __model.predict( size, propertyType, (selector_city+district), statusOutput, roomsCat, bathroomsCat, ) value = int(math.ceil((value_model ) / 5000.0) * 5000.0) st.write("Valore di mercato") st.write("{:,.0f}€".format(value)) if parking_space > 0: st.write("Prezzo medio Posto auto di %s - %s " % (city,district)) st.write("{:,.0f}€".format(parking_space))
org_prices = r.json() symbol_price = {} for i in org_prices: symbol_price[i['symbol']] = float(i['price']) if DEBUG: st.write(f'Currently there is {len(symbol_price)} ticker pairs on Binance.') start_time = date_to_UTC_miliseconds(start_date) #print(start_time) # 1609459200000 ke 01/01/2021 data = [] with st.spinner('Wait for it...'): #data = [] #start_time1 = time.time() with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor: futures = [] #for s in trading_symbols[:5]: for s in trading_symbols: #print(s) futures.append(executor.submit(get_binance_data, symbol=s, start_time=start_time, symbol_price=symbol_price)) symboli_koji_su_stariji_od_start_date = [] for future in concurrent.futures.as_completed(futures): #print('f---', future.result())
## Displaying JSON st.header("Displaying JSON") st.text("Display JSON") st.json({'name': "Jesse", 'gender': "male"}) ## Displaying Progressbars,Spinners and Balloons st.header("Displaying Progressbars,Spinners and Balloons") # Progress Bar import time my_bar = st.progress(0) for p in range(10): my_bar.progress(p + 1) # Spinner with st.spinner("Waiting .."): time.sleep(2) st.success("Finished!") # Balloons st.balloons() ## Working with Data Science (DataFrame,Plot,Tables,etc) st.header("Working with Data Science (DataFrame,Plot,Tables,etc)") # Plot st.pyplot() # DataFrames st.dataframe(df) # Tables
def main(local=False): st.set_page_config( page_title="Simple NTU Course Viewer", page_icon="🧊", layout="wide", initial_sidebar_state="expanded", ) with st.spinner('讀取資料中⋯'): course_df = read_df(local) course_df = pre_processing(course_df.copy()) st.write(""" # 台大 110 年課表查詢""") col1, col2 = st.beta_columns((7, 4)) with col1: search_txt = st.text_input('輸入課程名稱/ID/老師名稱', '') need_help = st.beta_expander('需要幫忙嗎 👉') with need_help: st.markdown( """輸入**課程名稱**或是**課程 ID** 或是**老師名稱**。不能夠同時輸入課程名稱和老師名稱。""", unsafe_allow_html=True) with col2: valid_column = course_df.drop('raw_day', axis=1).columns view_options = st.multiselect('選擇檢視欄位', list(valid_column), list(valid_column)) days = ['一', '二', '三', '四', '五', '六', '七'] # days_select = [False for i in range(7)] if 'days_select' not in st.session_state: st.session_state['days_select'] = [False for i in range(7)] def update_day(d): st.session_state['days_select'][ d] = not st.session_state['days_select'][d] with st.form("date_picker"): st.write("選擇上課日") cols = st.beta_columns(7) for i, col in enumerate(cols): st.session_state['days_select'][i] = col.checkbox(days[i]) date_opt = st.radio("篩選條件", ('Subset', 'All Matched')) # Every form must have a submit button. submitted = st.form_submit_button("確認") if submitted: # st.write(st.session_state['days_select']) days_select = st.session_state['days_select'] pass other_info = st.beta_expander('其他資訊 🔗') with other_info: st.markdown("""一些常用連結: + [PTT NTUcourse 看板](https://www.ptt.cc/bbs/NTUcourse/index.html) + [Original Repo](https://github.com/hungchun0201/NTUclassCrawler) + [台大課程網](https://nol.ntu.edu.tw/nol/guest/index.php) <span style="font-size: 10px">* 註:僅為小型試用版,故僅用 Streamlit 簡單製作而已。若有不週全的地方,請自行修正 🙌🏾</span> """, unsafe_allow_html=True) df = course_df def in_list(x, date_opt): if date_opt == 'Subset': if set(x).issubset( set(np.array(days)[st.session_state['days_select']])): return True else: return False else: if set(x) == set(np.array(days)[st.session_state['days_select']]): return True else: return False st.write("## 課表結果") with st.spinner("結果產生中⋯"): if search_txt == "" and np.sum(st.session_state['days_select']) == 0: display_df = df[view_options] else: if np.sum(st.session_state['days_select']) == 0: display_df = df[( df['Title'].str.contains(search_txt) | df['Instructor'].str.contains(search_txt) | df['Id'].str.contains(search_txt))][view_options] else: display_df = df[(df['Title'].str.contains(search_txt) | df['Instructor'].str.contains(search_txt) | df['Id'].str.contains(search_txt)) & course_df['raw_day'].apply( in_list, args=(date_opt, ))][view_options] st.write("""<style> tr:hover {background-color:#50536b42; table { max-width: -moz-fit-content; max-width: fit-content; white-space: nowrap; } </style>""", unsafe_allow_html=True) st.write(f"""<div style="overflow:scroll; justify-content: center;"> {display_df.to_html()} </div>""", unsafe_allow_html=True) # <div class="styledTable" style="overflow:scroll"> # </div> st.balloons()
def main(): # page title st.title('Twitter Sentiment Analysis') activities = ['Analyze Tweets', 'About'] choice = st.sidebar.selectbox('Select Activity', activities) #Loading Models if choice == "Analyze Tweets": flag = st.sidebar.checkbox('Add Keyword') st.subheader('Input a tweet query') # user query user_input = st.text_input("Keyword", "Type Here.") if flag: user_input2 = st.text_input("Another Keyword", "Type Here.") count = st.sidebar.slider("Number of Tweets", min_value=10, max_value=1000, value=100, step=10) bar = st.progress(0) if st.button("Submit"): with st.spinner('Wait for it...'): start = time.time() text_query = user_input queryTweet(text_query) bar.progress(10) vect, model = load_models() bar.progress(30) tw1 = getTweets(user_input, count) tw1_pred = predict(vect, model, tw1["Tweets"].tolist()) tw1_pred["Date"] = tw1["Date"] st.subheader(user_input) st.dataframe(tw1_pred) bar.progress(60) if (flag): tw2 = getTweets(user_input2, count) tw2_pred = predict(vect, model, tw2["Tweets"].tolist()) tw2_pred["Date"] = tw2["Date"] st.subheader(user_input2) st.dataframe(tw2_pred) # tdf["Date"]=df["Date"] if (flag): # scatter plot st.subheader("Scatter Plot") fig = make_subplots(rows=1, cols=2) fig.add_trace(go.Scatter(x=tw1_pred["Date"], y=tw1_pred["Sentiment"], name=user_input), row=1, col=1) fig.add_trace(go.Scatter(x=tw2_pred["Date"], y=tw2_pred["Sentiment"], name=user_input2), row=1, col=2) st.plotly_chart(fig) # pie chart st.subheader(user_input) val = tw1_pred["Sentiment"].value_counts().values fig = go.Figure() fig.add_trace( go.Pie(labels=['Positive', 'Negative'], values=val, name=user_input)) st.plotly_chart(fig) st.subheader(user_input2) val2 = tw2_pred["Sentiment"].value_counts().values fig = go.Figure() fig.add_trace( go.Pie(labels=['Positive', 'Negative'], values=val2, name=user_input2)) st.plotly_chart(fig) # bar chart st.subheader("Bar Chart") fig = go.Figure() fig.add_trace( go.Bar(x=['Negative', 'Positive'], y=val, name=user_input)) fig.add_trace( go.Bar(x=['Negative', 'Positive'], y=val2, name=user_input2)) fig.update_layout(title="{} v {}".format( user_input, user_input2), title_x=0.5, xaxis_title='Sentiment', yaxis_title='Number of Tweets') st.plotly_chart(fig) else: # plot st.subheader("Scatter Plot") fig = go.Figure() fig.add_trace( go.Scatter(x=tw1_pred["Date"], y=tw1_pred["Sentiment"], name=user_input)) st.plotly_chart(fig) # pie chart st.subheader("Pie Chart") val = tw1_pred["Sentiment"].value_counts().values fig = go.Figure() fig.add_trace( go.Pie(labels=['Negative', 'Positive'], values=val, name='First Tweet')) st.plotly_chart(fig) # bar chart st.subheader("Bar Chart") fig = go.Figure() fig.add_trace( go.Bar(x=['Negative', 'Positive'], y=val, name=user_input)) # fig.add_trace( # go.Bar(x=['Negative', 'Positive'], y=val2, name=user_input2)) fig.update_layout(title=user_input, title_x=0.5, xaxis_title='Sentiment', yaxis_title='Number of Tweets') st.plotly_chart(fig) bar.progress(100) end = time.time() print("Total Time: ", end - start) elif choice == "About": st.subheader("by Meshal") st.info( "Twitter Sentiment Classifier trained on Sentiment 140 Dataset. Tweets preprocessed and TF-IDF computed with ngram=(1,3) and 10k words . Best performing model was Support Vector Classifier with 80% Accuracy. GetOldTweets is used for twitter scraping." ) st.markdown("Built by Meshal")
def basic_annotator( get_dataset: Callable[..., Dict[str, Any]], display: Callable[[Any, str, str, str], anno.Page], selectors: Callable[[], dict] = None, ): username = st.sidebar.text_input(label="Enter Username", key="username") if username != "": if selectors is not None: values: dict = selectors() else: values = {} dataset: Dict[str, Any] = get_dataset(**values) # Why do we do this ? So if the dataset changes, we reload the assignments fron scratch. # Time saving Hackery. dataset_hash = hash(frozenset(dataset.keys())) state = SessionState.get( key=secrets.token_hex(128), username=username, assignments={}, get_next=True, dataset_hash=dataset_hash, ) should_load_assignments = ( state.get_next # Assignment Done or state.dataset_hash != dataset_hash # Dataset Changes or state.username != username # Username Changes ) if should_load_assignments: assignments: Dict[str, bool] = get_history(dataset_ids=list( dataset.keys()), username=username) state.assignments = assignments state.dataset_hash = dataset_hash state.username = username state.get_next = False else: assignments = state.assignments format_func = lambda x: (u"✅ " if assignments[x] else "❌ ") + x selected_assignment = st.sidebar.selectbox( label="Assignments", options=list(assignments.keys()), format_func=format_func, ) with st.spinner("Loading document"): state = SessionState.get(key=secrets.token_hex(128)) annotations = annotation_block( page=display( dataset[selected_assignment], username=username, assignment=selected_assignment, key=state.key, ), key=state.key, ) print("=" * 50) print(annotations) print("=" * 50) if annotations is not None: save(annotations["values"]) if annotations["next"] == True: if assignments[selected_assignment] == False: mark_as_completed(assignment_id=selected_assignment, username=username) state.get_next = True state.key = secrets.token_hex(128) st.experimental_rerun()
else: zoom = 11 st.pydeck_chart( pdk.Deck( map_style='mapbox://styles/mapbox/light-v9', initial_view_state=pdk.ViewState( latitude=cities_data[len(cities_data) - 1]['lat'], longitude=cities_data[len(cities_data) - 1]['lon'], zoom=zoom, pitch=50), layers=[layers], )) with st.spinner('Carregando gráfico...'): # plot data distribution if button_analyze == 'ESTADOS': figure = make_graph_map(data_to_show, option_selected, day) st.plotly_chart(figure) else: if city_to_compare_selected: st.info( f'{option_selected} NA CIDADE DE {city_choised.upper()}: {covid_19_city_data}' ) st.info( f'{option_selected} NA CIDADE DE {city_to_compare.upper()}: {covid_19_city_to_compare_data}' ) else: st.info( f'{option_selected} NA CIDADE DE {city_choised.upper()}: {covid_19_city_data}'