|
import io |
|
import os |
|
import json |
|
import streamlit as st |
|
import pandas as pd |
|
from google.oauth2 import service_account |
|
from googleapiclient.discovery import build |
|
from googleapiclient.http import MediaIoBaseDownload, MediaIoBaseUpload |
|
|
|
|
|
FOLDER_ID = os.getenv("FOLDER_ID") |
|
GOOGLE_CREDENTIALS = os.environ.get("GOOGLE_CREDENTIALS") |
|
DATASET_NAME = os.environ.get("DATASET_NAME") |
|
|
|
def create_new_file_in_drive(username, dataframe_to_upload, credentials_json, folder_id): |
|
"""Crée un nouveau fichier CSV dans Google Drive à partir d'un DataFrame Pandas.""" |
|
|
|
creds_dict = json.loads(credentials_json) |
|
|
|
|
|
creds = service_account.Credentials.from_service_account_info(creds_dict) |
|
|
|
|
|
service = build('drive', 'v3', credentials=creds) |
|
|
|
|
|
csv_buffer = io.BytesIO() |
|
dataframe_to_upload.to_csv(csv_buffer, index=False, sep=';', encoding='utf-8') |
|
csv_buffer.seek(0) |
|
|
|
|
|
filename = f"rating-results-{username}.csv" |
|
file_metadata = { |
|
'name': filename, |
|
'parents': [folder_id] |
|
} |
|
|
|
|
|
media = MediaIoBaseUpload(csv_buffer, mimetype='text/csv', resumable=True) |
|
file = service.files().create(body=file_metadata, media_body=media, fields='id').execute() |
|
|
|
print(f"File '{filename}' created successfully.") |
|
|
|
def main(): |
|
st.title("Problematic Rating App") |
|
|
|
|
|
if 'user' not in st.session_state: |
|
st.session_state.user = None |
|
if 'page' not in st.session_state: |
|
st.session_state.page = 0 |
|
|
|
|
|
if st.session_state.page == 0: |
|
st.session_state.user = st.text_input("Enter your name:") |
|
if st.session_state.user: |
|
st.session_state.page = 1 |
|
st.rerun() |
|
else: |
|
st.stop() |
|
|
|
|
|
try: |
|
df = pd.read_csv(DATASET_NAME, sep=';') |
|
except FileNotFoundError: |
|
st.error(f"{DATASET_NAME} not found. Please upload the file.") |
|
return |
|
|
|
|
|
if 'problem_index' not in st.session_state: |
|
st.session_state.problem_index = 0 |
|
if 'formulation_index' not in st.session_state: |
|
st.session_state.formulation_index = 0 |
|
if 'results' not in st.session_state: |
|
st.session_state.results = [] |
|
if 'specificity' not in st.session_state: |
|
st.session_state.specificity = None |
|
if 'clarity' not in st.session_state: |
|
st.session_state.clarity = None |
|
if 'priority' not in st.session_state: |
|
st.session_state.priority = None |
|
|
|
|
|
|
|
if st.session_state.problem_index < len(df): |
|
problem = df.iloc[st.session_state.problem_index] |
|
formulation_types = ['original_form', 'perspective_shift', 'paraphrase', 'structural_transformation'] |
|
current_formulation = formulation_types[st.session_state.formulation_index] |
|
st.write(f"Technical problem {st.session_state.problem_index + 1}/{len(df)}") |
|
st.write(f"Formulation {st.session_state.formulation_index + 1}/4") |
|
st.write(problem[current_formulation]) |
|
|
|
|
|
st.write("Rate the problem on the following three criteria:") |
|
col1, col2, col3 = st.columns(3) |
|
|
|
with col1: |
|
st.session_state.specificity = st.radio("Specificity", options=[0, 1, 2, 3, 4, 5, "Don't know"], index=None) |
|
|
|
with col2: |
|
st.session_state.clarity = st.radio("Clarity", options=[0, 1, 2, 3, 4, 5, "Don't know"], index=None) |
|
|
|
with col3: |
|
st.session_state.priority = st.radio("Priority Order", options=[0, 1, 2, 3, 4, 5, "Don't know"], index=None) |
|
|
|
|
|
if st.button("Next"): |
|
|
|
if st.session_state.user: |
|
st.session_state.results.append({ |
|
'user': st.session_state.user, |
|
'formulation': current_formulation, |
|
'problematic': problem[current_formulation], |
|
'specificity': st.session_state.specificity, |
|
'clarity': st.session_state.clarity, |
|
'priority': st.session_state.priority |
|
}) |
|
st.session_state.formulation_index += 1 |
|
if st.session_state.formulation_index == 4: |
|
st.session_state.formulation_index = 0 |
|
st.session_state.problem_index += 1 |
|
st.rerun() |
|
else: |
|
st.warning("Please enter your name before proceeding.") |
|
else: |
|
|
|
st.write("Thanks for rating the problems!") |
|
st.session_state.results_df = pd.DataFrame(st.session_state.results) |
|
create_new_file_in_drive(st.session_state.user, st.session_state.results_df, GOOGLE_CREDENTIALS, FOLDER_ID) |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |