Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import google.generativeai as genai
|
|
4 |
from gtts import gTTS
|
5 |
import os
|
6 |
import io
|
|
|
7 |
|
8 |
# Configuración de la página de Streamlit
|
9 |
st.set_page_config(page_title="🤖 Soph-IA", layout="wide")
|
@@ -11,10 +12,44 @@ st.set_page_config(page_title="🤖 Soph-IA", layout="wide")
|
|
11 |
# Obtener la API key desde los secretos de Hugging Face
|
12 |
api_key = st.secrets["API_KEY"] # Accede al secreto
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
# Respuestas automatizadas basadas en palabras clave
|
15 |
def respuestas_automatizadas(texto):
|
16 |
texto = texto.lower()
|
17 |
-
# Respuestas para preguntas específicas
|
18 |
respuestas = {
|
19 |
"quien te creo": "Fui creada por un Bioingeniero llamado Jeysshon Bustos Jimenez, mi creador.",
|
20 |
"quien es tu creador": "Fui creada por un Bioingeniero llamado Jeysshon Bustos Jimenez, mi creador.",
|
@@ -58,18 +93,18 @@ def procesar_imagen(imagen):
|
|
58 |
|
59 |
def hablar_texto(texto):
|
60 |
try:
|
61 |
-
tts = gTTS(text=texto, lang='es')
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
64 |
except Exception as e:
|
65 |
st.error("Hubo un error al generar el audio. Por favor, recarga la página y vuelve a intentar.")
|
66 |
-
|
67 |
-
# Cargar y aplicar CSS personalizado
|
68 |
-
with open("./style.css") as f:
|
69 |
-
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
70 |
|
71 |
# Título centrado
|
72 |
-
st.markdown("<h1
|
73 |
|
74 |
# Explicación técnica de Soph-IA
|
75 |
st.markdown("""
|
@@ -86,6 +121,7 @@ espacio_contenido_generado = st.empty()
|
|
86 |
|
87 |
st.write("¡Hola! Soy Soph-IA, tu asistente virtual. ¿Cómo puedo ayudarte hoy? 😊")
|
88 |
|
|
|
89 |
col1, col2 = st.columns([1, 3])
|
90 |
|
91 |
with col1:
|
@@ -97,9 +133,15 @@ with col2:
|
|
97 |
if entrada_texto:
|
98 |
with st.spinner("Generando respuesta..."):
|
99 |
resultado = procesar_texto(entrada_texto)
|
100 |
-
espacio_contenido_generado.
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
elif tipo_entrada == "🖼️ Subir imagen":
|
105 |
entrada_imagen = st.file_uploader("Sube una imagen", type=["jpg", "png", "jpeg"])
|
@@ -108,10 +150,9 @@ with col2:
|
|
108 |
st.image(imagen, caption='Imagen subida.', use_column_width=True)
|
109 |
with st.spinner("Procesando imagen..."):
|
110 |
respuesta = procesar_imagen(imagen)
|
111 |
-
espacio_contenido_generado.
|
112 |
|
113 |
st.write("¡Gracias por usar Soph-IA! 😊")
|
114 |
|
115 |
# Pie de página
|
116 |
-
st.markdown("<p
|
117 |
-
|
|
|
4 |
from gtts import gTTS
|
5 |
import os
|
6 |
import io
|
7 |
+
import base64
|
8 |
|
9 |
# Configuración de la página de Streamlit
|
10 |
st.set_page_config(page_title="🤖 Soph-IA", layout="wide")
|
|
|
12 |
# Obtener la API key desde los secretos de Hugging Face
|
13 |
api_key = st.secrets["API_KEY"] # Accede al secreto
|
14 |
|
15 |
+
# Estilo CSS para diseño atractivo
|
16 |
+
st.markdown("""
|
17 |
+
<style>
|
18 |
+
body {
|
19 |
+
background-color: #1a1a1a;
|
20 |
+
color: #ffffff;
|
21 |
+
font-family: 'Arial', sans-serif;
|
22 |
+
}
|
23 |
+
h1 {
|
24 |
+
color: #ffcc00;
|
25 |
+
text-align: center;
|
26 |
+
}
|
27 |
+
.response {
|
28 |
+
background-color: #333333;
|
29 |
+
border-radius: 5px;
|
30 |
+
padding: 10px;
|
31 |
+
margin-top: 10px;
|
32 |
+
}
|
33 |
+
.footer {
|
34 |
+
text-align: center;
|
35 |
+
color: #ffcc00;
|
36 |
+
margin-top: 20px;
|
37 |
+
}
|
38 |
+
.audio-button {
|
39 |
+
background-color: #ffcc00;
|
40 |
+
color: #1a1a1a;
|
41 |
+
border: none;
|
42 |
+
border-radius: 5px;
|
43 |
+
padding: 5px 10px;
|
44 |
+
cursor: pointer;
|
45 |
+
margin-top: 5px;
|
46 |
+
}
|
47 |
+
</style>
|
48 |
+
""", unsafe_allow_html=True)
|
49 |
+
|
50 |
# Respuestas automatizadas basadas en palabras clave
|
51 |
def respuestas_automatizadas(texto):
|
52 |
texto = texto.lower()
|
|
|
53 |
respuestas = {
|
54 |
"quien te creo": "Fui creada por un Bioingeniero llamado Jeysshon Bustos Jimenez, mi creador.",
|
55 |
"quien es tu creador": "Fui creada por un Bioingeniero llamado Jeysshon Bustos Jimenez, mi creador.",
|
|
|
93 |
|
94 |
def hablar_texto(texto):
|
95 |
try:
|
96 |
+
tts = gTTS(text=texto, lang='es', slow=False)
|
97 |
+
with io.BytesIO() as audio_file:
|
98 |
+
tts.save(audio_file)
|
99 |
+
audio_file.seek(0)
|
100 |
+
audio_base64 = base64.b64encode(audio_file.read()).decode()
|
101 |
+
return f"data:audio/mp3;base64,{audio_base64}"
|
102 |
except Exception as e:
|
103 |
st.error("Hubo un error al generar el audio. Por favor, recarga la página y vuelve a intentar.")
|
104 |
+
return None
|
|
|
|
|
|
|
105 |
|
106 |
# Título centrado
|
107 |
+
st.markdown("<h1>🤖 ¡Bienvenido a Soph-IA!</h1>", unsafe_allow_html=True)
|
108 |
|
109 |
# Explicación técnica de Soph-IA
|
110 |
st.markdown("""
|
|
|
121 |
|
122 |
st.write("¡Hola! Soy Soph-IA, tu asistente virtual. ¿Cómo puedo ayudarte hoy? 😊")
|
123 |
|
124 |
+
# Columnas para selección de entrada
|
125 |
col1, col2 = st.columns([1, 3])
|
126 |
|
127 |
with col1:
|
|
|
133 |
if entrada_texto:
|
134 |
with st.spinner("Generando respuesta..."):
|
135 |
resultado = procesar_texto(entrada_texto)
|
136 |
+
espacio_contenido_generado.markdown(f"<div class='response'>**Respuesta:** {resultado}</div>", unsafe_allow_html=True)
|
137 |
+
audio_data = hablar_texto(resultado)
|
138 |
+
if audio_data:
|
139 |
+
st.markdown(f"""
|
140 |
+
<audio controls>
|
141 |
+
<source src="{audio_data}" type="audio/mpeg">
|
142 |
+
Tu navegador no soporta el elemento de audio.
|
143 |
+
</audio>
|
144 |
+
""", unsafe_allow_html=True)
|
145 |
|
146 |
elif tipo_entrada == "🖼️ Subir imagen":
|
147 |
entrada_imagen = st.file_uploader("Sube una imagen", type=["jpg", "png", "jpeg"])
|
|
|
150 |
st.image(imagen, caption='Imagen subida.', use_column_width=True)
|
151 |
with st.spinner("Procesando imagen..."):
|
152 |
respuesta = procesar_imagen(imagen)
|
153 |
+
espacio_contenido_generado.markdown(f"<div class='response'>**Respuesta de la imagen:** {respuesta}</div>", unsafe_allow_html=True)
|
154 |
|
155 |
st.write("¡Gracias por usar Soph-IA! 😊")
|
156 |
|
157 |
# Pie de página
|
158 |
+
st.markdown("<p class='footer'>© 2024 Jeysshon</p>", unsafe_allow_html=True)
|
|