Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import time
|
|
| 7 |
import base64
|
| 8 |
import tempfile
|
| 9 |
from google.cloud import texttospeech
|
| 10 |
-
from google.cloud import
|
| 11 |
from streamlit_webrtc import webrtc_streamer, WebRtcMode, AudioProcessorBase
|
| 12 |
|
| 13 |
# Configuraci贸n de la clave API
|
|
@@ -111,10 +111,10 @@ class AudioProcessor(AudioProcessorBase):
|
|
| 111 |
|
| 112 |
# Funci贸n para transcribir audio a texto usando Google Cloud Speech-to-Text
|
| 113 |
def transcribir_audio(audio_bytes):
|
| 114 |
-
client =
|
| 115 |
-
audio =
|
| 116 |
-
config =
|
| 117 |
-
encoding=
|
| 118 |
sample_rate_hertz=16000,
|
| 119 |
language_code="es-ES",
|
| 120 |
)
|
|
@@ -228,4 +228,118 @@ if page == "Chat Asistente":
|
|
| 228 |
|
| 229 |
# Convertir texto a voz
|
| 230 |
audio_base64 = text_to_speech_base64(respuesta)
|
| 231 |
-
audio_html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import base64
|
| 8 |
import tempfile
|
| 9 |
from google.cloud import texttospeech
|
| 10 |
+
from google.cloud.speech import SpeechClient, RecognitionAudio, RecognitionConfig
|
| 11 |
from streamlit_webrtc import webrtc_streamer, WebRtcMode, AudioProcessorBase
|
| 12 |
|
| 13 |
# Configuraci贸n de la clave API
|
|
|
|
| 111 |
|
| 112 |
# Funci贸n para transcribir audio a texto usando Google Cloud Speech-to-Text
|
| 113 |
def transcribir_audio(audio_bytes):
|
| 114 |
+
client = SpeechClient()
|
| 115 |
+
audio = RecognitionAudio(content=audio_bytes)
|
| 116 |
+
config = RecognitionConfig(
|
| 117 |
+
encoding=RecognitionConfig.AudioEncoding.LINEAR16,
|
| 118 |
sample_rate_hertz=16000,
|
| 119 |
language_code="es-ES",
|
| 120 |
)
|
|
|
|
| 228 |
|
| 229 |
# Convertir texto a voz
|
| 230 |
audio_base64 = text_to_speech_base64(respuesta)
|
| 231 |
+
audio_html = f"""
|
| 232 |
+
<audio autoplay>
|
| 233 |
+
<source src="data:audio/mp3;base64,{audio_base64}" type="audio/mp3">
|
| 234 |
+
</audio>
|
| 235 |
+
"""
|
| 236 |
+
st.markdown(audio_html, unsafe_allow_html=True)
|
| 237 |
+
else:
|
| 238 |
+
st.warning("Por favor, ingresa una pregunta antes de enviar.")
|
| 239 |
+
|
| 240 |
+
elif page == "Gesti贸n de Pedidos":
|
| 241 |
+
st.subheader("馃搵 Gesti贸n de Pedidos")
|
| 242 |
+
menu_csv_path = "menu.csv" # Ruta al archivo CSV del men煤
|
| 243 |
+
if 'pedidos' not in st.session_state:
|
| 244 |
+
st.session_state.pedidos = []
|
| 245 |
+
|
| 246 |
+
pedido_agent = PedidoAgent(menu_csv_path)
|
| 247 |
+
calculo_pedido_agent = CalculoPedidoAgent()
|
| 248 |
+
|
| 249 |
+
pedido_agent.realizar_pedido(st.session_state)
|
| 250 |
+
calculo_pedido_agent.calcular_total(st.session_state)
|
| 251 |
+
|
| 252 |
+
elif page == "Generador de Frases B铆blicas":
|
| 253 |
+
import streamlit as st
|
| 254 |
+
from dotenv import load_dotenv
|
| 255 |
+
import os
|
| 256 |
+
from vertexai import generative_models
|
| 257 |
+
from vertexai.generative_models import GenerativeModel
|
| 258 |
+
|
| 259 |
+
# Configuraci贸n de la clave API y la autenticaci贸n de Google Cloud
|
| 260 |
+
load_dotenv()
|
| 261 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path/to/your/google-cloud-credentials.json"
|
| 262 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 263 |
+
IMAGE_GENERATION_API_KEY = os.getenv("IMAGE_GENERATION_API_KEY")
|
| 264 |
+
|
| 265 |
+
# Inicializaci贸n del modelo Gemini usando Vertex AI
|
| 266 |
+
model = GenerativeModel(model_name="gemini-1.0-pro-vision")
|
| 267 |
+
|
| 268 |
+
# Funci贸n para obtener una frase relacionada usando el modelo Gemini Advanced
|
| 269 |
+
def obtener_frase_relacionada(versiculo):
|
| 270 |
+
try:
|
| 271 |
+
response = model.generate_content([versiculo])
|
| 272 |
+
return response[0]
|
| 273 |
+
except Exception as e:
|
| 274 |
+
st.error(f"Error al obtener la frase relacionada: {e}")
|
| 275 |
+
return None
|
| 276 |
+
|
| 277 |
+
# Funci贸n para generar una imagen alusiva usando un servicio de generaci贸n de im谩genes
|
| 278 |
+
def generar_imagen(frase):
|
| 279 |
+
headers = {
|
| 280 |
+
"Authorization": f"Bearer {IMAGE_GENERATION_API_KEY}",
|
| 281 |
+
"Content-Type": "application/json"
|
| 282 |
+
}
|
| 283 |
+
data = {
|
| 284 |
+
"prompt": frase,
|
| 285 |
+
"model": "image-generation-model"
|
| 286 |
+
}
|
| 287 |
+
response = requests.post("https://api.image-generation.com/v1/generate", headers=headers, json=data)
|
| 288 |
+
if response.status_code == 200:
|
| 289 |
+
return response.json()["image_url"]
|
| 290 |
+
else:
|
| 291 |
+
st.error("Error al generar la imagen")
|
| 292 |
+
return None
|
| 293 |
+
|
| 294 |
+
# Configuraci贸n de Streamlit
|
| 295 |
+
st.set_page_config(page_title="Generador de Frases B铆blicas", page_icon="馃摐")
|
| 296 |
+
|
| 297 |
+
# Estilos CSS personalizados
|
| 298 |
+
st.markdown(
|
| 299 |
+
"""
|
| 300 |
+
<style>
|
| 301 |
+
body {
|
| 302 |
+
background: linear-gradient(to right, #f2f3f5, #ffcccb);
|
| 303 |
+
color: #333;
|
| 304 |
+
}
|
| 305 |
+
.stButton>button {
|
| 306 |
+
background-color: #4CAF50;
|
| 307 |
+
color: white;
|
| 308 |
+
border-radius: 10px;
|
| 309 |
+
}
|
| 310 |
+
.stTextInput>div>div>input {
|
| 311 |
+
border: 1px solid #4CAF50;
|
| 312 |
+
border-radius: 10px;
|
| 313 |
+
}
|
| 314 |
+
.stMarkdown>div>p {
|
| 315 |
+
color: #4CAF50;
|
| 316 |
+
font-weight: bold;
|
| 317 |
+
}
|
| 318 |
+
</style>
|
| 319 |
+
""",
|
| 320 |
+
unsafe_allow_html=True,
|
| 321 |
+
)
|
| 322 |
+
|
| 323 |
+
# Encabezado
|
| 324 |
+
st.image("biblie.jpg")
|
| 325 |
+
st.title("馃摐 Generador de Frases B铆blicas")
|
| 326 |
+
st.markdown("Escribe un vers铆culo o una palabra y obt茅n una frase relacionada de personajes b铆blicos y santos, junto con una imagen alusiva.")
|
| 327 |
+
|
| 328 |
+
# Entrada de texto para el vers铆culo o palabra
|
| 329 |
+
versiculo_usuario = st.text_input("Escribe un vers铆culo o una palabra:")
|
| 330 |
+
|
| 331 |
+
if st.button("Generar"):
|
| 332 |
+
if versiculo_usuario:
|
| 333 |
+
with st.spinner("Generando frase..."):
|
| 334 |
+
frase_relacionada = obtener_frase_relacionada(versiculo_usuario)
|
| 335 |
+
if frase_relacionada:
|
| 336 |
+
st.subheader("Frase relacionada:")
|
| 337 |
+
st.markdown(frase_relacionada)
|
| 338 |
+
|
| 339 |
+
with st.spinner("Generando imagen..."):
|
| 340 |
+
imagen_url = generar_imagen(frase_relacionada)
|
| 341 |
+
if imagen_url:
|
| 342 |
+
st.subheader("Imagen alusiva:")
|
| 343 |
+
st.image(imagen_url)
|
| 344 |
+
else:
|
| 345 |
+
st.warning("Por favor, ingresa un vers铆culo o una palabra antes de generar.")
|