andyqin18 commited on
Commit
aa2cadb
1 Parent(s): a7fbbb7
Files changed (1) hide show
  1. app.py +27 -27
app.py CHANGED
@@ -3,7 +3,7 @@ import pandas as pd
3
  import numpy as np
4
  from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
5
 
6
-
7
  fine_tuned_model = "andyqin18/test-finetuned"
8
  sample_text_num = 10
9
 
@@ -34,27 +34,24 @@ model_descrip = {
34
  Labels: POS; NEU; NEG"
35
  }
36
 
37
-
38
-
39
-
40
  user_input = st.text_input("Enter your text:", value="NYU is the better than Columbia.")
41
  user_model = st.selectbox("Please select a model:", model_descrip)
42
 
 
43
  # Display model information
44
  st.write("### Model Description:")
45
  st.write(model_descrip[user_model])
46
 
47
 
48
-
49
-
50
  # Perform analysis and print result
51
  if st.button("Analyze"):
52
  if not user_input:
53
  st.write("Please enter a text.")
54
  else:
55
  with st.spinner("Hang on.... Analyzing..."):
 
56
  if user_model == fine_tuned_model:
57
- result = analyze(user_model, user_input, top_k=2)
58
  result_dict = {
59
  "Text": [user_input],
60
  "Highest Toxicity Class": [result[0][0]['label']],
@@ -63,29 +60,32 @@ if st.button("Analyze"):
63
  "Second Highest Score": [result[0][1]['score']]
64
  }
65
  st.dataframe(pd.DataFrame(result_dict))
66
- if st.button("Click to generate ten sample analysis"):
67
- df = pd.read_csv("milestone3/comp/test_comment.csv")
68
- test_texts = df["comment_text"].values
69
- sample_texts = np.random.choice(test_texts, size=sample_text_num, replace=False)
70
 
71
- init_table_dict = {
72
- "Text": [],
73
- "Highest Toxicity Class": [],
74
- "Highest Score": [],
75
- "Second Highest Toxicity Class": [],
76
- "Second Highest Score": []
77
- }
78
-
79
- for text in sample_texts:
80
- result = analyze(fine_tuned_model, text[:50], top_k=2)
81
- init_table_dict["Text"].append(text[:50])
82
- init_table_dict["Highest Toxicity Class"].append(result[0][0]['label'])
83
- init_table_dict["Highest Score"].append(result[0][0]['score'])
84
- init_table_dict["Second Highest Toxicity Class"].append(result[0][1]['label'])
85
- init_table_dict["Second Highest Score"].append(result[0][1]['score'])
 
 
 
 
 
 
 
86
  st.dataframe(pd.DataFrame(init_table_dict))
87
  else:
88
- st.write("(─‿‿─)")
89
 
90
 
91
  else:
 
3
  import numpy as np
4
  from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
5
 
6
+ # Define global variables
7
  fine_tuned_model = "andyqin18/test-finetuned"
8
  sample_text_num = 10
9
 
 
34
  Labels: POS; NEU; NEG"
35
  }
36
 
 
 
 
37
  user_input = st.text_input("Enter your text:", value="NYU is the better than Columbia.")
38
  user_model = st.selectbox("Please select a model:", model_descrip)
39
 
40
+
41
  # Display model information
42
  st.write("### Model Description:")
43
  st.write(model_descrip[user_model])
44
 
45
 
 
 
46
  # Perform analysis and print result
47
  if st.button("Analyze"):
48
  if not user_input:
49
  st.write("Please enter a text.")
50
  else:
51
  with st.spinner("Hang on.... Analyzing..."):
52
+ # If fine-tuned
53
  if user_model == fine_tuned_model:
54
+ result = analyze(user_model, user_input, top_k=2) # Top 2 labels with highest score
55
  result_dict = {
56
  "Text": [user_input],
57
  "Highest Toxicity Class": [result[0][0]['label']],
 
60
  "Second Highest Score": [result[0][1]['score']]
61
  }
62
  st.dataframe(pd.DataFrame(result_dict))
 
 
 
 
63
 
64
+ # 10 Sample Table
65
+ if st.button("Click to generate ten sample analysis"):
66
+ with st.spinner("Hang on.... Analyzing..."):
67
+ df = pd.read_csv("milestone3/comp/test_comment.csv")
68
+ test_texts = df["comment_text"].values
69
+ sample_texts = np.random.choice(test_texts, size=sample_text_num, replace=False)
70
+
71
+ init_table_dict = {
72
+ "Text": [],
73
+ "Highest Toxicity Class": [],
74
+ "Highest Score": [],
75
+ "Second Highest Toxicity Class": [],
76
+ "Second Highest Score": []
77
+ }
78
+
79
+ for text in sample_texts:
80
+ result = analyze(fine_tuned_model, text[:50], top_k=2)
81
+ init_table_dict["Text"].append(text[:50])
82
+ init_table_dict["Highest Toxicity Class"].append(result[0][0]['label'])
83
+ init_table_dict["Highest Score"].append(result[0][0]['score'])
84
+ init_table_dict["Second Highest Toxicity Class"].append(result[0][1]['label'])
85
+ init_table_dict["Second Highest Score"].append(result[0][1]['score'])
86
  st.dataframe(pd.DataFrame(init_table_dict))
87
  else:
88
+ st.write("( ─ ‿ ‿ ─ )")
89
 
90
 
91
  else: