Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,49 @@
|
|
| 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
|
| 11 |
-
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 12 |
-
|
| 13 |
@st.cache_resource
|
| 14 |
-
def
|
| 15 |
-
|
| 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,
|
| 21 |
-
prompt = f"Crea una presentación de PowerPoint sobre el tema: {topic}.
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def create_powerpoint(slides):
|
| 31 |
prs = Presentation()
|
| 32 |
|
| 33 |
-
for
|
| 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,28 +53,30 @@ def create_powerpoint(slides):
|
|
| 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 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
else:
|
| 79 |
st.warning("Por favor, ingrese un tema para la presentación.")
|
| 80 |
|
|
|
|
| 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 |
|
| 8 |
+
# Initialize the Hugging Face Inference Client
|
|
|
|
|
|
|
| 9 |
@st.cache_resource
|
| 10 |
+
def get_inference_client():
|
| 11 |
+
return InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
def generate_presentation_content(topic, client):
|
| 14 |
+
prompt = f"""Crea una presentación de PowerPoint sobre el tema: {topic}.
|
| 15 |
+
Genera exactamente 5 diapositivas. Cada diapositiva debe tener un título y contenido.
|
| 16 |
+
Formatea la salida como un JSON con la siguiente estructura:
|
| 17 |
+
{{
|
| 18 |
+
"slides": [
|
| 19 |
+
{{"title": "Título de la diapositiva 1", "content": "Contenido de la diapositiva 1"}},
|
| 20 |
+
{{"title": "Título de la diapositiva 2", "content": "Contenido de la diapositiva 2"}},
|
| 21 |
+
...
|
| 22 |
+
]
|
| 23 |
+
}}
|
| 24 |
+
"""
|
| 25 |
|
| 26 |
+
response = client.text_generation(prompt, max_new_tokens=1000, temperature=0.7)
|
|
|
|
| 27 |
|
| 28 |
+
try:
|
| 29 |
+
slides_data = json.loads(response)
|
| 30 |
+
return slides_data['slides']
|
| 31 |
+
except json.JSONDecodeError:
|
| 32 |
+
st.error("Error al procesar la respuesta del modelo. Por favor, intente nuevamente.")
|
| 33 |
+
return None
|
| 34 |
|
| 35 |
def create_powerpoint(slides):
|
| 36 |
prs = Presentation()
|
| 37 |
|
| 38 |
+
for slide_data in slides:
|
| 39 |
slide = prs.slides.add_slide(prs.slide_layouts[1])
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
title_shape = slide.shapes.title
|
| 42 |
content_shape = slide.placeholders[1]
|
| 43 |
|
| 44 |
+
title_shape.text = slide_data['title']
|
| 45 |
+
content_shape.text = slide_data['content']
|
| 46 |
|
|
|
|
| 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 |
+
client = get_inference_client()
|
| 57 |
+
|
| 58 |
topic = st.text_input("Por favor, ingrese el tema de la presentación:")
|
| 59 |
|
| 60 |
if st.button("Generar Presentación"):
|
| 61 |
if topic:
|
| 62 |
+
try:
|
| 63 |
+
with st.spinner("Generando contenido de la presentación..."):
|
| 64 |
+
slides = generate_presentation_content(topic, client)
|
| 65 |
+
|
| 66 |
+
if slides:
|
| 67 |
+
with st.spinner("Creando archivo PowerPoint..."):
|
| 68 |
+
pptx_buffer = create_powerpoint(slides)
|
| 69 |
+
|
| 70 |
+
st.success("Presentación generada con éxito!")
|
| 71 |
+
|
| 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 |
+
except Exception as e:
|
| 79 |
+
st.error(f"Ocurrió un error al generar la presentación: {str(e)}")
|
| 80 |
else:
|
| 81 |
st.warning("Por favor, ingrese un tema para la presentación.")
|
| 82 |
|