Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import os | |
| import time | |
| import base64 | |
| import random | |
| from dotenv import load_dotenv | |
| from google.cloud import texttospeech | |
| import PyPDF2 | |
| from fpdf import FPDF | |
| import tempfile | |
| import requests | |
| # Cargar variables de entorno desde el archivo .env | |
| load_dotenv() | |
| os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "botidinamix-g.json" | |
| groq_api_key = os.getenv("GROQ_API_KEY") | |
| # Verifica que la clave API de Groq est茅 configurada | |
| if not groq_api_key: | |
| st.error("No API key provided for Groq. Please set your API key in the .env file.") | |
| # Configuraci贸n de Streamlit | |
| st.set_page_config(page_title="Asistente Teol贸gico", page_icon="馃摉") | |
| # Estilos CSS personalizados | |
| st.markdown( | |
| """ | |
| <style> | |
| @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&display=swap'); | |
| body { | |
| background-color: #1C1C1C; | |
| color: #ECF0F1; | |
| background-image: url('https://s1.1zoom.me/big0/395/Fields_Sunrises_and_499477.jpg'); | |
| background-size: cover; | |
| font-family: 'Poppins', sans-serif; | |
| } | |
| .stButton>button { | |
| background-color: #007BFF; | |
| color: #FFD700; | |
| border: none; | |
| border-radius: 8px; | |
| font-size: 16px; | |
| padding: 12px 24px; | |
| cursor: pointer; | |
| transition: background-color 0.3s, transform 0.3s; | |
| } | |
| .stButton>button:hover { | |
| background-color: #0056b3; | |
| transform: scale(1.05); | |
| } | |
| .stTextInput>div>div>input { | |
| border: 2px solid #FFD700; | |
| border-radius: 8px; | |
| font-size: 16px; | |
| padding: 10px; | |
| background-color: #000000; | |
| color: #ECF0F1; | |
| } | |
| .stMarkdown>div>p { | |
| color: #D0D3D4; | |
| font-size: 16px; | |
| line-height: 1.6; | |
| } | |
| .stMarkdown>h1, .stMarkdown>h2, .stMarkdown>h3, .stMarkdown>h4, .stMarkdown>h5, .stMarkdown>h6 { | |
| color: #FF6F61; | |
| font-family: 'Poppins', sans-serif; | |
| font-weight: bold; | |
| text-transform: uppercase; | |
| } | |
| .stAudio { | |
| margin-top: 20px; | |
| border: 2px solid #FF6F61; | |
| border-radius: 10px; | |
| } | |
| .stFileUploader>div>div>input { | |
| border: 2px solid #FFD700; | |
| border-radius: 8px; | |
| font-size: 16px; | |
| padding: 10px; | |
| background-color: #000000; | |
| color: #ECF0F1; | |
| } | |
| .spinner { | |
| border: 8px solid #f3f3f3; | |
| border-top: 8px solid #FF6F61; | |
| border-radius: 50%; | |
| width: 60px; | |
| height: 60px; | |
| animation: spin 1s linear infinite; | |
| } | |
| @keyframes spin { | |
| 0% { transform: rotate(0deg); } | |
| 100% { transform: rotate(360deg); } | |
| } | |
| .video-container { | |
| border: 3px solid #FF6F61; | |
| background-color: #1C1C1C; | |
| padding: 10px; | |
| border-radius: 10px; | |
| margin-bottom: 20px. | |
| } | |
| .assistant-response { | |
| font-size: 16px; | |
| color: #D0D3D4; | |
| background-color: #2E2E2E; | |
| padding: 10px; | |
| border-radius: 8px; | |
| border: 1px solid #FF6F61; | |
| } | |
| </style> | |
| """, | |
| unsafe_allow_html=True, | |
| ) | |
| # Encabezado | |
| st.image("biblie.jpg") | |
| st.title("馃摉 LOS C脫DIGOS DE DIOS - BOTIDINAMIX AI") | |
| st.markdown("Bienvenido al Asistente Teol贸gico, donde puedes preguntar sobre interpretaciones y reflexiones b铆blicas.") | |
| # Barra lateral para la navegaci贸n | |
| st.sidebar.title("Navegaci贸n") | |
| page = st.sidebar.selectbox("Selecciona una p谩gina", ["P谩gina Principal", "Chat Asistente", "Generador de Frases B铆blicas", "Recibir Reflexi贸n", "La conexi贸n", "Diario Reflexivo"]) | |
| # Funci贸n para obtener respuesta de Groq | |
| def obtener_respuesta(pregunta, contexto="", modelo="llama3-8b-8192", temperatura=0.5): | |
| """Obtiene una respuesta de Groq basada en el contexto y la pregunta proporcionados.""" | |
| if not pregunta: | |
| st.error("La pregunta no puede estar vac铆a.") | |
| return "Lo siento, la pregunta no puede estar vac铆a." | |
| try: | |
| headers = { | |
| "Authorization": f"Bearer {groq_api_key}", | |
| "Content-Type": "application/json" | |
| } | |
| data = { | |
| "messages": [ | |
| {"role": "system", "content": contexto}, | |
| {"role": "user", "content": pregunta} | |
| ], | |
| "model": modelo, | |
| "temperature": temperatura, | |
| "max_tokens": 1024, | |
| "top_p": 1, | |
| "stop": None, | |
| "stream": False | |
| } | |
| response = requests.post("https://api.groq.com/v1/completions", headers=headers, json=data) | |
| response.raise_for_status() | |
| result = response.json() | |
| return result['choices'][0]['message']['content'].strip() | |
| except Exception as e: | |
| st.error(f"Error al comunicarse con Groq: {e}") | |
| return "Lo siento, no puedo procesar tu solicitud en este momento." | |
| # Funci贸n para generar reflexi贸n usando Groq | |
| def generar_reflexion(keyword): | |
| prompt = f"Genera una reflexi贸n inspiradora sobre {keyword} en el contexto de la espiritualidad y la Biblia." | |
| respuesta = obtener_respuesta(prompt) | |
| return respuesta | |
| # Funci贸n para convertir texto a voz | |
| def text_to_speech_base64(text): | |
| try: | |
| client = texttospeech.TextToSpeechClient() | |
| input_text = texttospeech.SynthesisInput(text=text) | |
| voice = texttospeech.VoiceSelectionParams(language_code="es-ES", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL) | |
| audio_config = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.MP3) | |
| response = client.synthesize_speech(input=input_text, voice=voice, audio_config=audio_config) | |
| return base64.b64encode(response.audio_content).decode("utf-8") | |
| except Exception as e: | |
| return f"Error en la conversi贸n de texto a voz: {e}" | |
| # Funci贸n para obtener una imagen aleatoria de la carpeta "im谩genes" | |
| def obtener_imagen_aleatoria(): | |
| carpeta_imagenes = "imagenes" # Cambia esta ruta por la correcta | |
| archivos = os.listdir(carpeta_imagenes) | |
| archivos_img = [archivo for archivo in archivos if archivo.endswith((".png", ".jpg", ".jpeg"))] | |
| if archivos_img: | |
| imagen_seleccionada = random.choice(archivos_img) | |
| imagen_path = os.path.join(carpeta_imagenes, imagen_seleccionada) | |
| return imagen_path | |
| return None | |
| # Funci贸n para obtener un audio aleatorio de la carpeta "reflexiones" | |
| def obtener_audio_aleatorio(): | |
| carpeta_reflexiones = "reflexiones" # Cambia esta ruta por la correcta | |
| archivos = os.listdir(carpeta_reflexiones) | |
| archivos_mp3 = [archivo for archivo in archivos if archivo.endswith(".mp3")] | |
| if archivos_mp3: | |
| audio_seleccionado = random.choice(archivos_mp3) | |
| with open(os.path.join(carpeta_reflexiones, audio_seleccionado), "rb") as audio_file: | |
| audio_bytes = audio_file.read() | |
| return base64.b64encode(audio_bytes).decode("utf-8"), audio_seleccionado | |
| return None, None | |
| # Funci贸n para extraer texto de un PDF | |
| def extraer_texto_pdf(pdf_path): | |
| text = "" | |
| try: | |
| with open(pdf_path, "rb") as file: | |
| reader = PyPDF2.PdfFileReader(file) | |
| for page_num in range(reader.numPages): | |
| text += reader.getPage(page_num).extract_text() | |
| except Exception as e: | |
| text = f"Error al extraer texto del PDF: {e}" | |
| return text | |
| # Funci贸n "La conexi贸n" para generar oraciones del PDF | |
| def generar_oracion_desde_pdf(): | |
| pdf_path = "diario-de-oraciones.pdf" # Ruta |