Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from pptx import Presentation
|
| 3 |
-
from pptx.util import Inches
|
| 4 |
import io
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
import json
|
|
@@ -24,12 +24,9 @@ def extract_and_clean_json(text):
|
|
| 24 |
def generate_presentation_content(topic, client, max_retries=3):
|
| 25 |
prompt = f"""Genera una presentación de PowerPoint sobre el tema: "{topic}".
|
| 26 |
Debes crear exactamente 9 diapositivas. Cada diapositiva debe tener un título y contenido.
|
| 27 |
-
También debes proporcionar un título y subtítulo para la presentación.
|
| 28 |
Es CRUCIAL que tu respuesta sea ÚNICAMENTE un JSON válido con la siguiente estructura exacta, sin texto adicional antes o después:
|
| 29 |
|
| 30 |
{{
|
| 31 |
-
"title": "Título de la presentación",
|
| 32 |
-
"subtitle": "Subtítulo de la presentación",
|
| 33 |
"slides": [
|
| 34 |
{{"title": "Título de la diapositiva 1", "content": "Contenido de la diapositiva 1"}},
|
| 35 |
{{"title": "Título de la diapositiva 2", "content": "Contenido de la diapositiva 2"}},
|
|
@@ -52,9 +49,9 @@ def generate_presentation_content(topic, client, max_retries=3):
|
|
| 52 |
try:
|
| 53 |
json_str = extract_and_clean_json(response)
|
| 54 |
if json_str:
|
| 55 |
-
|
| 56 |
-
if 'slides' in
|
| 57 |
-
return
|
| 58 |
|
| 59 |
# Si llegamos aquí, el JSON no era válido o no tenía la estructura correcta
|
| 60 |
raise ValueError("JSON inválido o estructura incorrecta")
|
|
@@ -70,20 +67,10 @@ def generate_presentation_content(topic, client, max_retries=3):
|
|
| 70 |
|
| 71 |
return None
|
| 72 |
|
| 73 |
-
def create_powerpoint(
|
| 74 |
prs = Presentation(template_path)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
title_slide_layout = prs.slide_layouts[0]
|
| 78 |
-
title_slide = prs.slides.add_slide(title_slide_layout)
|
| 79 |
-
title = title_slide.shapes.title
|
| 80 |
-
subtitle = title_slide.placeholders[1]
|
| 81 |
-
|
| 82 |
-
title.text = presentation_data['title']
|
| 83 |
-
subtitle.text = presentation_data['subtitle']
|
| 84 |
-
|
| 85 |
-
# Crear las diapositivas de contenido
|
| 86 |
-
for slide_data in presentation_data['slides']:
|
| 87 |
slide = prs.slides.add_slide(prs.slide_layouts[1])
|
| 88 |
|
| 89 |
title_shape = slide.shapes.title
|
|
@@ -130,16 +117,16 @@ def main():
|
|
| 130 |
if topic:
|
| 131 |
try:
|
| 132 |
with st.spinner("Generando contenido de la presentación..."):
|
| 133 |
-
|
| 134 |
|
| 135 |
-
if
|
| 136 |
template_path = template_options[selected_template]
|
| 137 |
if not os.path.exists(template_path):
|
| 138 |
st.error(f"No se encontró la plantilla: {template_path}")
|
| 139 |
return
|
| 140 |
|
| 141 |
with st.spinner("Creando archivo PowerPoint..."):
|
| 142 |
-
pptx_buffer = create_powerpoint(
|
| 143 |
|
| 144 |
st.success("Presentación generada con éxito!")
|
| 145 |
|
|
@@ -155,4 +142,4 @@ def main():
|
|
| 155 |
st.warning("Por favor, ingrese un tema para la presentación.")
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|
| 158 |
-
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from pptx import Presentation
|
| 3 |
+
from pptx.util import Inches, Pt
|
| 4 |
import io
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
import json
|
|
|
|
| 24 |
def generate_presentation_content(topic, client, max_retries=3):
|
| 25 |
prompt = f"""Genera una presentación de PowerPoint sobre el tema: "{topic}".
|
| 26 |
Debes crear exactamente 9 diapositivas. Cada diapositiva debe tener un título y contenido.
|
|
|
|
| 27 |
Es CRUCIAL que tu respuesta sea ÚNICAMENTE un JSON válido con la siguiente estructura exacta, sin texto adicional antes o después:
|
| 28 |
|
| 29 |
{{
|
|
|
|
|
|
|
| 30 |
"slides": [
|
| 31 |
{{"title": "Título de la diapositiva 1", "content": "Contenido de la diapositiva 1"}},
|
| 32 |
{{"title": "Título de la diapositiva 2", "content": "Contenido de la diapositiva 2"}},
|
|
|
|
| 49 |
try:
|
| 50 |
json_str = extract_and_clean_json(response)
|
| 51 |
if json_str:
|
| 52 |
+
slides_data = json.loads(json_str)
|
| 53 |
+
if 'slides' in slides_data and len(slides_data['slides']) == 9:
|
| 54 |
+
return slides_data['slides']
|
| 55 |
|
| 56 |
# Si llegamos aquí, el JSON no era válido o no tenía la estructura correcta
|
| 57 |
raise ValueError("JSON inválido o estructura incorrecta")
|
|
|
|
| 67 |
|
| 68 |
return None
|
| 69 |
|
| 70 |
+
def create_powerpoint(slides, template_path):
|
| 71 |
prs = Presentation(template_path)
|
| 72 |
|
| 73 |
+
for slide_data in slides:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
slide = prs.slides.add_slide(prs.slide_layouts[1])
|
| 75 |
|
| 76 |
title_shape = slide.shapes.title
|
|
|
|
| 117 |
if topic:
|
| 118 |
try:
|
| 119 |
with st.spinner("Generando contenido de la presentación..."):
|
| 120 |
+
slides = generate_presentation_content(topic, client)
|
| 121 |
|
| 122 |
+
if slides:
|
| 123 |
template_path = template_options[selected_template]
|
| 124 |
if not os.path.exists(template_path):
|
| 125 |
st.error(f"No se encontró la plantilla: {template_path}")
|
| 126 |
return
|
| 127 |
|
| 128 |
with st.spinner("Creando archivo PowerPoint..."):
|
| 129 |
+
pptx_buffer = create_powerpoint(slides, template_path)
|
| 130 |
|
| 131 |
st.success("Presentación generada con éxito!")
|
| 132 |
|
|
|
|
| 142 |
st.warning("Por favor, ingrese un tema para la presentación.")
|
| 143 |
|
| 144 |
if __name__ == "__main__":
|
| 145 |
+
main()
|