Spaces:
Sleeping
Sleeping
| import os | |
| import requests | |
| import streamlit as st | |
| from googleapiclient.discovery import build | |
| from google.auth.credentials import AnonymousCredentials | |
| from google.auth.transport.requests import Request | |
| from dotenv import load_dotenv | |
| from google.oauth2.credentials import Credentials | |
| if st.session_state.Cloud != 0: | |
| load_dotenv() | |
| # Charger les secrets depuis les variables d'environnement | |
| CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID") | |
| CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET") | |
| REFRESH_TOKEN = os.getenv("GOOGLE_REFRESH_TOKEN") | |
| GOOGLE_PREPROMPT_FILE_ID = os.getenv("GOOGLE_PREPROMPT_FILE_ID") | |
| # URL pour rafraîchir le token | |
| TOKEN_URL = "https://oauth2.googleapis.com/token" | |
| # Fonction pour obtenir un token d'accès | |
| def get_access_token(): | |
| data = { | |
| "client_id": CLIENT_ID, | |
| "client_secret": CLIENT_SECRET, | |
| "refresh_token": REFRESH_TOKEN, | |
| "grant_type": "refresh_token", | |
| } | |
| response = requests.post(TOKEN_URL, data=data) | |
| response_data = response.json() | |
| if "access_token" in response_data: | |
| return response_data["access_token"] | |
| else: | |
| raise Exception(f"Erreur d'obtention du token d'accès : {response_data}") | |
| # Obtenir le token d'accès | |
| access_token = get_access_token() | |
| # Fonction pour lire le contenu d'un Google Doc | |
| def read_google_doc(doc_id): | |
| try: | |
| # Créer les credentials à partir du token d'accès | |
| creds = Credentials( | |
| token=access_token, | |
| refresh_token=REFRESH_TOKEN, | |
| token_uri="https://oauth2.googleapis.com/token", | |
| client_id=CLIENT_ID, | |
| client_secret=CLIENT_SECRET | |
| ) | |
| # Construire le service Google Docs avec les credentials | |
| docs_service = build('docs', 'v1', credentials=creds) | |
| # Requête pour obtenir le contenu du document | |
| document = docs_service.documents().get(documentId=doc_id).execute() | |
| content = document.get('body', {}).get('content', []) | |
| # Initialiser une liste pour stocker chaque ligne | |
| lines = [] | |
| # Extraire chaque ligne de texte | |
| for element in content: | |
| if 'paragraph' in element: | |
| paragraph_text = "" | |
| for paragraph in element['paragraph']['elements']: | |
| t = paragraph.get('textRun', {}).get('content', '') | |
| paragraph_text += t | |
| if paragraph_text.strip(): | |
| lines.append(paragraph_text.strip()) | |
| return lines | |
| except Exception as e: | |
| raise Exception(f"Erreur lors de la lecture du document : {e}") | |
| def read_param(): | |
| # Lire et afficher le contenu d'un fichier | |
| try: | |
| lines = read_google_doc(GOOGLE_PREPROMPT_FILE_ID) | |
| return lines | |
| except Exception as e: | |
| st.write(f"Erreur : {e}") | |
| def format_param(): | |
| try: | |
| lines = read_param() | |
| if not lines: | |
| raise Exception("Le contenu du document est vide.") | |
| label = [] | |
| question = [] | |
| options = [[] for _ in range(8)] | |
| i = 0 | |
| for p in range(8): | |
| while i < len(lines) and len(lines[i]) >= 3 and lines[i][:3] == "===": | |
| i += 1 | |
| label.append(lines[i][8:]) | |
| i += 1 | |
| if p not in [0, 1, 2]: | |
| question.append(lines[i]) | |
| i += 1 | |
| else: | |
| question.append("") | |
| while i < len(lines) and len(lines[i]) >= 3 and lines[i][:3] != "===": | |
| options[p].append(lines[i]) | |
| i += 1 | |
| i += 1 | |
| while i < len(lines): | |
| question.append(lines[i]) | |
| i += 1 | |
| except Exception as e: | |
| st.write(f"Erreur : {e}") | |
| return label, question, options |