Spaces:
Sleeping
Sleeping
Commit
·
70d35b5
1
Parent(s):
d91ab5d
Update google_drive_read_preprompt.py
Browse files
tabs/google_drive_read_preprompt.py
CHANGED
|
@@ -39,14 +39,20 @@ access_token = get_access_token()
|
|
| 39 |
|
| 40 |
# Fonction pour lire le contenu d'un Google Doc
|
| 41 |
def read_google_doc(doc_id):
|
| 42 |
-
# Créer les credentials à partir du token d'accès
|
| 43 |
-
creds = Credentials(token=access_token)
|
| 44 |
-
|
| 45 |
-
# Construire le service Google Docs avec les credentials
|
| 46 |
-
docs_service = build('docs', 'v1', credentials=creds)
|
| 47 |
-
|
| 48 |
-
# Requête pour obtenir le contenu du document
|
| 49 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
document = docs_service.documents().get(documentId=doc_id).execute()
|
| 51 |
content = document.get('body', {}).get('content', [])
|
| 52 |
|
|
@@ -56,16 +62,15 @@ def read_google_doc(doc_id):
|
|
| 56 |
# Extraire chaque ligne de texte
|
| 57 |
for element in content:
|
| 58 |
if 'paragraph' in element:
|
| 59 |
-
paragraph_text = ""
|
| 60 |
for paragraph in element['paragraph']['elements']:
|
| 61 |
t = paragraph.get('textRun', {}).get('content', '')
|
| 62 |
-
paragraph_text += t
|
| 63 |
|
| 64 |
-
# Ajouter le paragraphe complet s'il contient du texte
|
| 65 |
if paragraph_text.strip():
|
| 66 |
lines.append(paragraph_text.strip())
|
| 67 |
|
| 68 |
-
return lines
|
| 69 |
except Exception as e:
|
| 70 |
raise Exception(f"Erreur lors de la lecture du document : {e}")
|
| 71 |
|
|
@@ -84,30 +89,33 @@ def read_param():
|
|
| 84 |
def format_param():
|
| 85 |
try:
|
| 86 |
lines = read_param()
|
|
|
|
|
|
|
|
|
|
| 87 |
label = []
|
| 88 |
question = []
|
| 89 |
options = [[] for _ in range(8)]
|
| 90 |
i = 0
|
| 91 |
for p in range(8):
|
| 92 |
-
while (lines[i][:3] == "==="
|
| 93 |
-
i +=1
|
| 94 |
label.append(lines[i][8:])
|
| 95 |
-
i +=1
|
| 96 |
-
if p not in [0,1,2]:
|
| 97 |
question.append(lines[i])
|
| 98 |
-
i +=1
|
| 99 |
-
else:
|
| 100 |
-
|
|
|
|
| 101 |
options[p].append(lines[i])
|
| 102 |
-
i +=1
|
| 103 |
-
i+=1
|
| 104 |
-
while i<len(lines):
|
| 105 |
question.append(lines[i])
|
| 106 |
-
i +=1
|
| 107 |
-
print("label:\n",label)
|
| 108 |
-
print("question:\n",question)
|
| 109 |
-
print("options:\n",options)
|
| 110 |
except Exception as e:
|
| 111 |
st.write(f"Erreur : {e}")
|
| 112 |
-
return label, question, options
|
| 113 |
-
|
|
|
|
| 39 |
|
| 40 |
# Fonction pour lire le contenu d'un Google Doc
|
| 41 |
def read_google_doc(doc_id):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
try:
|
| 43 |
+
# Créer les credentials à partir du token d'accès
|
| 44 |
+
creds = Credentials(
|
| 45 |
+
token=access_token,
|
| 46 |
+
refresh_token=REFRESH_TOKEN,
|
| 47 |
+
token_uri="https://oauth2.googleapis.com/token",
|
| 48 |
+
client_id=CLIENT_ID,
|
| 49 |
+
client_secret=CLIENT_SECRET
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Construire le service Google Docs avec les credentials
|
| 53 |
+
docs_service = build('docs', 'v1', credentials=creds)
|
| 54 |
+
|
| 55 |
+
# Requête pour obtenir le contenu du document
|
| 56 |
document = docs_service.documents().get(documentId=doc_id).execute()
|
| 57 |
content = document.get('body', {}).get('content', [])
|
| 58 |
|
|
|
|
| 62 |
# Extraire chaque ligne de texte
|
| 63 |
for element in content:
|
| 64 |
if 'paragraph' in element:
|
| 65 |
+
paragraph_text = ""
|
| 66 |
for paragraph in element['paragraph']['elements']:
|
| 67 |
t = paragraph.get('textRun', {}).get('content', '')
|
| 68 |
+
paragraph_text += t
|
| 69 |
|
|
|
|
| 70 |
if paragraph_text.strip():
|
| 71 |
lines.append(paragraph_text.strip())
|
| 72 |
|
| 73 |
+
return lines
|
| 74 |
except Exception as e:
|
| 75 |
raise Exception(f"Erreur lors de la lecture du document : {e}")
|
| 76 |
|
|
|
|
| 89 |
def format_param():
|
| 90 |
try:
|
| 91 |
lines = read_param()
|
| 92 |
+
if not lines:
|
| 93 |
+
raise Exception("Le contenu du document est vide.")
|
| 94 |
+
|
| 95 |
label = []
|
| 96 |
question = []
|
| 97 |
options = [[] for _ in range(8)]
|
| 98 |
i = 0
|
| 99 |
for p in range(8):
|
| 100 |
+
while i < len(lines) and len(lines[i]) >= 3 and lines[i][:3] == "===":
|
| 101 |
+
i += 1
|
| 102 |
label.append(lines[i][8:])
|
| 103 |
+
i += 1
|
| 104 |
+
if p not in [0, 1, 2]:
|
| 105 |
question.append(lines[i])
|
| 106 |
+
i += 1
|
| 107 |
+
else:
|
| 108 |
+
question.append("")
|
| 109 |
+
while i < len(lines) and len(lines[i]) >= 3 and lines[i][:3] != "===":
|
| 110 |
options[p].append(lines[i])
|
| 111 |
+
i += 1
|
| 112 |
+
i += 1
|
| 113 |
+
while i < len(lines):
|
| 114 |
question.append(lines[i])
|
| 115 |
+
i += 1
|
| 116 |
+
print("label:\n", label)
|
| 117 |
+
print("question:\n", question)
|
| 118 |
+
print("options:\n", options)
|
| 119 |
except Exception as e:
|
| 120 |
st.write(f"Erreur : {e}")
|
| 121 |
+
return label, question, options
|
|
|