Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,16 @@
|
|
| 1 |
-
import time
|
| 2 |
import pandas as pd
|
| 3 |
import streamlit as st
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
import openai
|
| 6 |
import os
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Configuración de la clave API
|
| 9 |
load_dotenv()
|
| 10 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 11 |
|
| 12 |
# Clases para la gestión de pedidos
|
| 13 |
class PedidoAgent:
|
|
@@ -75,6 +78,41 @@ def obtener_respuesta(pregunta, modelo="gpt-4", temperatura=0.5):
|
|
| 75 |
respuesta = response['choices'][0]['message']['content']
|
| 76 |
return respuesta
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
# Configuración de Streamlit
|
| 79 |
st.set_page_config(page_title="Asistente Teológico", page_icon="📖")
|
| 80 |
|
|
@@ -114,7 +152,7 @@ st.markdown(
|
|
| 114 |
)
|
| 115 |
|
| 116 |
# Encabezado
|
| 117 |
-
st.image("
|
| 118 |
st.title("📖 Asistente Teológico - BOTIDINAMIX AI")
|
| 119 |
st.markdown("Bienvenido al Asistente Teológico, donde puedes preguntar sobre interpretaciones y reflexiones bíblicas.")
|
| 120 |
|
|
@@ -139,6 +177,10 @@ if st.button("Enviar"):
|
|
| 139 |
st.session_state.mensajes.append({"role": "assistant", "content": respuesta, "timestamp": time.time()})
|
| 140 |
with st.chat_message("assistant"):
|
| 141 |
st.markdown(respuesta)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
else:
|
| 143 |
st.warning("Por favor, ingresa una pregunta antes de enviar.")
|
| 144 |
|
|
@@ -153,3 +195,26 @@ calculo_pedido_agent = CalculoPedidoAgent()
|
|
| 153 |
|
| 154 |
pedido_agent.realizar_pedido(st.session_state)
|
| 155 |
calculo_pedido_agent.calcular_total(st.session_state)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import streamlit as st
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
import openai
|
| 5 |
import os
|
| 6 |
+
import time
|
| 7 |
+
from google.cloud import texttospeech, speech
|
| 8 |
+
from streamlit_webrtc import webrtc_streamer, WebRtcMode, AudioProcessorBase
|
| 9 |
|
| 10 |
# Configuración de la clave API
|
| 11 |
load_dotenv()
|
| 12 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 13 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path/to/your/google-cloud-credentials.json"
|
| 14 |
|
| 15 |
# Clases para la gestión de pedidos
|
| 16 |
class PedidoAgent:
|
|
|
|
| 78 |
respuesta = response['choices'][0]['message']['content']
|
| 79 |
return respuesta
|
| 80 |
|
| 81 |
+
# Función para convertir texto a voz usando Google Cloud Text-to-Speech
|
| 82 |
+
def text_to_speech(text):
|
| 83 |
+
client = texttospeech.TextToSpeechClient()
|
| 84 |
+
synthesis_input = texttospeech.SynthesisInput(text=text)
|
| 85 |
+
voice = texttospeech.VoiceSelectionParams(language_code="es-ES", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL)
|
| 86 |
+
audio_config = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.MP3)
|
| 87 |
+
response = client.synthesize_speech(input=synthesis_input, voice=voice, audio_config=audio_config)
|
| 88 |
+
audio_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3").name
|
| 89 |
+
with open(audio_path, "wb") as out:
|
| 90 |
+
out.write(response.audio_content)
|
| 91 |
+
return audio_path
|
| 92 |
+
|
| 93 |
+
# Clase para procesar audio
|
| 94 |
+
class AudioProcessor(AudioProcessorBase):
|
| 95 |
+
def __init__(self):
|
| 96 |
+
self.audio_bytes = b''
|
| 97 |
+
|
| 98 |
+
def recv(self, frame):
|
| 99 |
+
self.audio_bytes += frame.to_ndarray().tobytes()
|
| 100 |
+
return frame
|
| 101 |
+
|
| 102 |
+
# Función para transcribir audio a texto usando Google Cloud Speech-to-Text
|
| 103 |
+
def transcribir_audio(audio_bytes):
|
| 104 |
+
client = speech.SpeechClient()
|
| 105 |
+
audio = speech.RecognitionAudio(content=audio_bytes)
|
| 106 |
+
config = speech.RecognitionConfig(
|
| 107 |
+
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
|
| 108 |
+
sample_rate_hertz=16000,
|
| 109 |
+
language_code="es-ES",
|
| 110 |
+
)
|
| 111 |
+
response = client.recognize(config=config, audio=audio)
|
| 112 |
+
for result in response.results:
|
| 113 |
+
return result.alternatives[0].transcript
|
| 114 |
+
return ""
|
| 115 |
+
|
| 116 |
# Configuración de Streamlit
|
| 117 |
st.set_page_config(page_title="Asistente Teológico", page_icon="📖")
|
| 118 |
|
|
|
|
| 152 |
)
|
| 153 |
|
| 154 |
# Encabezado
|
| 155 |
+
st.image("biblia.jpg")
|
| 156 |
st.title("📖 Asistente Teológico - BOTIDINAMIX AI")
|
| 157 |
st.markdown("Bienvenido al Asistente Teológico, donde puedes preguntar sobre interpretaciones y reflexiones bíblicas.")
|
| 158 |
|
|
|
|
| 177 |
st.session_state.mensajes.append({"role": "assistant", "content": respuesta, "timestamp": time.time()})
|
| 178 |
with st.chat_message("assistant"):
|
| 179 |
st.markdown(respuesta)
|
| 180 |
+
|
| 181 |
+
# Convertir texto a voz
|
| 182 |
+
audio_path = text_to_speech(respuesta)
|
| 183 |
+
st.audio(audio_path, format="audio/mp3", start_time=0)
|
| 184 |
else:
|
| 185 |
st.warning("Por favor, ingresa una pregunta antes de enviar.")
|
| 186 |
|
|
|
|
| 195 |
|
| 196 |
pedido_agent.realizar_pedido(st.session_state)
|
| 197 |
calculo_pedido_agent.calcular_total(st.session_state)
|
| 198 |
+
|
| 199 |
+
# Captura de audio y transcripción
|
| 200 |
+
st.subheader("🎤 Captura de voz y transcripción")
|
| 201 |
+
if st.button("Grabar 🎙️"):
|
| 202 |
+
webrtc_ctx = webrtc_streamer(
|
| 203 |
+
key="example",
|
| 204 |
+
mode=WebRtcMode.SENDONLY,
|
| 205 |
+
audio_receiver_size=256,
|
| 206 |
+
rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]},
|
| 207 |
+
media_stream_constraints={"audio": True},
|
| 208 |
+
audio_processor_factory=AudioProcessor,
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
if webrtc_ctx.audio_receiver:
|
| 212 |
+
audio_frames = webrtc_ctx.audio_receiver.get_frames(timeout=1)
|
| 213 |
+
for audio_frame in audio_frames:
|
| 214 |
+
audio_bytes = audio_frame.to_ndarray().tobytes()
|
| 215 |
+
transcripcion = transcribir_audio(audio_bytes)
|
| 216 |
+
if transcripcion:
|
| 217 |
+
st.session_state.mensajes.append({"role": "user", "content": transcripcion, "timestamp": time.time()})
|
| 218 |
+
with st.chat_message("user"):
|
| 219 |
+
st.markdown(transcripcion)
|
| 220 |
+
break # Solo capturamos una vez por grabación
|