Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,49 @@
|
|
| 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
|
| 7 |
-
import re
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
return InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
def generate_presentation_content(topic,
|
| 20 |
-
prompt = f"
|
| 21 |
-
|
| 22 |
-
Formatea la salida como un JSON con la siguiente estructura:
|
| 23 |
-
{{
|
| 24 |
-
"slides": [
|
| 25 |
-
{{"title": "Título de la diapositiva 1", "content": "Contenido de la diapositiva 1"}},
|
| 26 |
-
{{"title": "Título de la diapositiva 2", "content": "Contenido de la diapositiva 2"}},
|
| 27 |
-
...
|
| 28 |
-
]
|
| 29 |
-
}}
|
| 30 |
-
Asegúrate de que la salida sea un JSON válido sin ningún texto adicional antes o después.
|
| 31 |
-
"""
|
| 32 |
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
if json_str:
|
| 38 |
-
slides_data = json.loads(json_str)
|
| 39 |
-
return slides_data['slides']
|
| 40 |
-
else:
|
| 41 |
-
raise ValueError("No se encontró un JSON válido en la respuesta")
|
| 42 |
-
except json.JSONDecodeError as e:
|
| 43 |
-
st.error(f"Error al decodificar JSON: {str(e)}")
|
| 44 |
-
st.text("Respuesta del modelo:")
|
| 45 |
-
st.code(response)
|
| 46 |
-
return None
|
| 47 |
-
except KeyError:
|
| 48 |
-
st.error("El JSON no contiene la clave 'slides'")
|
| 49 |
-
st.text("JSON recibido:")
|
| 50 |
-
st.code(json_str)
|
| 51 |
-
return None
|
| 52 |
-
except ValueError as e:
|
| 53 |
-
st.error(str(e))
|
| 54 |
-
st.text("Respuesta del modelo:")
|
| 55 |
-
st.code(response)
|
| 56 |
-
return None
|
| 57 |
|
| 58 |
def create_powerpoint(slides):
|
| 59 |
prs = Presentation()
|
| 60 |
|
| 61 |
-
for
|
| 62 |
slide = prs.slides.add_slide(prs.slide_layouts[1])
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
title_shape = slide.shapes.title
|
| 65 |
content_shape = slide.placeholders[1]
|
| 66 |
|
| 67 |
-
title_shape.text =
|
| 68 |
-
content_shape.text =
|
| 69 |
|
|
|
|
| 70 |
pptx_buffer = io.BytesIO()
|
| 71 |
prs.save(pptx_buffer)
|
| 72 |
pptx_buffer.seek(0)
|
|
@@ -76,30 +53,28 @@ def create_powerpoint(slides):
|
|
| 76 |
def main():
|
| 77 |
st.title("Generador de presentaciones PowerPoint con IA")
|
| 78 |
|
| 79 |
-
client = get_inference_client()
|
| 80 |
-
|
| 81 |
topic = st.text_input("Por favor, ingrese el tema de la presentación:")
|
| 82 |
|
| 83 |
if st.button("Generar Presentación"):
|
| 84 |
if topic:
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
else:
|
| 104 |
st.warning("Por favor, ingrese un tema para la presentación.")
|
| 105 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
from pptx import Presentation
|
| 5 |
from pptx.util import Inches, Pt
|
| 6 |
+
import os
|
| 7 |
import io
|
| 8 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Initialize the client
|
| 11 |
+
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
|
|
|
| 12 |
|
| 13 |
+
@st.cache_resource
|
| 14 |
+
def load_model():
|
| 15 |
+
model_name = "Mixtral-8x7B-Instruct-v0.1"
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 17 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
|
| 18 |
+
return tokenizer, model
|
| 19 |
|
| 20 |
+
def generate_presentation_content(topic, model, tokenizer):
|
| 21 |
+
prompt = f"Crea una presentación de PowerPoint sobre el tema: {topic}. Incluye 5 diapositivas con títulos y contenido."
|
| 22 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
output = model.generate(input_ids, max_length=500, num_return_sequences=1, temperature=0.7)
|
| 25 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 26 |
|
| 27 |
+
slides = generated_text.split("\n\n")
|
| 28 |
+
return slides[:5] # Limitar a 5 diapositivas
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def create_powerpoint(slides):
|
| 31 |
prs = Presentation()
|
| 32 |
|
| 33 |
+
for slide_content in slides:
|
| 34 |
slide = prs.slides.add_slide(prs.slide_layouts[1])
|
| 35 |
|
| 36 |
+
lines = slide_content.split("\n")
|
| 37 |
+
title = lines[0]
|
| 38 |
+
content = "\n".join(lines[1:])
|
| 39 |
+
|
| 40 |
title_shape = slide.shapes.title
|
| 41 |
content_shape = slide.placeholders[1]
|
| 42 |
|
| 43 |
+
title_shape.text = title
|
| 44 |
+
content_shape.text = content
|
| 45 |
|
| 46 |
+
# Guardar la presentación en un buffer de bytes
|
| 47 |
pptx_buffer = io.BytesIO()
|
| 48 |
prs.save(pptx_buffer)
|
| 49 |
pptx_buffer.seek(0)
|
|
|
|
| 53 |
def main():
|
| 54 |
st.title("Generador de presentaciones PowerPoint con IA")
|
| 55 |
|
|
|
|
|
|
|
| 56 |
topic = st.text_input("Por favor, ingrese el tema de la presentación:")
|
| 57 |
|
| 58 |
if st.button("Generar Presentación"):
|
| 59 |
if topic:
|
| 60 |
+
with st.spinner("Cargando modelo..."):
|
| 61 |
+
tokenizer, model = load_model()
|
| 62 |
+
|
| 63 |
+
with st.spinner("Generando contenido de la presentación..."):
|
| 64 |
+
slides = generate_presentation_content(topic, model, tokenizer)
|
| 65 |
+
|
| 66 |
+
with st.spinner("Creando archivo PowerPoint..."):
|
| 67 |
+
pptx_buffer = create_powerpoint(slides)
|
| 68 |
+
|
| 69 |
+
st.success("Presentación generada con éxito!")
|
| 70 |
+
|
| 71 |
+
# Ofrecer la descarga del archivo
|
| 72 |
+
st.download_button(
|
| 73 |
+
label="Descargar Presentación",
|
| 74 |
+
data=pptx_buffer,
|
| 75 |
+
file_name=f"{topic.replace(' ', '_')}_presentacion.pptx",
|
| 76 |
+
mime="application/vnd.openxmlformats-officedocument.presentationml.presentation"
|
| 77 |
+
)
|
| 78 |
else:
|
| 79 |
st.warning("Por favor, ingrese un tema para la presentación.")
|
| 80 |
|