Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,21 @@ from streamlit_option_menu import option_menu
|
|
| 8 |
from gemini_utility import (load_gemini_pro, gemini_pro_vision_responce)
|
| 9 |
from PIL import Image
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Setting the page config
|
| 12 |
st.set_page_config(
|
| 13 |
page_title="GnosticDev AI",
|
|
@@ -16,35 +31,11 @@ st.set_page_config(
|
|
| 16 |
initial_sidebar_state="expanded",
|
| 17 |
)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# Funci贸n para cargar configuraciones desde un archivo JSON
|
| 22 |
-
def load_config():
|
| 23 |
-
if os.path.exists(CONFIG_FILE):
|
| 24 |
-
try:
|
| 25 |
-
with open(CONFIG_FILE, "r") as file:
|
| 26 |
-
return json.load(file)
|
| 27 |
-
except Exception as e:
|
| 28 |
-
st.error(f"Error cargando configuraciones: {e}")
|
| 29 |
-
return {}
|
| 30 |
-
|
| 31 |
-
# Funci贸n para guardar configuraciones en un archivo JSON
|
| 32 |
-
def save_config(config):
|
| 33 |
-
try:
|
| 34 |
-
with open(CONFIG_FILE, "w") as file:
|
| 35 |
-
json.dump(config, file, indent=4)
|
| 36 |
-
except Exception as e:
|
| 37 |
-
st.error(f"Error guardando configuraciones: {e}")
|
| 38 |
-
|
| 39 |
-
# Cargar configuraciones al inicio
|
| 40 |
-
config = load_config()
|
| 41 |
-
if "system_prompt" not in config:
|
| 42 |
-
config["system_prompt"] = ""
|
| 43 |
-
|
| 44 |
-
# Inicializar estados
|
| 45 |
if "system_prompt" not in st.session_state:
|
| 46 |
-
st.session_state.system_prompt =
|
| 47 |
|
|
|
|
| 48 |
def save_chat_history(history):
|
| 49 |
serializable_history = []
|
| 50 |
for message in history:
|
|
@@ -54,6 +45,7 @@ def save_chat_history(history):
|
|
| 54 |
})
|
| 55 |
st.session_state.cookie_chat_history = json.dumps(serializable_history)
|
| 56 |
|
|
|
|
| 57 |
def load_chat_history():
|
| 58 |
if 'cookie_chat_history' in st.session_state:
|
| 59 |
try:
|
|
@@ -99,7 +91,7 @@ def extract_urls(text):
|
|
| 99 |
|
| 100 |
def fetch_url_content(url):
|
| 101 |
try:
|
| 102 |
-
response = requests.get(url, timeout=10)
|
| 103 |
response.raise_for_status()
|
| 104 |
return response.text
|
| 105 |
except requests.exceptions.RequestException as e:
|
|
@@ -108,6 +100,7 @@ def fetch_url_content(url):
|
|
| 108 |
def process_url_content(content):
|
| 109 |
try:
|
| 110 |
soup = BeautifulSoup(content, "html.parser")
|
|
|
|
| 111 |
text = soup.get_text(" ", strip=True)
|
| 112 |
return text
|
| 113 |
except Exception as e:
|
|
@@ -118,7 +111,7 @@ def process_urls_in_prompt(prompt):
|
|
| 118 |
new_prompt = prompt
|
| 119 |
for url in urls:
|
| 120 |
content = fetch_url_content(url)
|
| 121 |
-
if content.startswith("Error"):
|
| 122 |
new_prompt = new_prompt.replace(url, content)
|
| 123 |
else:
|
| 124 |
processed_content = process_url_content(content)
|
|
@@ -137,13 +130,14 @@ if selected == "System Prompt":
|
|
| 137 |
if st.button("Guardar System Prompt"):
|
| 138 |
processed_prompt = process_urls_in_prompt(new_system_prompt)
|
| 139 |
st.session_state.system_prompt = processed_prompt
|
| 140 |
-
|
| 141 |
-
save_config(config)
|
| 142 |
if "chat_session" in st.session_state:
|
| 143 |
del st.session_state.chat_session
|
| 144 |
st.success("System Prompt actualizado con 茅xito!")
|
| 145 |
|
| 146 |
-
|
|
|
|
|
|
|
| 147 |
|
| 148 |
elif selected == "Chatbot":
|
| 149 |
model = load_gemini_pro()
|
|
@@ -152,18 +146,11 @@ elif selected == "Chatbot":
|
|
| 152 |
if loaded_chat:
|
| 153 |
st.session_state.chat_session = loaded_chat
|
| 154 |
else:
|
| 155 |
-
# Crea una nueva sesi贸n de chat sin enviar el system_prompt como mensaje inicial
|
| 156 |
st.session_state.chat_session = model.start_chat(history=[])
|
|
|
|
| 157 |
|
| 158 |
st.title("Gnosticdev Chatbot")
|
| 159 |
-
|
| 160 |
-
# Muestra solo el historial del chat, si existe
|
| 161 |
-
for message in st.session_state.chat_session.history:
|
| 162 |
-
with st.chat_message(translate_role_to_streamlit(message.role)):
|
| 163 |
-
st.markdown(message.parts[0].text)
|
| 164 |
-
|
| 165 |
-
# Entrada del usuario
|
| 166 |
-
user_prompt = st.chat_input("Preg煤ntame algo...")
|
| 167 |
if user_prompt:
|
| 168 |
processed_user_prompt = process_urls_in_prompt(user_prompt)
|
| 169 |
st.chat_message("user").markdown(processed_user_prompt)
|
|
|
|
| 8 |
from gemini_utility import (load_gemini_pro, gemini_pro_vision_responce)
|
| 9 |
from PIL import Image
|
| 10 |
|
| 11 |
+
# Ruta para guardar el System Prompt
|
| 12 |
+
SYSTEM_PROMPT_FILE = "system_prompt.json"
|
| 13 |
+
|
| 14 |
+
# Funci贸n para cargar el System Prompt desde el archivo
|
| 15 |
+
def load_system_prompt():
|
| 16 |
+
if os.path.exists(SYSTEM_PROMPT_FILE):
|
| 17 |
+
with open(SYSTEM_PROMPT_FILE, 'r') as f:
|
| 18 |
+
return json.load(f).get("system_prompt", "")
|
| 19 |
+
return ""
|
| 20 |
+
|
| 21 |
+
# Funci贸n para guardar el System Prompt en el archivo
|
| 22 |
+
def save_system_prompt(system_prompt):
|
| 23 |
+
with open(SYSTEM_PROMPT_FILE, 'w') as f:
|
| 24 |
+
json.dump({"system_prompt": system_prompt}, f)
|
| 25 |
+
|
| 26 |
# Setting the page config
|
| 27 |
st.set_page_config(
|
| 28 |
page_title="GnosticDev AI",
|
|
|
|
| 31 |
initial_sidebar_state="expanded",
|
| 32 |
)
|
| 33 |
|
| 34 |
+
# Cargar el System Prompt guardado
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if "system_prompt" not in st.session_state:
|
| 36 |
+
st.session_state.system_prompt = load_system_prompt()
|
| 37 |
|
| 38 |
+
# Funci贸n para guardar el historial en cookies
|
| 39 |
def save_chat_history(history):
|
| 40 |
serializable_history = []
|
| 41 |
for message in history:
|
|
|
|
| 45 |
})
|
| 46 |
st.session_state.cookie_chat_history = json.dumps(serializable_history)
|
| 47 |
|
| 48 |
+
# Funci贸n para cargar el historial desde cookies
|
| 49 |
def load_chat_history():
|
| 50 |
if 'cookie_chat_history' in st.session_state:
|
| 51 |
try:
|
|
|
|
| 91 |
|
| 92 |
def fetch_url_content(url):
|
| 93 |
try:
|
| 94 |
+
response = requests.get(url, timeout=10) # Agregar timeout para evitar bloqueos
|
| 95 |
response.raise_for_status()
|
| 96 |
return response.text
|
| 97 |
except requests.exceptions.RequestException as e:
|
|
|
|
| 100 |
def process_url_content(content):
|
| 101 |
try:
|
| 102 |
soup = BeautifulSoup(content, "html.parser")
|
| 103 |
+
# Extrae solo el texto del cuerpo principal, ignorando etiquetas de scripts y estilos
|
| 104 |
text = soup.get_text(" ", strip=True)
|
| 105 |
return text
|
| 106 |
except Exception as e:
|
|
|
|
| 111 |
new_prompt = prompt
|
| 112 |
for url in urls:
|
| 113 |
content = fetch_url_content(url)
|
| 114 |
+
if content.startswith("Error"): # Gestion de errores al obtener el contenido de la URL
|
| 115 |
new_prompt = new_prompt.replace(url, content)
|
| 116 |
else:
|
| 117 |
processed_content = process_url_content(content)
|
|
|
|
| 130 |
if st.button("Guardar System Prompt"):
|
| 131 |
processed_prompt = process_urls_in_prompt(new_system_prompt)
|
| 132 |
st.session_state.system_prompt = processed_prompt
|
| 133 |
+
save_system_prompt(processed_prompt) # Guardar en archivo JSON
|
|
|
|
| 134 |
if "chat_session" in st.session_state:
|
| 135 |
del st.session_state.chat_session
|
| 136 |
st.success("System Prompt actualizado con 茅xito!")
|
| 137 |
|
| 138 |
+
if st.session_state.system_prompt:
|
| 139 |
+
st.markdown("### System Prompt Actual:")
|
| 140 |
+
st.info(st.session_state.system_prompt)
|
| 141 |
|
| 142 |
elif selected == "Chatbot":
|
| 143 |
model = load_gemini_pro()
|
|
|
|
| 146 |
if loaded_chat:
|
| 147 |
st.session_state.chat_session = loaded_chat
|
| 148 |
else:
|
|
|
|
| 149 |
st.session_state.chat_session = model.start_chat(history=[])
|
| 150 |
+
# No enviar autom谩ticamente el system_prompt aqu铆
|
| 151 |
|
| 152 |
st.title("Gnosticdev Chatbot")
|
| 153 |
+
user_prompt = st.chat_input("Preguntame algo...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
if user_prompt:
|
| 155 |
processed_user_prompt = process_urls_in_prompt(user_prompt)
|
| 156 |
st.chat_message("user").markdown(processed_user_prompt)
|