Spaces:
Sleeping
Sleeping
import streamlit as st | |
import google.generativeai as genai | |
import os | |
import time | |
from dotenv import load_dotenv | |
from styles import get_custom_css, get_response_html_wrapper | |
from formulas import offer_formulas | |
import PyPDF2 | |
import docx | |
from PIL import Image | |
import io | |
# Set page to wide mode to use full width | |
st.set_page_config(layout="wide") | |
# Load environment variables | |
load_dotenv() | |
# Configure Google Gemini API | |
genai.configure(api_key=os.getenv('GOOGLE_API_KEY')) | |
model = genai.GenerativeModel('gemini-2.0-flash') | |
# Initialize session state variables if they don't exist | |
if 'submitted' not in st.session_state: | |
st.session_state.submitted = False | |
if 'offer_result' not in st.session_state: | |
st.session_state.offer_result = "" | |
if 'generated' not in st.session_state: | |
st.session_state.generated = False | |
# Hide Streamlit menu and footer | |
st.markdown(""" | |
<style> | |
#MainMenu {visibility: hidden;} | |
footer {visibility: hidden;} | |
header {visibility: hidden;} | |
</style> | |
""", unsafe_allow_html=True) | |
# Custom CSS | |
st.markdown(get_custom_css(), unsafe_allow_html=True) | |
# App title and description | |
st.markdown('<h1 style="text-align: center;">Great Offer Generator</h1>', unsafe_allow_html=True) | |
st.markdown('<h3 style="text-align: center;">Transform your skills into compelling offers!</h3>', unsafe_allow_html=True) | |
# Create two columns for layout - left column 40%, right column 60% | |
col1, col2 = st.columns([4, 6]) | |
# Main input section in left column | |
with col1: | |
# Keep only the manual input tab | |
with st.container(): | |
# Input fields for user skills/product/service | |
st.markdown("👍 Tus Habilidades") | |
skills = st.text_area("", key="skills", height=70) | |
# Input fields for target audience | |
st.markdown("🎯 Producto/Servicio") | |
target_audience = st.text_area("", key="target_audience", height=70) | |
# Formula type selection | |
st.markdown("📝 Tipo de Fórmula") | |
formula_type = st.selectbox("", list(offer_formulas.keys()), key="formula_type") | |
# Creativity level slider | |
st.markdown("🌡️ Nivel de Creatividad") | |
creativity = st.slider("", min_value=0.0, max_value=2.0, value=1.0, step=0.01, key="creativity") | |
# Generate button - MOVED UP before the advanced configuration | |
if st.button("Generar Oferta 🔥"): | |
# Your existing button logic here | |
pass | |
# Advanced configuration - NOW AFTER the button | |
with st.expander("⚙️ Configuración Avanzada"): | |
# Your existing advanced configuration options here | |
pass | |
# Results column | |
with col2: | |
if st.session_state.submitted and not st.session_state.generated: | |
with st.spinner('Creando tu oferta perfecta...'): | |
base_prompt = f"""You are a professional copywriter specializing in creating irresistible offers. | |
First, analyze the target audience/avatar based on the information provided: | |
Target Audience: {st.session_state.target_audience if hasattr(st.session_state, 'target_audience') and st.session_state.target_audience else 'General audience'} | |
Consider their: | |
- Pain points and frustrations | |
- Desires and aspirations | |
- Current situation | |
- Emotional state | |
- Objections they might have | |
Then, create a compelling and persuasive offer using the {st.session_state.formula_type} formula that speaks directly to this avatar. | |
""" | |
if st.session_state.input_type == "manual": | |
prompt = base_prompt + f""" | |
Based on the following information: | |
Skills: {st.session_state.skills} | |
Product/Service: {st.session_state.product_service} | |
""" | |
elif st.session_state.input_type == "file": | |
prompt = base_prompt + f""" | |
Based on the following information from the uploaded file: | |
File Content: {st.session_state.file_content} | |
""" | |
elif st.session_state.input_type == "image": | |
prompt = base_prompt + f""" | |
Based on the image provided, create an offer that highlights the visual elements and appeals to the target audience. | |
""" | |
elif st.session_state.input_type == "both": | |
prompt = base_prompt + f""" | |
Based on the following combined information: | |
Skills: {st.session_state.skills} | |
Product/Service: {st.session_state.product_service} | |
Additional Information from File: {st.session_state.file_content} | |
Please consider both the manual input and file content to create a comprehensive offer. | |
""" | |
elif st.session_state.input_type == "manual_image": | |
prompt = base_prompt + f""" | |
Based on the following information and the image provided: | |
Skills: {st.session_state.skills} | |
Product/Service: {st.session_state.product_service} | |
Please analyze both the text information and the visual elements in the image to create a comprehensive offer. | |
""" | |
elif st.session_state.input_type == "file_image": | |
prompt = base_prompt + f""" | |
Based on the following information from the uploaded file and the image provided: | |
File Content: {st.session_state.file_content} | |
Please analyze both the file content and the visual elements in the image to create a comprehensive offer. | |
""" | |
else: | |
prompt = base_prompt + f""" | |
Based on all the following information: | |
Skills: {st.session_state.skills} | |
Product/Service: {st.session_state.product_service} | |
Additional Information from File: {st.session_state.file_content} | |
Please analyze the text information, file content, and the visual elements in the image to create the most comprehensive offer. | |
""" | |
prompt += f""" | |
Formula Description: | |
{offer_formulas[st.session_state.formula_type]["description"]} | |
Examples of this formula: | |
{chr(10).join(offer_formulas[st.session_state.formula_type]["examples"])} | |
Please create a professional, engaging, and irresistible offer that: | |
1. Highlights the value proposition based on the skills/product/service provided | |
2. Addresses the specific pain points of the analyzed avatar | |
3. Creates urgency and desire | |
4. STRICTLY follows the structure format of the examples above | |
IMPORTANT: Provide ONLY the final offer text. Do not include any explanations, labels, formatting instructions, brackets, or call to action at the end.""" | |
try: | |
generation_config = genai.GenerationConfig(temperature=st.session_state.temperature) | |
if "image" in st.session_state.input_type: | |
response = model.generate_content([prompt, st.session_state.image_parts[0]], generation_config=generation_config) | |
else: | |
response = model.generate_content(prompt, generation_config=generation_config) | |
st.session_state.offer_result = response.text | |
st.session_state.generated = True # Mark as generated | |
except Exception as e: | |
st.error(f'Ocurrió un error: {str(e)}') | |
st.session_state.submitted = False | |
# Display results if we have an offer result | |
if st.session_state.generated: | |
# With this line that uses the wrapper: | |
st.markdown(get_response_html_wrapper(st.session_state.offer_result), unsafe_allow_html=True) | |
# Add a small space | |
st.markdown('<div style="height: 15px;"></div>', unsafe_allow_html=True) | |
col_download, col_empty = st.columns([8, 2]) | |
with col_download: | |
st.download_button( | |
label="Descargar Oferta", | |
data=st.session_state.offer_result, | |
file_name="oferta_generada.txt", | |
mime="text/plain" | |
) | |
# Footer | |
st.markdown('---') | |
st.markdown('Made with ❤️ by Jesús Cabrera') |