Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import PyPDF2
|
|
| 5 |
import requests
|
| 6 |
import io
|
| 7 |
import json
|
| 8 |
-
import os #
|
| 9 |
|
| 10 |
# Variable global para almacenar el texto extraído de la base de conocimientos
|
| 11 |
# Se vacía cada vez que se carga un nuevo archivo.
|
|
@@ -91,6 +91,13 @@ def get_llm_response(prompt_text, context_text, personality_setting):
|
|
| 91 |
# Combinar la instrucción del sistema, el contexto y la pregunta del usuario en un solo prompt
|
| 92 |
full_prompt = f"{system_instruction}\n\nContexto:\n{context_text}\n\nPregunta: {prompt_text}"
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
# Preparar el historial de chat para la API de Gemini
|
| 95 |
chat_history = []
|
| 96 |
chat_history.append({"role": "user", "parts": [{"text": full_prompt}]})
|
|
@@ -103,11 +110,6 @@ def get_llm_response(prompt_text, context_text, personality_setting):
|
|
| 103 |
}
|
| 104 |
}
|
| 105 |
|
| 106 |
-
# La clave de la API se deja vacía según las instrucciones para el entorno Canvas.
|
| 107 |
-
# En un despliegue real, esto debería manejarse de forma segura (e.g., variables de entorno).
|
| 108 |
-
api_key = "" # Canvas inyectará la clave API en tiempo de ejecución
|
| 109 |
-
api_url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={api_key}"
|
| 110 |
-
|
| 111 |
try:
|
| 112 |
# Realizar la solicitud POST a la API de Gemini
|
| 113 |
response = requests.post(api_url, headers={'Content-Type': 'application/json'}, data=json.dumps(payload))
|
|
|
|
| 5 |
import requests
|
| 6 |
import io
|
| 7 |
import json
|
| 8 |
+
import os # Importar el módulo os para acceder a variables de entorno
|
| 9 |
|
| 10 |
# Variable global para almacenar el texto extraído de la base de conocimientos
|
| 11 |
# Se vacía cada vez que se carga un nuevo archivo.
|
|
|
|
| 91 |
# Combinar la instrucción del sistema, el contexto y la pregunta del usuario en un solo prompt
|
| 92 |
full_prompt = f"{system_instruction}\n\nContexto:\n{context_text}\n\nPregunta: {prompt_text}"
|
| 93 |
|
| 94 |
+
# *** CAMBIO IMPORTANTE AQUÍ: Leer la clave API de las variables de entorno ***
|
| 95 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 96 |
+
if not api_key:
|
| 97 |
+
return "Error: La clave API de Gemini no está configurada. Por favor, añádela como un secreto en Hugging Face Spaces (GEMINI_API_KEY)."
|
| 98 |
+
|
| 99 |
+
api_url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={api_key}"
|
| 100 |
+
|
| 101 |
# Preparar el historial de chat para la API de Gemini
|
| 102 |
chat_history = []
|
| 103 |
chat_history.append({"role": "user", "parts": [{"text": full_prompt}]})
|
|
|
|
| 110 |
}
|
| 111 |
}
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
try:
|
| 114 |
# Realizar la solicitud POST a la API de Gemini
|
| 115 |
response = requests.post(api_url, headers={'Content-Type': 'application/json'}, data=json.dumps(payload))
|