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