lorenzozan commited on
Commit
a882d8b
·
verified ·
1 Parent(s): fbe5237

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -7
app.py CHANGED
@@ -1,7 +1,8 @@
1
  import streamlit as st
2
  import torch
3
  from transformers import AutoModel, AutoTokenizer
4
- import pandas as pd
 
5
 
6
  st.set_page_config(page_title="ME2-BERT Moralities", layout="centered")
7
 
@@ -116,17 +117,17 @@ if st.session_state["prediction_result"]:
116
  st.write("**Predicted moral foundations:**")
117
  st.json(st.session_state["prediction_result"])
118
 
119
-
120
-
121
  st.markdown("## CSV File Upload")
122
- st.write("You can upload a space-separated CSV file with a 'text' column. The file must have at most 10 rows.")
123
 
124
  uploaded_file = st.file_uploader("Upload CSV (space-separated)", type=["csv"])
125
  if uploaded_file is not None:
126
  df = pd.read_csv(uploaded_file, sep=' ')
127
-
128
- if len(df) > 10:
129
- st.error("The CSV file must have at most 10 rows.")
130
  else:
131
  # Controllo esistenza colonna 'text'
132
  if 'text' not in df.columns:
@@ -134,10 +135,37 @@ if uploaded_file is not None:
134
  else:
135
  text_list = df['text'].tolist()
136
  predictions = predict_moralities(text_list)
 
 
137
  st.write("### Predictions for each row:")
138
  for i, row_text in enumerate(text_list):
139
  st.write(f"Row {i+1}: {row_text}")
140
  st.json(predictions[i])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  # ---------------------------------------------------------------
142
 
143
  st.markdown(
 
1
  import streamlit as st
2
  import torch
3
  from transformers import AutoModel, AutoTokenizer
4
+ import pandas as pd
5
+ import io
6
 
7
  st.set_page_config(page_title="ME2-BERT Moralities", layout="centered")
8
 
 
117
  st.write("**Predicted moral foundations:**")
118
  st.json(st.session_state["prediction_result"])
119
 
120
+ # SEZIONE AGGIUNTA PER IL FILE CSV (MODIFICHE RICHIESTE)
121
+ # ---------------------------------------------------------------
122
  st.markdown("## CSV File Upload")
123
+ st.write("You can upload a space-separated CSV file with a 'text' column. The file must have at most 50 rows.")
124
 
125
  uploaded_file = st.file_uploader("Upload CSV (space-separated)", type=["csv"])
126
  if uploaded_file is not None:
127
  df = pd.read_csv(uploaded_file, sep=' ')
128
+ # Controllo numero di righe
129
+ if len(df) > 50:
130
+ st.error("The CSV file must have at most 50 rows.")
131
  else:
132
  # Controllo esistenza colonna 'text'
133
  if 'text' not in df.columns:
 
135
  else:
136
  text_list = df['text'].tolist()
137
  predictions = predict_moralities(text_list)
138
+
139
+ # Stampa row-by-row come da codice precedente
140
  st.write("### Predictions for each row:")
141
  for i, row_text in enumerate(text_list):
142
  st.write(f"Row {i+1}: {row_text}")
143
  st.json(predictions[i])
144
+
145
+ # Creiamo un DataFrame con text e predizioni per permettere download e copia
146
+ results_for_download = []
147
+ for txt, preds in zip(text_list, predictions):
148
+ row_data = {"text": txt}
149
+ row_data.update(preds) # Aggiunge CARE/HARM, FAIRNESS/CHEATING, ecc.
150
+ results_for_download.append(row_data)
151
+
152
+ df_results = pd.DataFrame(results_for_download)
153
+
154
+ # Convertiamo in CSV in memoria
155
+ csv_buffer = io.StringIO()
156
+ df_results.to_csv(csv_buffer, index=False)
157
+ csv_data = csv_buffer.getvalue()
158
+
159
+ # Bottone per il download
160
+ st.download_button(
161
+ label="Download predictions as CSV",
162
+ data=csv_data,
163
+ file_name="predictions.csv",
164
+ mime="text/csv"
165
+ )
166
+
167
+ # Text area per poter copiare il CSV
168
+ st.text_area("Copy the results as CSV text", value=csv_data, height=200)
169
  # ---------------------------------------------------------------
170
 
171
  st.markdown(