Oliver Li commited on
Commit
e7c1530
·
1 Parent(s): 89af5f1

use session state

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -1,8 +1,9 @@
1
  import streamlit as st
2
  import pandas as pd
3
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
4
-
5
  # Function to load the pre-trained model
 
6
  def load_finetune_model(model_name):
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
@@ -48,7 +49,7 @@ selected_model = st.selectbox("Choose a fine-tuned model:", model_options)
48
  st.write("### Model Information")
49
  st.write(f"**Description:** {model_options[selected_model]['description']}")
50
 
51
- table_df = pd.DataFrame(columns=["Text (portion)", "Toxicity class 1", "Class 1 probability", "Toxicity class 2", "Class 2 probability"])
52
  initial_table_data = [{'Text (portion)': ["who's speaking? \n you goddamn cocksucker you know "],
53
  'Toxicity class 1': ['obscene'],
54
  'Class 1 probability': 0.7282997369766235,
@@ -100,8 +101,9 @@ initial_table_data = [{'Text (portion)': ["who's speaking? \n you goddamn cocksu
100
  'Toxicity class 2': ['obscene'],
101
  'Class 2 probability': 0.16506287455558777}]
102
  for d in initial_table_data:
103
- table_df = pd.concat([table_df, pd.DataFrame(d)], ignore_index=True)
104
  # Load the model and perform toxicity analysis
 
105
  if st.button("Analyze"):
106
  if not text:
107
  st.write("Please enter a text.")
@@ -123,14 +125,18 @@ if st.button("Analyze"):
123
  "Toxicity class 2": [results[1][0]],
124
  f"Class 2 probability": results[1][1]
125
  }
126
- print("Before concatenation:")
127
- print(table_df)
128
  # Concatenate the new data frame with the existing data frame
129
- table_df = pd.concat([pd.DataFrame(table_data), table_df], ignore_index=True)
130
- print("After concatenation:")
131
- print(table_df)
 
 
 
 
132
  # Update the table with the new data frame
133
- st.table(table_df)
134
  else:
135
  st.empty()
136
  sentiment_pipeline = load_model(selected_model)
 
1
  import streamlit as st
2
  import pandas as pd
3
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
4
+ import SessionState
5
  # Function to load the pre-trained model
6
+ ss = SessionState.get(table_df=pd.DataFrame(), first_run = True)
7
  def load_finetune_model(model_name):
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
 
49
  st.write("### Model Information")
50
  st.write(f"**Description:** {model_options[selected_model]['description']}")
51
 
52
+ initial_table_df = pd.DataFrame(columns=["Text (portion)", "Toxicity class 1", "Class 1 probability", "Toxicity class 2", "Class 2 probability"])
53
  initial_table_data = [{'Text (portion)': ["who's speaking? \n you goddamn cocksucker you know "],
54
  'Toxicity class 1': ['obscene'],
55
  'Class 1 probability': 0.7282997369766235,
 
101
  'Toxicity class 2': ['obscene'],
102
  'Class 2 probability': 0.16506287455558777}]
103
  for d in initial_table_data:
104
+ initial_table_df = pd.concat([initial_table_df, pd.DataFrame(d)], ignore_index=True)
105
  # Load the model and perform toxicity analysis
106
+
107
  if st.button("Analyze"):
108
  if not text:
109
  st.write("Please enter a text.")
 
125
  "Toxicity class 2": [results[1][0]],
126
  f"Class 2 probability": results[1][1]
127
  }
128
+ # print("Before concatenation:")
129
+ # print(table_df)
130
  # Concatenate the new data frame with the existing data frame
131
+ if ss.first_run:
132
+ ss.table_df = pd.concat([pd.DataFrame(table_data), ss.table_df, initial_table_data], ignore_index=True)
133
+ ss.first_run = False
134
+ else:
135
+ ss.table_df = pd.concat([pd.DataFrame(table_data), ss.table_df], ignore_index=True)
136
+ # print("After concatenation:")
137
+ # print(table_df)
138
  # Update the table with the new data frame
139
+ st.table(ss.table_df)
140
  else:
141
  st.empty()
142
  sentiment_pipeline = load_model(selected_model)