Spaces:
Running
Running
| from dotenv import load_dotenv | |
| import streamlit as st | |
| import os | |
| import google.generativeai as genai | |
| # Cargar variables de entorno | |
| load_dotenv() | |
| # Configurar API de Google Gemini | |
| genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) | |
| # Función para obtener la respuesta del modelo Gemini | |
| def get_gemini_response(input_prompt, genre, length, language, mood, target_audience, product_type): | |
| if not input_prompt: | |
| return "Por favor, escribe un mensaje para generar contenido." | |
| model = genai.GenerativeModel('gemini-1.5-flash') | |
| full_prompt = f""" | |
| You are a creative writer. Create a {genre} in {language} with approximately {length} words. | |
| The {genre} should be {mood} and targeted to {target_audience}. | |
| This is for a {product_type}. | |
| The {genre} should be based on the following prompt: | |
| "{input_prompt}" | |
| Make sure it contains realistic and emotional elements. | |
| """ | |
| response = model.generate_content([full_prompt]) | |
| return response.parts[0].text if response and response.parts else "Error al generar contenido." | |
| # Configurar la aplicación Streamlit | |
| st.set_page_config(page_title="Story Generator", page_icon=":pencil:", layout="wide") | |
| # Título de la app | |
| st.markdown(""" | |
| <h1 style='text-align: center;'>Generador de Historias</h1> | |
| <h3 style='text-align: center;'>Escribe una idea y la IA creará una historia única.</h3> | |
| """, unsafe_allow_html=True) | |
| # Crear dos columnas | |
| col1, col2 = st.columns([1, 1]) | |
| # Columna izquierda para inputs | |
| with col1: | |
| # Entrada de texto principal | |
| input_prompt = st.text_area("Escribe de qué quieres que trate la historia:", placeholder="Escribe aquí tu idea...") | |
| # Campos de texto libre | |
| target_audience = st.text_input("Público Objetivo:", placeholder="Ejemplo: Profesionales de 25-35 años interesados en tecnología...") | |
| product_type = st.text_input("Producto o Servicio:", placeholder="Ejemplo: Curso de marketing digital...") | |
| # Opciones avanzadas en acordeón | |
| with st.expander("Opciones avanzadas"): | |
| genre = st.selectbox("Tipo de texto:", ["Historia", "Poema", "Cita", "Ensayo"]) | |
| length = st.slider("Longitud del texto (palabras):", | |
| min_value=25, | |
| max_value=100, | |
| value=60, | |
| step=5) | |
| language = st.selectbox("Idioma del texto:", ["Español", "Inglés"]) | |
| mood = st.selectbox("Estado de ánimo:", ["Emocional", "Triste", "Feliz", "Horror", "Comedia", "Romántico"]) | |
| generate_button = st.button("Generar historia") | |
| with col2: | |
| if generate_button: | |
| response = get_gemini_response( | |
| input_prompt, genre, length, language, | |
| mood, target_audience, product_type | |
| ) | |
| st.subheader("Contenido generado:") | |
| st.write(response) |