Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import google.generativeai as genai | |
| from gtts import gTTS | |
| from io import BytesIO | |
| import os | |
| # =============== TU CLAVE GEMINI =============== | |
| GEMINI_API_KEY = "AIzaSyAyZsHx4BSlUbcYhJE5QVHhtwxJw14D5SA" | |
| # Configuración de página | |
| st.set_page_config( | |
| page_title="Lector Gemini TTS", | |
| page_icon="🔊", | |
| layout="centered" | |
| ) | |
| st.title("🔊 Lector Inteligente con Gemini") | |
| st.markdown("Escribe o pega texto → Gemini lo mejora / responde → Lo lee en voz alta") | |
| # Configurar Gemini | |
| genai.configure(api_key=GEMINI_API_KEY) | |
| model = genai.GenerativeModel('gemini-2.0-flash') | |
| # =============== INTERFAZ =============== | |
| opcion = st.radio("¿Qué quieres hacer?", | |
| ["Leer texto directamente", "Chatear con Gemini y leer respuesta"]) | |
| if opcion == "Leer texto directamente": | |
| texto = st.text_area("Pega el texto que quieres que lea:", height=200) | |
| col1, col2 = st.columns([1, 1]) | |
| with col1: | |
| idioma = st.selectbox("Idioma", ["es", "en", "fr", "pt", "it"], index=0) | |
| with col2: | |
| velocidad = st.checkbox("Hablar más lento", value=False) | |
| if st.button("🔊 Leer texto", type="primary"): | |
| if texto.strip(): | |
| with st.spinner("Generando audio..."): | |
| tts = gTTS(text=texto, lang=idioma, slow=velocidad) | |
| audio_bytes = BytesIO() | |
| tts.write_to_fp(audio_bytes) | |
| audio_bytes.seek(0) | |
| st.audio(audio_bytes, format="audio/mp3") | |
| st.success("¡Reproduciendo!") | |
| else: | |
| st.error("Por favor escribe algo de texto") | |
| else: # Chat con Gemini | |
| st.subheader("💬 Chat con Gemini (respuestas leídas en voz alta)") | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| for msg in st.session_state.messages: | |
| with st.chat_message(msg["role"]): | |
| st.write(msg["content"]) | |
| prompt = st.chat_input("Escribe tu mensaje...") | |
| if prompt: | |
| st.session_state.messages.append({"role": "user", "content": prompt}) | |
| with st.chat_message("user"): | |
| st.write(prompt) | |
| with st.chat_message("assistant"): | |
| with st.spinner("Pensando..."): | |
| response = model.generate_content(prompt) | |
| respuesta = response.text | |
| st.write(respuesta) | |
| st.session_state.messages.append({"role": "assistant", "content": respuesta}) | |
| # Leer respuesta en voz alta | |
| with st.spinner("Generando voz..."): | |
| tts = gTTS(text=respuesta, lang='es', slow=False) | |
| audio_bytes = BytesIO() | |
| tts.write_to_fp(audio_bytes) | |
| audio_bytes.seek(0) | |
| st.audio(audio_bytes, format="audio/mp3") |