Josedcape commited on
Commit
84fb546
·
verified ·
1 Parent(s): 2d9e371

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -90
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import streamlit as st
2
  import openai
3
  import os
@@ -107,93 +108,4 @@ def generar_reflexion(keyword):
107
 
108
  # Función para convertir texto a voz
109
  def text_to_speech_base64(text):
110
- client = texttospeech.TextToSpeechClient()
111
- input_text = texttospeech.SynthesisInput(text=text)
112
- voice = texttospeech.VoiceSelectionParams(language_code="es-ES", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL)
113
- audio_config = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.MP3)
114
- response = client.synthesize_speech(input=input_text, voice=voice, audio_config=audio_config)
115
- return base64.b64encode(response.audio_content).decode("utf-8")
116
-
117
- # Función para obtener un audio aleatorio de la carpeta "reflexiones"
118
- def obtener_audio_aleatorio():
119
- carpeta_reflexiones = "reflexiones" # Cambia esta ruta por la correcta
120
- archivos = os.listdir(carpeta_reflexiones)
121
- archivos_mp3 = [archivo for archivo in archivos if archivo.endswith(".mp3")]
122
- if archivos_mp3:
123
- audio_seleccionado = random.choice(archivos_mp3)
124
- with open(os.path.join(carpeta_reflexiones, audio_seleccionado), "rb") as audio_file:
125
- audio_bytes = audio_file.read()
126
- return base64.b64encode(audio_bytes).decode("utf-8"), audio_seleccionado
127
- return None, None
128
-
129
- # Función para obtener respuesta del asistente
130
- def obtener_respuesta(pregunta):
131
- prompt = f"Responde a la siguiente pregunta bíblica: {pregunta}"
132
- response = openai.ChatCompletion.create(
133
- model="gpt-4",
134
- messages=[{"role": "system", "content": "Eres un asistente teológico carismático, entusiasmado por ayudar a comprender la palabra de Dios."},
135
- {"role": "user", "content": prompt}],
136
- temperature=0.7,
137
- max_tokens=200,
138
- )
139
- respuesta = response['choices'][0]['message']['content']
140
- return respuesta
141
-
142
- if page == "Chat Asistente":
143
- # Chat con el asistente
144
- st.subheader("🗣️ Chat con el Asistente")
145
- if 'mensajes' not in st.session_state:
146
- st.session_state.mensajes = []
147
-
148
- for mensaje in st.session_state.mensajes:
149
- with st.chat_message(mensaje["role"]):
150
- if isinstance(mensaje["content"], str):
151
- st.markdown(mensaje["content"])
152
- elif isinstance(mensaje["content"], bytes):
153
- st.image(mensaje["content"])
154
-
155
- st.markdown('<div class="video-container">', unsafe_allow_html=True)
156
- st.markdown(
157
- """
158
- <video autoplay loop muted playsinline style="width: 100%;">
159
- <source src="videos/Avatar los codigos de Dios.mp4" type="video/mp4">
160
- </video>
161
- """,
162
- unsafe_allow_html=True,
163
- )
164
- st.markdown('</div>', unsafe_allow_html=True)
165
-
166
- pregunta_usuario = st.text_input("Escribe tu pregunta sobre la Biblia:", key="pregunta_input")
167
- imagen_usuario = st.file_uploader("Sube una imagen (opcional):", type=["png", "jpg", "jpeg"])
168
-
169
- if st.button("Enviar"):
170
- if pregunta_usuario or imagen_usuario:
171
- if pregunta_usuario:
172
- st.session_state.mensajes.append({"role": "user", "content": pregunta_usuario, "timestamp": time.time()})
173
- with st.chat_message("user"):
174
- st.markdown(pregunta_usuario)
175
-
176
- if imagen_usuario:
177
- imagen_bytes = imagen_usuario.getvalue()
178
- st.session_state.mensajes.append({"role": "user", "content": imagen_bytes, "timestamp": time.time()})
179
- with st.chat_message("user"):
180
- st.image(imagen_bytes)
181
-
182
- # Limpiar el campo de texto usando JavaScript para evitar el error de modificación del estado
183
- st.markdown(
184
- """
185
- <script>
186
- document.querySelector('input[type="text"]').value = "";
187
- </script>
188
- """,
189
- unsafe_allow_html=True,
190
- )
191
-
192
- with st.spinner("Generando respuesta..."):
193
- with st.empty():
194
- spinner = st.markdown('<div class="spinner"></div>', unsafe_allow_html=True)
195
- time.sleep(1) # Simulación del tiempo de procesamiento
196
- spinner.empty()
197
-
198
- if pregunta_usuario:
199
- respuesta = obtener_respuesta(pregunta_usuario
 
1
+
2
  import streamlit as st
3
  import openai
4
  import os
 
108
 
109
  # Función para convertir texto a voz
110
  def text_to_speech_base64(text):
111
+ client = texttospeech.TextToSpeechClient