JeCabrera commited on
Commit
1441154
verified
1 Parent(s): ba10dda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -51
app.py CHANGED
@@ -46,7 +46,7 @@ col1, col2 = st.columns([4, 6])
46
 
47
  # Main input section in left column
48
  with col1:
49
- input_tab1, input_tab2, input_tab3 = st.tabs(["Entrada Manual", "Subir Archivo", "Subir Imagen"])
50
 
51
  with input_tab1:
52
  skills = st.text_area('馃挭 Tus Habilidades', height=70,
@@ -55,53 +55,62 @@ with col1:
55
  help='Describe tu producto o servicio')
56
 
57
  with input_tab2:
58
- uploaded_file = st.file_uploader("Sube un archivo con tu informaci贸n", type=['txt', 'pdf', 'docx'])
 
59
  if uploaded_file is not None:
60
  file_type = uploaded_file.name.split('.')[-1].lower()
61
 
62
- if file_type == 'txt':
63
- file_content = uploaded_file.read().decode('utf-8')
64
- st.success(f"Archivo cargado correctamente: {uploaded_file.name}")
65
-
66
- elif file_type == 'pdf':
67
- try:
68
- import PyPDF2
69
- pdf_reader = PyPDF2.PdfReader(uploaded_file)
70
- file_content = ""
71
- for page in pdf_reader.pages:
72
- file_content += page.extract_text() + "\n"
73
- st.success(f"Archivo PDF cargado correctamente: {uploaded_file.name}")
74
- except Exception as e:
75
- st.error(f"Error al leer el archivo PDF: {str(e)}")
76
- file_content = ""
77
 
78
- elif file_type == 'docx':
79
- try:
80
- import docx
81
- doc = docx.Document(uploaded_file)
82
- file_content = "\n".join([para.text for para in doc.paragraphs])
83
- st.success(f"Archivo DOCX cargado correctamente: {uploaded_file.name}")
84
- except Exception as e:
85
- st.error(f"Error al leer el archivo DOCX: {str(e)}")
86
- file_content = ""
87
-
88
- with st.expander("Vista previa del contenido"):
89
- st.write(file_content[:500] + "..." if len(file_content) > 500 else file_content)
90
-
91
- with input_tab3:
92
- uploaded_image = st.file_uploader("Sube una imagen de tu producto o servicio", type=['jpg', 'jpeg', 'png'])
93
- if uploaded_image is not None:
94
- image = Image.open(uploaded_image)
95
- st.image(image, caption="Imagen cargada", use_container_width=True)
 
 
 
 
 
 
 
 
 
 
96
 
97
- image_bytes = uploaded_image.getvalue()
98
- image_parts = [
99
- {
100
- "mime_type": uploaded_image.type,
101
- "data": image_bytes
102
- }
103
- ]
104
- st.success(f"Imagen cargada correctamente: {uploaded_image.name}")
 
 
 
 
 
 
 
 
105
 
106
  with st.expander('鈿欙笍 Configuraci贸n Avanzada'):
107
  target_audience = st.text_area('馃懃 P煤blico Objetivo', height=70,
@@ -118,12 +127,12 @@ with col1:
118
 
119
  def generate_offer():
120
  has_manual_input = bool(skills or product_service)
121
- has_file_input = bool(uploaded_file is not None)
122
- has_image_input = bool(uploaded_image is not None)
123
 
124
  # Simple validation - check if we have at least one input type
125
  if not (has_manual_input or has_file_input or has_image_input):
126
- st.error('Por favor ingresa texto, sube un archivo o una imagen')
127
  return
128
 
129
  st.session_state.submitted = True
@@ -141,12 +150,8 @@ with col1:
141
 
142
  # Set input type based on what's available
143
  if has_image_input:
144
- if has_manual_input and has_file_input:
145
- st.session_state.input_type = "all"
146
- elif has_manual_input:
147
  st.session_state.input_type = "manual_image"
148
- elif has_file_input:
149
- st.session_state.input_type = "file_image"
150
  else:
151
  st.session_state.input_type = "image"
152
  else:
 
46
 
47
  # Main input section in left column
48
  with col1:
49
+ input_tab1, input_tab2 = st.tabs(["Entrada Manual", "Subir Archivo o Imagen"])
50
 
51
  with input_tab1:
52
  skills = st.text_area('馃挭 Tus Habilidades', height=70,
 
55
  help='Describe tu producto o servicio')
56
 
57
  with input_tab2:
58
+ uploaded_file = st.file_uploader("Sube un archivo o imagen",
59
+ type=['txt', 'pdf', 'docx', 'jpg', 'jpeg', 'png'])
60
  if uploaded_file is not None:
61
  file_type = uploaded_file.name.split('.')[-1].lower()
62
 
63
+ # Handle text files
64
+ if file_type in ['txt', 'pdf', 'docx']:
65
+ if file_type == 'txt':
66
+ file_content = uploaded_file.read().decode('utf-8')
67
+ st.success(f"Archivo de texto cargado correctamente: {uploaded_file.name}")
 
 
 
 
 
 
 
 
 
 
68
 
69
+ elif file_type == 'pdf':
70
+ try:
71
+ import PyPDF2
72
+ pdf_reader = PyPDF2.PdfReader(uploaded_file)
73
+ file_content = ""
74
+ for page in pdf_reader.pages:
75
+ file_content += page.extract_text() + "\n"
76
+ st.success(f"Archivo PDF cargado correctamente: {uploaded_file.name}")
77
+ except Exception as e:
78
+ st.error(f"Error al leer el archivo PDF: {str(e)}")
79
+ file_content = ""
80
+
81
+ elif file_type == 'docx':
82
+ try:
83
+ import docx
84
+ doc = docx.Document(uploaded_file)
85
+ file_content = "\n".join([para.text for para in doc.paragraphs])
86
+ st.success(f"Archivo DOCX cargado correctamente: {uploaded_file.name}")
87
+ except Exception as e:
88
+ st.error(f"Error al leer el archivo DOCX: {str(e)}")
89
+ file_content = ""
90
+
91
+ # Display preview of file content
92
+ with st.expander("Vista previa del contenido"):
93
+ st.write(file_content[:500] + "..." if len(file_content) > 500 else file_content)
94
+
95
+ # Set file type flag
96
+ is_image = False
97
 
98
+ # Handle image files
99
+ elif file_type in ['jpg', 'jpeg', 'png']:
100
+ image = Image.open(uploaded_file)
101
+ st.image(image, caption="Imagen cargada", use_container_width=True)
102
+
103
+ image_bytes = uploaded_file.getvalue()
104
+ image_parts = [
105
+ {
106
+ "mime_type": uploaded_file.type,
107
+ "data": image_bytes
108
+ }
109
+ ]
110
+ st.success(f"Imagen cargada correctamente: {uploaded_file.name}")
111
+
112
+ # Set file type flag
113
+ is_image = True
114
 
115
  with st.expander('鈿欙笍 Configuraci贸n Avanzada'):
116
  target_audience = st.text_area('馃懃 P煤blico Objetivo', height=70,
 
127
 
128
  def generate_offer():
129
  has_manual_input = bool(skills or product_service)
130
+ has_file_input = bool(uploaded_file is not None and not is_image)
131
+ has_image_input = bool(uploaded_file is not None and is_image)
132
 
133
  # Simple validation - check if we have at least one input type
134
  if not (has_manual_input or has_file_input or has_image_input):
135
+ st.error('Por favor ingresa texto o sube un archivo/imagen')
136
  return
137
 
138
  st.session_state.submitted = True
 
150
 
151
  # Set input type based on what's available
152
  if has_image_input:
153
+ if has_manual_input:
 
 
154
  st.session_state.input_type = "manual_image"
 
 
155
  else:
156
  st.session_state.input_type = "image"
157
  else: