File size: 12,094 Bytes
3fe53b9
 
 
 
 
e393388
508e0ed
 
b7da667
 
3fe53b9
b0abaa3
 
 
3fe53b9
 
 
 
 
c3a5730
3fe53b9
f0d2397
 
 
 
 
 
2f69897
 
 
 
 
 
 
 
 
3fe53b9
 
 
c3a5730
d94dd19
 
3fe53b9
5c05cb8
 
2f69897
 
 
b7da667
fe52220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f69897
b7da667
 
 
 
e157e03
b7da667
 
 
 
 
 
 
 
 
 
1376a64
 
 
e393388
 
 
 
 
 
 
1376a64
 
2f69897
f0d2397
b85b822
b7da667
 
0ff427e
b85b822
 
 
 
88f44b0
c3a5730
 
 
 
88f44b0
 
 
 
 
 
 
 
0ff427e
 
 
 
 
88f44b0
 
0ff427e
 
 
b7da667
 
 
0ff427e
b7da667
b85b822
 
 
b7da667
 
 
 
 
37ce3d4
b7da667
 
88f44b0
 
b7da667
 
37ce3d4
 
 
 
f0d2397
1376a64
f0d2397
 
 
 
1376a64
b7da667
 
 
 
 
 
fe52220
b7da667
 
fe52220
 
 
0ff427e
b7da667
 
fe52220
 
b7da667
 
 
 
 
 
 
0ff427e
 
 
 
 
 
b7da667
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3a5730
b7da667
 
 
 
 
 
 
 
fe52220
 
e393388
 
 
b7da667
536b0c5
f9b9c56
f0d2397
 
 
2f69897
b7da667
 
 
 
 
f0d2397
2f69897
d94dd19
f0d2397
1376a64
 
 
 
 
 
 
 
 
f0d2397
1376a64
f0d2397
3fe53b9
 
 
2f69897
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import streamlit as st
import google.generativeai as genai
import os
from dotenv import load_dotenv
from styles import get_custom_css
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-1.5-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 = ""

# 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:
    input_tab1, input_tab2, input_tab3 = st.tabs(["Entrada Manual", "Subir Archivo", "Subir Imagen"])
    
    with input_tab1:
        skills = st.text_area('💪 Tus Habilidades', height=70, 
                            help='Lista tus habilidades y experiencia clave')
        product_service = st.text_area('🎯 Producto/Servicio', height=70,
                                    help='Describe tu producto o servicio')
    
    with input_tab2:
        uploaded_file = st.file_uploader("Sube un archivo con tu información", type=['txt', 'pdf', 'docx'])
        if uploaded_file is not None:
            file_type = uploaded_file.name.split('.')[-1].lower()
            
            if file_type == 'txt':
                file_content = uploaded_file.read().decode('utf-8')
                st.success(f"Archivo cargado correctamente: {uploaded_file.name}")
                
            elif file_type == 'pdf':
                try:
                    import PyPDF2
                    pdf_reader = PyPDF2.PdfReader(uploaded_file)
                    file_content = ""
                    for page in pdf_reader.pages:
                        file_content += page.extract_text() + "\n"
                    st.success(f"Archivo PDF cargado correctamente: {uploaded_file.name}")
                except Exception as e:
                    st.error(f"Error al leer el archivo PDF: {str(e)}")
                    file_content = ""
                    
            elif file_type == 'docx':
                try:
                    import docx
                    doc = docx.Document(uploaded_file)
                    file_content = "\n".join([para.text for para in doc.paragraphs])
                    st.success(f"Archivo DOCX cargado correctamente: {uploaded_file.name}")
                except Exception as e:
                    st.error(f"Error al leer el archivo DOCX: {str(e)}")
                    file_content = ""
            
            with st.expander("Vista previa del contenido"):
                st.write(file_content[:500] + "..." if len(file_content) > 500 else file_content)

    with input_tab3:
        uploaded_image = st.file_uploader("Sube una imagen de tu producto o servicio", type=['jpg', 'jpeg', 'png'])
        if uploaded_image is not None:
            image = Image.open(uploaded_image)
            st.image(image, caption="Imagen cargada", use_container_width=True)
            
            image_bytes = uploaded_image.getvalue()
            image_parts = [
                {
                    "mime_type": uploaded_image.type,
                    "data": image_bytes
                }
            ]
            st.success(f"Imagen cargada correctamente: {uploaded_image.name}")

    with st.expander('⚙️ Configuración Avanzada'):
        target_audience = st.text_area('👥 Público Objetivo', height=70,
                                    help='Describe tu cliente o público ideal')
        
        formula_type = st.selectbox(
            '📋 Tipo de Fórmula',
            options=list(offer_formulas.keys()),
            help='Selecciona el tipo de fórmula para tu oferta'
        )
        
        temperature = st.slider('🌡️ Nivel de Creatividad', min_value=0.0, max_value=2.0, value=0.7,
                            help='Valores más altos hacen que el resultado sea más creativo pero menos enfocado')

    def generate_offer():
        has_manual_input = bool(skills or product_service)
        has_file_input = bool('uploaded_file' in locals() and uploaded_file is not None)
        has_image_input = bool('uploaded_image' in locals() and uploaded_image is not None)

        if not (has_manual_input or has_file_input or has_image_input):
            st.error('Por favor proporciona al menos una entrada: texto, archivo o imagen')
            return

        valid_input = (
            (has_manual_input and (has_file_input or has_image_input)) or
            (not has_manual_input and (has_file_input or has_image_input)) or
            has_file_input or
            has_image_input
        )

        if not valid_input:
            st.error('Por favor ingresa una combinación válida:\n' +
                    '- Texto + (archivo o imagen)\n' +
                    '- Solo archivo o imagen\n' +
                    '- Solo archivo\n' +
                    '- Solo imagen')
            return
        
        st.session_state.submitted = True
        
        if has_manual_input:
            st.session_state.skills = skills if skills else ""
            st.session_state.product_service = product_service if product_service else ""
        
        if has_file_input:
            st.session_state.file_content = file_content
            
        if has_image_input:
            st.session_state.image_parts = image_parts
        
        if has_image_input:
            if has_manual_input and has_file_input:
                st.session_state.input_type = "all"
            elif has_manual_input:
                st.session_state.input_type = "manual_image"
            elif has_file_input:
                st.session_state.input_type = "file_image"
            else:
                st.session_state.input_type = "image"
        else:
            if has_manual_input and has_file_input:
                st.session_state.input_type = "both"
            elif has_file_input:
                st.session_state.input_type = "file"
            elif has_manual_input:
                st.session_state.input_type = "manual"
        
        st.session_state.target_audience = target_audience
        st.session_state.temperature = temperature
        st.session_state.formula_type = formula_type
            
    st.button('Generar Oferta 🎉', on_click=generate_offer)

# Results column
with col2:
    if st.session_state.submitted:
        with st.spinner('Creando tu oferta perfecta...'):
            base_prompt = f"""You are a professional copywriter specializing in creating irresistible offers. 
            Create a compelling and persuasive offer using the {st.session_state.formula_type} formula.
            
            Target Audience: {st.session_state.target_audience if hasattr(st.session_state, 'target_audience') and st.session_state.target_audience else 'General audience'}
            """
            
            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"]}
            
            Please create a professional, engaging, and irresistible offer that highlights the value proposition and creates urgency.
            
            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.markdown('### Oferta Generada')
                st.markdown(st.session_state.offer_result)
                
                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"
                    )
            except Exception as e:
                st.error(f'Ocurrió un error: {str(e)}')
                st.session_state.submitted = False

# Footer
st.markdown('---')
st.markdown('Made with ❤️ by Jesús Cabrera')