Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -55,3 +55,127 @@ def generate_presentation_content(topic, client):
|
|
| 55 |
else:
|
| 56 |
raise ValueError("No se encontró un JSON válido en la respuesta")
|
| 57 |
except json.JSONDecodeError as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
else:
|
| 56 |
raise ValueError("No se encontró un JSON válido en la respuesta")
|
| 57 |
except json.JSONDecodeError as e:
|
| 58 |
+
st.error(f"Error al decodificar JSON: {str(e)}")
|
| 59 |
+
st.text("JSON procesado (después de limpieza):")
|
| 60 |
+
st.code(json_str)
|
| 61 |
+
st.text("Respuesta original del modelo:")
|
| 62 |
+
st.code(response['generated_text'])
|
| 63 |
+
return None
|
| 64 |
+
except ValueError as e:
|
| 65 |
+
st.error(str(e))
|
| 66 |
+
st.text("Respuesta del modelo:")
|
| 67 |
+
st.code(response['generated_text'])
|
| 68 |
+
return None
|
| 69 |
+
|
| 70 |
+
def apply_design(prs, design):
|
| 71 |
+
if design == "Moderno":
|
| 72 |
+
background = prs.slides[0].background
|
| 73 |
+
fill = background.fill
|
| 74 |
+
fill.solid()
|
| 75 |
+
fill.fore_color.rgb = RGBColor(240, 240, 240)
|
| 76 |
+
|
| 77 |
+
for slide in prs.slides:
|
| 78 |
+
for shape in slide.shapes:
|
| 79 |
+
if shape.has_text_frame:
|
| 80 |
+
tf = shape.text_frame
|
| 81 |
+
tf.text = tf.text
|
| 82 |
+
for paragraph in tf.paragraphs:
|
| 83 |
+
paragraph.font.color.rgb = RGBColor(0, 0, 0)
|
| 84 |
+
if shape.name == 'Title':
|
| 85 |
+
paragraph.font.size = Pt(44)
|
| 86 |
+
else:
|
| 87 |
+
paragraph.font.size = Pt(24)
|
| 88 |
+
elif design == "Corporativo":
|
| 89 |
+
background = prs.slides[0].background
|
| 90 |
+
fill = background.fill
|
| 91 |
+
fill.solid()
|
| 92 |
+
fill.fore_color.rgb = RGBColor(255, 255, 255)
|
| 93 |
+
|
| 94 |
+
for slide in prs.slides:
|
| 95 |
+
left = top = Inches(0.5)
|
| 96 |
+
width = Inches(1)
|
| 97 |
+
height = Inches(0.5)
|
| 98 |
+
shape = slide.shapes.add_shape(MSO_SHAPE.RECTANGLE, left, top, width, height)
|
| 99 |
+
shape.fill.solid()
|
| 100 |
+
shape.fill.fore_color.rgb = RGBColor(0, 112, 192)
|
| 101 |
+
|
| 102 |
+
for shape in slide.shapes:
|
| 103 |
+
if shape.has_text_frame:
|
| 104 |
+
tf = shape.text_frame
|
| 105 |
+
tf.text = tf.text
|
| 106 |
+
for paragraph in tf.paragraphs:
|
| 107 |
+
paragraph.font.color.rgb = RGBColor(0, 0, 0)
|
| 108 |
+
if shape.name == 'Title':
|
| 109 |
+
paragraph.font.size = Pt(40)
|
| 110 |
+
paragraph.font.color.rgb = RGBColor(0, 112, 192)
|
| 111 |
+
else:
|
| 112 |
+
paragraph.font.size = Pt(20)
|
| 113 |
+
|
| 114 |
+
def create_powerpoint(slides, design):
|
| 115 |
+
prs = Presentation()
|
| 116 |
+
|
| 117 |
+
# Diapositiva de título
|
| 118 |
+
slide = prs.slides.add_slide(prs.slide_layouts[0])
|
| 119 |
+
title = slide.shapes.title
|
| 120 |
+
subtitle = slide.placeholders[1]
|
| 121 |
+
title.text = slides[0]['title']
|
| 122 |
+
subtitle.text = slides[0]['content']
|
| 123 |
+
|
| 124 |
+
# Otras diapositivas
|
| 125 |
+
for slide_data in slides[1:]:
|
| 126 |
+
slide = prs.slides.add_slide(prs.slide_layouts[1])
|
| 127 |
+
title = slide.shapes.title
|
| 128 |
+
content = slide.placeholders[1]
|
| 129 |
+
title.text = slide_data['title']
|
| 130 |
+
content.text = slide_data['content']
|
| 131 |
+
|
| 132 |
+
# Diapositiva "Gracias"
|
| 133 |
+
slide = prs.slides.add_slide(prs.slide_layouts[0])
|
| 134 |
+
title = slide.shapes.title
|
| 135 |
+
title.text = "¡Gracias!"
|
| 136 |
+
|
| 137 |
+
apply_design(prs, design)
|
| 138 |
+
|
| 139 |
+
pptx_buffer = io.BytesIO()
|
| 140 |
+
prs.save(pptx_buffer)
|
| 141 |
+
pptx_buffer.seek(0)
|
| 142 |
+
|
| 143 |
+
return pptx_buffer
|
| 144 |
+
|
| 145 |
+
def main():
|
| 146 |
+
st.title("Generador de presentaciones PowerPoint con IA")
|
| 147 |
+
|
| 148 |
+
client = get_inference_client()
|
| 149 |
+
|
| 150 |
+
topic = st.text_input("Por favor, ingrese el tema de la presentación:")
|
| 151 |
+
|
| 152 |
+
design = st.selectbox(
|
| 153 |
+
"Seleccione un diseño para la presentación:",
|
| 154 |
+
("Simple", "Moderno", "Corporativo")
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
if st.button("Generar Presentación"):
|
| 158 |
+
if topic:
|
| 159 |
+
try:
|
| 160 |
+
with st.spinner("Generando contenido de la presentación..."):
|
| 161 |
+
slides = generate_presentation_content(topic, client)
|
| 162 |
+
|
| 163 |
+
if slides:
|
| 164 |
+
with st.spinner("Creando archivo PowerPoint..."):
|
| 165 |
+
pptx_buffer = create_powerpoint(slides, design)
|
| 166 |
+
|
| 167 |
+
st.success("Presentación generada con éxito!")
|
| 168 |
+
|
| 169 |
+
st.download_button(
|
| 170 |
+
label="Descargar Presentación",
|
| 171 |
+
data=pptx_buffer,
|
| 172 |
+
file_name=f"{topic.replace(' ', '_')}_presentacion.pptx",
|
| 173 |
+
mime="application/vnd.openxmlformats-officedocument.presentationml.presentation"
|
| 174 |
+
)
|
| 175 |
+
except Exception as e:
|
| 176 |
+
st.error(f"Ocurrió un error al generar la presentación: {str(e)}")
|
| 177 |
+
else:
|
| 178 |
+
st.warning("Por favor, ingrese un tema para la presentación.")
|
| 179 |
+
|
| 180 |
+
if __name__ == "__main__":
|
| 181 |
+
main()
|