Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,4 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import google.auth
|
| 3 |
-
import google.auth.transport.requests
|
| 4 |
-
import google.oauth2.credentials
|
| 5 |
-
import google_auth_oauthlib.flow
|
| 6 |
-
import googleapiclient.discovery
|
| 7 |
import os
|
| 8 |
import time
|
| 9 |
import base64
|
|
@@ -14,11 +9,12 @@ from google.cloud import texttospeech
|
|
| 14 |
import PyPDF2
|
| 15 |
from fpdf import FPDF
|
| 16 |
import tempfile
|
| 17 |
-
|
| 18 |
|
| 19 |
# Cargar variables de entorno desde el archivo .env
|
| 20 |
load_dotenv()
|
| 21 |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "botidinamix-g.json"
|
|
|
|
| 22 |
|
| 23 |
# Configuraci贸n de Streamlit
|
| 24 |
st.set_page_config(page_title="Asistente Teol贸gico", page_icon="馃摉")
|
|
@@ -53,46 +49,46 @@ st.markdown(
|
|
| 53 |
color: black;
|
| 54 |
max-height: 300px;
|
| 55 |
overflow-y: auto;
|
| 56 |
-
font-size: 16px
|
| 57 |
}
|
| 58 |
.stMarkdown>h1, .stMarkdown>h2, .stMarkdown>h3, .stMarkdown>h4, .stMarkdown>h5, .stMarkdown>h6 {
|
| 59 |
color: white;
|
| 60 |
font-weight: bold;
|
| 61 |
-
font-style: italic
|
| 62 |
}
|
| 63 |
.stAudio {
|
| 64 |
margin-top: 20px;
|
| 65 |
-
border: 2px solid #4CAF50
|
| 66 |
-
border-radius: 10px
|
| 67 |
}
|
| 68 |
.stFileUploader>div>div>input {
|
| 69 |
-
border: 1px solid #4CAF50
|
| 70 |
-
border-radius: 10px
|
| 71 |
-
font-size: 16px
|
| 72 |
}
|
| 73 |
.spinner {
|
| 74 |
-
border: 8px solid #f3f3f3
|
| 75 |
-
border-top: 8px solid #4CAF50
|
| 76 |
-
border-radius: 50%
|
| 77 |
-
width: 60px
|
| 78 |
-
height: 60px
|
| 79 |
-
animation: spin 1s linear infinite
|
| 80 |
}
|
| 81 |
@keyframes spin {
|
| 82 |
-
0% { transform: rotate(0deg)
|
| 83 |
-
100% { transform: rotate(360deg)
|
| 84 |
}
|
| 85 |
.video-container {
|
| 86 |
-
border: 3px solid blue
|
| 87 |
-
background-color: gold
|
| 88 |
-
padding: 10px
|
| 89 |
-
border-radius: 10px
|
| 90 |
-
margin-bottom: 20px
|
| 91 |
}
|
| 92 |
.assistant-response {
|
| 93 |
-
max-height: 200px
|
| 94 |
-
overflow-y: auto
|
| 95 |
-
font-size: 16px
|
| 96 |
}
|
| 97 |
</style>
|
| 98 |
""",
|
|
@@ -119,12 +115,10 @@ def listar_imagenes(carpeta):
|
|
| 119 |
imagenes.append(os.path.join(carpeta, archivo))
|
| 120 |
return imagenes
|
| 121 |
|
| 122 |
-
# Funci贸n para generar reflexi贸n con Google Gemini
|
| 123 |
def generar_reflexion(keyword):
|
| 124 |
prompt = f"Genera una reflexi贸n inspiradora sobre {keyword} en el contexto de la espiritualidad y la Biblia."
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
respuesta = response['choices'][0]['message']['content']
|
| 128 |
return respuesta
|
| 129 |
|
| 130 |
# Funci贸n para convertir texto a voz
|
|
@@ -140,7 +134,7 @@ def text_to_speech_base64(text):
|
|
| 140 |
def obtener_audio_aleatorio():
|
| 141 |
carpeta_reflexiones = "reflexiones" # Cambia esta ruta por la correcta
|
| 142 |
archivos = os.listdir(carpeta_reflexiones)
|
| 143 |
-
archivos_mp3 = [archivo for archivo in archivos
|
| 144 |
if archivos_mp3:
|
| 145 |
audio_seleccionado = random.choice(archivos_mp3)
|
| 146 |
with open(os.path.join(carpeta_reflexiones, audio_seleccionado), "rb") as audio_file:
|
|
@@ -149,12 +143,8 @@ def obtener_audio_aleatorio():
|
|
| 149 |
return None, None
|
| 150 |
|
| 151 |
def google_gemini_api_request(prompt):
|
| 152 |
-
model =
|
| 153 |
-
response = model.
|
| 154 |
-
prompt=prompt,
|
| 155 |
-
temperature=0.7, # Ajusta la temperatura para controlar la creatividad
|
| 156 |
-
max_output_tokens=512 # Limita la longitud de la respuesta
|
| 157 |
-
)
|
| 158 |
return response
|
| 159 |
|
| 160 |
# Funci贸n para extraer texto de un PDF
|
|
@@ -171,9 +161,8 @@ def generar_oracion_desde_pdf():
|
|
| 171 |
pdf_path = "diario-de-oraciones.pdf" # Ruta al PDF
|
| 172 |
texto_pdf = extraer_texto_pdf(pdf_path)
|
| 173 |
prompt = f"Genera una oraci贸n inspiradora basada en el siguiente texto: {texto_pdf[:2000]}" # Limitar a los primeros 2000 caracteres
|
| 174 |
-
# Aqu铆 deber铆as reemplazar con la llamada adecuada a la API de Google Gemini
|
| 175 |
response = google_gemini_api_request(prompt)
|
| 176 |
-
respuesta = response
|
| 177 |
return respuesta
|
| 178 |
|
| 179 |
# Funci贸n para generar y descargar un PDF con la entrada del diario
|
|
@@ -189,12 +178,9 @@ def generar_pdf(fecha, versiculo, rema, investigacion, reflexion):
|
|
| 189 |
pdf.cell(0, 10, f"Vers铆culo: {versiculo}", ln=True, fill=True)
|
| 190 |
pdf.cell(0, 10, "Rema:", ln=True, fill=True)
|
| 191 |
pdf.multi_cell(0, 10, rema, fill=True)
|
| 192 |
-
pdf.cell(0, 10, "Investigaci贸n:",
|
| 193 |
-
pdf.multi_cell(0, 10, investigacion, fill=True)
|
| 194 |
-
pdf.cell(0, 10, "Reflexi贸n:", ln=True, fill=True)
|
| 195 |
-
pdf.multi_cell(0, 10, reflexion, fill=True)
|
| 196 |
|
| 197 |
-
|
| 198 |
pdf.output(tmpfile.name)
|
| 199 |
return tmpfile.name
|
| 200 |
|
|
@@ -358,5 +344,4 @@ elif page == "Diario Reflexivo":
|
|
| 358 |
data=f,
|
| 359 |
file_name="diario_reflexivo.pdf",
|
| 360 |
mime="application/pdf"
|
| 361 |
-
)
|
| 362 |
-
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import time
|
| 4 |
import base64
|
|
|
|
| 9 |
import PyPDF2
|
| 10 |
from fpdf import FPDF
|
| 11 |
import tempfile
|
| 12 |
+
import google.generativeai as genai
|
| 13 |
|
| 14 |
# Cargar variables de entorno desde el archivo .env
|
| 15 |
load_dotenv()
|
| 16 |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "botidinamix-g.json"
|
| 17 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 18 |
|
| 19 |
# Configuraci贸n de Streamlit
|
| 20 |
st.set_page_config(page_title="Asistente Teol贸gico", page_icon="馃摉")
|
|
|
|
| 49 |
color: black;
|
| 50 |
max-height: 300px;
|
| 51 |
overflow-y: auto;
|
| 52 |
+
font-size: 16px.
|
| 53 |
}
|
| 54 |
.stMarkdown>h1, .stMarkdown>h2, .stMarkdown>h3, .stMarkdown>h4, .stMarkdown>h5, .stMarkdown>h6 {
|
| 55 |
color: white;
|
| 56 |
font-weight: bold;
|
| 57 |
+
font-style: italic.
|
| 58 |
}
|
| 59 |
.stAudio {
|
| 60 |
margin-top: 20px;
|
| 61 |
+
border: 2px solid #4CAF50.
|
| 62 |
+
border-radius: 10px.
|
| 63 |
}
|
| 64 |
.stFileUploader>div>div>input {
|
| 65 |
+
border: 1px solid #4CAF50.
|
| 66 |
+
border-radius: 10px.
|
| 67 |
+
font-size: 16px.
|
| 68 |
}
|
| 69 |
.spinner {
|
| 70 |
+
border: 8px solid #f3f3f3.
|
| 71 |
+
border-top: 8px solid #4CAF50.
|
| 72 |
+
border-radius: 50%.
|
| 73 |
+
width: 60px.
|
| 74 |
+
height: 60px.
|
| 75 |
+
animation: spin 1s linear infinite.
|
| 76 |
}
|
| 77 |
@keyframes spin {
|
| 78 |
+
0% { transform: rotate(0deg). }
|
| 79 |
+
100% { transform: rotate(360deg). }
|
| 80 |
}
|
| 81 |
.video-container {
|
| 82 |
+
border: 3px solid blue.
|
| 83 |
+
background-color: gold.
|
| 84 |
+
padding: 10px.
|
| 85 |
+
border-radius: 10px.
|
| 86 |
+
margin-bottom: 20px.
|
| 87 |
}
|
| 88 |
.assistant-response {
|
| 89 |
+
max-height: 200px.
|
| 90 |
+
overflow-y: auto.
|
| 91 |
+
font-size: 16px.
|
| 92 |
}
|
| 93 |
</style>
|
| 94 |
""",
|
|
|
|
| 115 |
imagenes.append(os.path.join(carpeta, archivo))
|
| 116 |
return imagenes
|
| 117 |
|
|
|
|
| 118 |
def generar_reflexion(keyword):
|
| 119 |
prompt = f"Genera una reflexi贸n inspiradora sobre {keyword} en el contexto de la espiritualidad y la Biblia."
|
| 120 |
+
response = genai.GenerativeModel('models/gemini-1.5-pro').generate_content(prompt)
|
| 121 |
+
respuesta = response.text # Accede al texto de la respuesta
|
|
|
|
| 122 |
return respuesta
|
| 123 |
|
| 124 |
# Funci贸n para convertir texto a voz
|
|
|
|
| 134 |
def obtener_audio_aleatorio():
|
| 135 |
carpeta_reflexiones = "reflexiones" # Cambia esta ruta por la correcta
|
| 136 |
archivos = os.listdir(carpeta_reflexiones)
|
| 137 |
+
archivos_mp3 = [archivo for archivo in archivos si archivo.endswith(".mp3")]
|
| 138 |
if archivos_mp3:
|
| 139 |
audio_seleccionado = random.choice(archivos_mp3)
|
| 140 |
with open(os.path.join(carpeta_reflexiones, audio_seleccionado), "rb") as audio_file:
|
|
|
|
| 143 |
return None, None
|
| 144 |
|
| 145 |
def google_gemini_api_request(prompt):
|
| 146 |
+
model = genai.GenerativeModel('models/gemini-1.5-pro') # Reemplaza con el ID real
|
| 147 |
+
response = model.generate_content(prompt=prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
return response
|
| 149 |
|
| 150 |
# Funci贸n para extraer texto de un PDF
|
|
|
|
| 161 |
pdf_path = "diario-de-oraciones.pdf" # Ruta al PDF
|
| 162 |
texto_pdf = extraer_texto_pdf(pdf_path)
|
| 163 |
prompt = f"Genera una oraci贸n inspiradora basada en el siguiente texto: {texto_pdf[:2000]}" # Limitar a los primeros 2000 caracteres
|
|
|
|
| 164 |
response = google_gemini_api_request(prompt)
|
| 165 |
+
respuesta = response.text
|
| 166 |
return respuesta
|
| 167 |
|
| 168 |
# Funci贸n para generar y descargar un PDF con la entrada del diario
|
|
|
|
| 178 |
pdf.cell(0, 10, f"Vers铆culo: {versiculo}", ln=True, fill=True)
|
| 179 |
pdf.cell(0, 10, "Rema:", ln=True, fill=True)
|
| 180 |
pdf.multi_cell(0, 10, rema, fill=True)
|
| 181 |
+
pdf.cell(0, 10, "Investigaci贸n:", lnPara utilizar la API de Google Gemini en lugar de OpenAI en tu aplicaci贸n de Streamlit, primero aseg煤rate de seguir correctamente los pasos de configuraci贸n y uso de la API de Google Gemini. Aqu铆 te dejo una gu铆a y un ejemplo de c贸mo hacerlo:
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmpfile:
|
| 184 |
pdf.output(tmpfile.name)
|
| 185 |
return tmpfile.name
|
| 186 |
|
|
|
|
| 344 |
data=f,
|
| 345 |
file_name="diario_reflexivo.pdf",
|
| 346 |
mime="application/pdf"
|
| 347 |
+
)
|
|
|