Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,18 @@ 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.
|
|
@@ -21,15 +27,32 @@ def generate_presentation_content(topic, client):
|
|
| 21 |
...
|
| 22 |
]
|
| 23 |
}}
|
|
|
|
| 24 |
"""
|
| 25 |
|
| 26 |
response = client.text_generation(prompt, max_new_tokens=1000, temperature=0.7)
|
| 27 |
|
| 28 |
try:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
return None
|
| 34 |
|
| 35 |
def create_powerpoint(slides):
|
|
|
|
| 4 |
import io
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
import json
|
| 7 |
+
import re
|
| 8 |
|
|
|
|
| 9 |
@st.cache_resource
|
| 10 |
def get_inference_client():
|
| 11 |
return InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 12 |
|
| 13 |
+
def extract_json_from_text(text):
|
| 14 |
+
json_match = re.search(r'\{.*\}', text, re.DOTALL)
|
| 15 |
+
if json_match:
|
| 16 |
+
return json_match.group()
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
def generate_presentation_content(topic, client):
|
| 20 |
prompt = f"""Crea una presentación de PowerPoint sobre el tema: {topic}.
|
| 21 |
Genera exactamente 5 diapositivas. Cada diapositiva debe tener un título y contenido.
|
|
|
|
| 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 |
response = client.text_generation(prompt, max_new_tokens=1000, temperature=0.7)
|
| 34 |
|
| 35 |
try:
|
| 36 |
+
json_str = extract_json_from_text(response)
|
| 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):
|