Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,119 +18,11 @@ openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
| 18 |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path/to/your/google-cloud-credentials.json"
|
| 19 |
|
| 20 |
# Inicialización de Vertex AI
|
| 21 |
-
vertexai.init(project="gen-lang-client-0608257787", location="us-
|
| 22 |
|
| 23 |
# Inicialización del modelo Gemini usando Vertex AI
|
| 24 |
model = GenerativeModel(model_name="gemini-1.0-pro-vision")
|
| 25 |
|
| 26 |
-
# Clases para la gestión de pedidos
|
| 27 |
-
class PedidoAgent:
|
| 28 |
-
def __init__(self, menu_csv_path):
|
| 29 |
-
self.menu_csv_path = menu_csv_path
|
| 30 |
-
self.menu_data = self.load_menu()
|
| 31 |
-
|
| 32 |
-
def load_menu(self):
|
| 33 |
-
try:
|
| 34 |
-
df_menu = pd.read_csv(self.menu_csv_path)
|
| 35 |
-
productos = df_menu["Producto"].tolist()
|
| 36 |
-
precios = df_menu["Precio"].tolist()
|
| 37 |
-
return {"productos": productos, "precios": precios}
|
| 38 |
-
except Exception as e:
|
| 39 |
-
st.error(f"Error al cargar el menú: {e}")
|
| 40 |
-
return None
|
| 41 |
-
|
| 42 |
-
def realizar_pedido(self, state):
|
| 43 |
-
if not self.menu_data:
|
| 44 |
-
st.warning("No se pudo cargar el menú.")
|
| 45 |
-
return
|
| 46 |
-
|
| 47 |
-
productos = self.menu_data["productos"]
|
| 48 |
-
precios = self.menu_data["precios"]
|
| 49 |
-
|
| 50 |
-
# Interfaz de usuario para seleccionar productos y cantidades
|
| 51 |
-
with st.form("pedido_form"):
|
| 52 |
-
producto_seleccionado = st.selectbox("Producto:", productos)
|
| 53 |
-
cantidad = st.number_input("Cantidad:", min_value=1, value=1)
|
| 54 |
-
submitted = st.form_submit_button("Agregar al pedido")
|
| 55 |
-
|
| 56 |
-
if submitted:
|
| 57 |
-
precio = precios[productos.index(producto_seleccionado)]
|
| 58 |
-
state.pedidos.append(
|
| 59 |
-
{"Producto": producto_seleccionado, "Cantidad": cantidad, "Precio": precio}
|
| 60 |
-
)
|
| 61 |
-
st.success("Pedido agregado correctamente")
|
| 62 |
-
|
| 63 |
-
# Mostrar el pedido actual
|
| 64 |
-
st.subheader("Pedido actual:")
|
| 65 |
-
if state.pedidos:
|
| 66 |
-
df_pedidos = pd.DataFrame(state.pedidos)
|
| 67 |
-
st.table(df_pedidos)
|
| 68 |
-
|
| 69 |
-
class CalculoPedidoAgent:
|
| 70 |
-
def calcular_total(self, state):
|
| 71 |
-
st.subheader("Resumen del Pedido")
|
| 72 |
-
if state.pedidos:
|
| 73 |
-
df_pedidos = pd.DataFrame(state.pedidos)
|
| 74 |
-
st.table(df_pedidos)
|
| 75 |
-
total = df_pedidos["Cantidad"].mul(df_pedidos["Precio"]).sum()
|
| 76 |
-
st.markdown(f"**Total: ${total:.2f}**")
|
| 77 |
-
else:
|
| 78 |
-
st.info("El pedido está vacío.")
|
| 79 |
-
|
| 80 |
-
# Función para obtener respuesta de OpenAI
|
| 81 |
-
def obtener_respuesta(pregunta, modelo="gpt-4", temperatura=0.5):
|
| 82 |
-
response = openai.ChatCompletion.create(
|
| 83 |
-
model=modelo,
|
| 84 |
-
messages=[{"role": "system", "content": "You are a knowledgeable theological assistant."},
|
| 85 |
-
{"role": "user", "content": pregunta}],
|
| 86 |
-
temperature=temperatura,
|
| 87 |
-
max_tokens=150,
|
| 88 |
-
)
|
| 89 |
-
respuesta = response['choices'][0]['message']['content']
|
| 90 |
-
return respuesta
|
| 91 |
-
|
| 92 |
-
# Función para convertir texto a voz usando Google Cloud Text-to-Speech
|
| 93 |
-
def text_to_speech(text):
|
| 94 |
-
client = texttospeech.TextToSpeechClient()
|
| 95 |
-
synthesis_input = texttospeech.SynthesisInput(text=text)
|
| 96 |
-
voice = texttospeech.VoiceSelectionParams(language_code="es-ES", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL)
|
| 97 |
-
audio_config = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.MP3)
|
| 98 |
-
response = client.synthesize_speech(input=synthesis_input, voice=voice, audio_config=audio_config)
|
| 99 |
-
audio_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3").name
|
| 100 |
-
with open(audio_path, "wb") as out:
|
| 101 |
-
out.write(response.audio_content)
|
| 102 |
-
return audio_path
|
| 103 |
-
|
| 104 |
-
def text_to_speech_base64(text):
|
| 105 |
-
audio_path = text_to_speech(text)
|
| 106 |
-
with open(audio_path, "rb") as audio_file:
|
| 107 |
-
audio_bytes = audio_file.read()
|
| 108 |
-
audio_base64 = base64.b64encode(audio_bytes).decode("utf-8")
|
| 109 |
-
return audio_base64
|
| 110 |
-
|
| 111 |
-
# Clase para procesar audio
|
| 112 |
-
class AudioProcessor(AudioProcessorBase):
|
| 113 |
-
def __init__(self):
|
| 114 |
-
self.audio_bytes = b''
|
| 115 |
-
|
| 116 |
-
def recv(self, frame):
|
| 117 |
-
self.audio_bytes += frame.to_ndarray().tobytes()
|
| 118 |
-
return frame
|
| 119 |
-
|
| 120 |
-
# Función para transcribir audio a texto usando Google Cloud Speech-to-Text
|
| 121 |
-
def transcribir_audio(audio_bytes):
|
| 122 |
-
client = SpeechClient()
|
| 123 |
-
audio = RecognitionAudio(content=audio_bytes)
|
| 124 |
-
config = RecognitionConfig(
|
| 125 |
-
encoding=RecognitionConfig.AudioEncoding.LINEAR16,
|
| 126 |
-
sample_rate_hertz=16000,
|
| 127 |
-
language_code="es-ES",
|
| 128 |
-
)
|
| 129 |
-
response = client.recognize(config=config, audio=audio)
|
| 130 |
-
for result in response.results:
|
| 131 |
-
return result.alternatives[0].transcript
|
| 132 |
-
return ""
|
| 133 |
-
|
| 134 |
# Configuración de Streamlit
|
| 135 |
st.set_page_config(page_title="Asistente Teológico", page_icon="📖")
|
| 136 |
|
|
@@ -284,38 +176,8 @@ elif page == "Generador de Frases Bíblicas":
|
|
| 284 |
st.error("Error al generar la imagen")
|
| 285 |
return None
|
| 286 |
|
| 287 |
-
# Configuración de Streamlit
|
| 288 |
-
st.set_page_config(page_title="Generador de Frases Bíblicas", page_icon="📜")
|
| 289 |
-
|
| 290 |
-
# Estilos CSS personalizados
|
| 291 |
-
st.markdown(
|
| 292 |
-
"""
|
| 293 |
-
<style>
|
| 294 |
-
body {
|
| 295 |
-
background: linear-gradient(to right, #f2f3f5, #ffcccb);
|
| 296 |
-
color: #333;
|
| 297 |
-
}
|
| 298 |
-
.stButton>button {
|
| 299 |
-
background-color: #4CAF50;
|
| 300 |
-
color: white;
|
| 301 |
-
border-radius: 10px;
|
| 302 |
-
}
|
| 303 |
-
.stTextInput>div>div>input {
|
| 304 |
-
border: 1px solid #4CAF50;
|
| 305 |
-
border-radius: 10px;
|
| 306 |
-
}
|
| 307 |
-
.stMarkdown>div>p {
|
| 308 |
-
color: #4CAF50;
|
| 309 |
-
font-weight: bold;
|
| 310 |
-
}
|
| 311 |
-
</style>
|
| 312 |
-
""",
|
| 313 |
-
unsafe_allow_html=True,
|
| 314 |
-
)
|
| 315 |
-
|
| 316 |
# Encabezado
|
| 317 |
-
st.
|
| 318 |
-
st.title("📜 Generador de Frases Bíblicas")
|
| 319 |
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.")
|
| 320 |
|
| 321 |
# Entrada de texto para el versículo o palabra
|
|
|
|
| 18 |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path/to/your/google-cloud-credentials.json"
|
| 19 |
|
| 20 |
# Inicialización de Vertex AI
|
| 21 |
+
vertexai.init(project="gen-lang-client-0608257787", location="us-central1") # Reemplaza "your-project-id" y "us-central1"
|
| 22 |
|
| 23 |
# Inicialización del modelo Gemini usando Vertex AI
|
| 24 |
model = GenerativeModel(model_name="gemini-1.0-pro-vision")
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
# Configuración de Streamlit
|
| 27 |
st.set_page_config(page_title="Asistente Teológico", page_icon="📖")
|
| 28 |
|
|
|
|
| 176 |
st.error("Error al generar la imagen")
|
| 177 |
return None
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
# Encabezado
|
| 180 |
+
st.subheader("📜 Generador de Frases Bíblicas")
|
|
|
|
| 181 |
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.")
|
| 182 |
|
| 183 |
# Entrada de texto para el versículo o palabra
|