Spaces:
Sleeping
Sleeping
Jose Martin Rangel Espinoza commited on
Commit ·
1cd8943
1
Parent(s): 07d3e6c
🔧 Refactor authentication to use global variables for Drive service and folders
Browse files
app.py
CHANGED
|
@@ -20,6 +20,8 @@ SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
|
|
| 20 |
CHROMA_DIR = "chroma_db"
|
| 21 |
|
| 22 |
chain = None
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Autenticación manual para Hugging Face
|
| 25 |
|
|
@@ -27,7 +29,6 @@ def authenticate_manual(credentials_path, code):
|
|
| 27 |
with open(credentials_path, "r") as f:
|
| 28 |
client_config = json.load(f)
|
| 29 |
|
| 30 |
-
# Forzar uso de redirect_uri manual
|
| 31 |
client_config["installed"]["redirect_uris"] = ["urn:ietf:wg:oauth:2.0:oob"]
|
| 32 |
flow = InstalledAppFlow.from_client_config(client_config, SCOPES, redirect_uri="urn:ietf:wg:oauth:2.0:oob")
|
| 33 |
auth_url, _ = flow.authorization_url(prompt='consent')
|
|
@@ -37,9 +38,9 @@ def authenticate_manual(credentials_path, code):
|
|
| 37 |
creds = flow.credentials
|
| 38 |
with open("token.json", 'w') as token:
|
| 39 |
token.write(creds.to_json())
|
| 40 |
-
return build('drive', 'v3', credentials=creds), "✅
|
| 41 |
|
| 42 |
-
return None, auth_url
|
| 43 |
|
| 44 |
# Obtener carpetas de Drive
|
| 45 |
|
|
@@ -148,25 +149,24 @@ with gr.Blocks(title="Google Drive RAG") as demo:
|
|
| 148 |
user_input = gr.Textbox(label="Escribe tu pregunta")
|
| 149 |
enviar_btn = gr.Button("Enviar pregunta")
|
| 150 |
|
| 151 |
-
carpetas_drive = gr.State({})
|
| 152 |
-
servicio_drive = gr.State(None)
|
| 153 |
-
|
| 154 |
def conectar_drive(cred_file, auth_code):
|
|
|
|
| 155 |
try:
|
| 156 |
-
service,
|
| 157 |
if not service:
|
| 158 |
-
return [],
|
|
|
|
| 159 |
carpetas = listar_carpetas_drive(service)
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
return list(carpetas.keys()),
|
| 163 |
except Exception as e:
|
| 164 |
-
return [], f"❌ Error: {e}", ""
|
| 165 |
|
| 166 |
def procesar_pdfs(nombre_carpeta, cred_file, tok_file, api_key):
|
| 167 |
try:
|
| 168 |
-
service =
|
| 169 |
-
folder_id =
|
| 170 |
if not folder_id:
|
| 171 |
return "❌ Carpeta no encontrada."
|
| 172 |
folder_path = get_pdfs_from_drive_by_id(service, folder_id)
|
|
|
|
| 20 |
CHROMA_DIR = "chroma_db"
|
| 21 |
|
| 22 |
chain = None
|
| 23 |
+
carpetas_drive_global = {}
|
| 24 |
+
servicio_drive_global = None
|
| 25 |
|
| 26 |
# Autenticación manual para Hugging Face
|
| 27 |
|
|
|
|
| 29 |
with open(credentials_path, "r") as f:
|
| 30 |
client_config = json.load(f)
|
| 31 |
|
|
|
|
| 32 |
client_config["installed"]["redirect_uris"] = ["urn:ietf:wg:oauth:2.0:oob"]
|
| 33 |
flow = InstalledAppFlow.from_client_config(client_config, SCOPES, redirect_uri="urn:ietf:wg:oauth:2.0:oob")
|
| 34 |
auth_url, _ = flow.authorization_url(prompt='consent')
|
|
|
|
| 38 |
creds = flow.credentials
|
| 39 |
with open("token.json", 'w') as token:
|
| 40 |
token.write(creds.to_json())
|
| 41 |
+
return build('drive', 'v3', credentials=creds), "✅ Conectado correctamente.", ""
|
| 42 |
|
| 43 |
+
return None, "Copia y abre este enlace para autorizar:", f"[{auth_url}]({auth_url})"
|
| 44 |
|
| 45 |
# Obtener carpetas de Drive
|
| 46 |
|
|
|
|
| 149 |
user_input = gr.Textbox(label="Escribe tu pregunta")
|
| 150 |
enviar_btn = gr.Button("Enviar pregunta")
|
| 151 |
|
|
|
|
|
|
|
|
|
|
| 152 |
def conectar_drive(cred_file, auth_code):
|
| 153 |
+
global carpetas_drive_global, servicio_drive_global
|
| 154 |
try:
|
| 155 |
+
service, estado_text, link = authenticate_manual(cred_file.name, auth_code.strip())
|
| 156 |
if not service:
|
| 157 |
+
return gr.update(choices=[]), estado_text, link
|
| 158 |
+
|
| 159 |
carpetas = listar_carpetas_drive(service)
|
| 160 |
+
carpetas_drive_global = carpetas
|
| 161 |
+
servicio_drive_global = service
|
| 162 |
+
return gr.update(choices=list(carpetas.keys())), estado_text, ""
|
| 163 |
except Exception as e:
|
| 164 |
+
return gr.update(choices=[]), f"❌ Error: {e}", ""
|
| 165 |
|
| 166 |
def procesar_pdfs(nombre_carpeta, cred_file, tok_file, api_key):
|
| 167 |
try:
|
| 168 |
+
service = servicio_drive_global
|
| 169 |
+
folder_id = carpetas_drive_global.get(nombre_carpeta)
|
| 170 |
if not folder_id:
|
| 171 |
return "❌ Carpeta no encontrada."
|
| 172 |
folder_path = get_pdfs_from_drive_by_id(service, folder_id)
|