JairoCesar commited on
Commit
3a1cc37
·
verified ·
1 Parent(s): 9581066

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -11
app.py CHANGED
@@ -10,10 +10,17 @@ import re
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):
@@ -28,26 +35,27 @@ def generate_presentation_content(topic, client):
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))
 
10
  def get_inference_client():
11
  return InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
12
 
13
+ def extract_and_clean_json(text):
14
+ # Intenta encontrar un JSON válido en el texto
15
  json_match = re.search(r'\{.*\}', text, re.DOTALL)
16
  if json_match:
17
+ json_str = json_match.group()
18
+ # Limpia el JSON de caracteres no válidos
19
+ json_str = re.sub(r'[\n\t\r]', '', json_str)
20
+ # Intenta corregir problemas comunes de formato
21
+ json_str = re.sub(r',\s*}', '}', json_str) # Elimina comas finales extras
22
+ json_str = re.sub(r',\s*]', ']', json_str) # Elimina comas finales extras en arrays
23
+ return json_str
24
  return None
25
 
26
  def generate_presentation_content(topic, client):
 
35
  ]
36
  }}
37
  Asegúrate de que la salida sea un JSON válido sin ningún texto adicional antes o después.
38
+ No incluyas saltos de línea o caracteres especiales dentro del JSON.
39
  """
40
 
41
+ response = client.text_generation(prompt, max_new_tokens=1500, temperature=0.7)
42
 
43
  try:
44
+ json_str = extract_and_clean_json(response)
45
  if json_str:
46
  slides_data = json.loads(json_str)
47
+ if 'slides' in slides_data and len(slides_data['slides']) == 5:
48
+ return slides_data['slides']
49
+ else:
50
+ raise ValueError("El JSON no contiene exactamente 5 diapositivas")
51
  else:
52
  raise ValueError("No se encontró un JSON válido en la respuesta")
53
  except json.JSONDecodeError as e:
54
  st.error(f"Error al decodificar JSON: {str(e)}")
55
+ st.text("JSON procesado (después de limpieza):")
 
 
 
 
 
56
  st.code(json_str)
57
+ st.text("Respuesta original del modelo:")
58
+ st.code(response)
59
  return None
60
  except ValueError as e:
61
  st.error(str(e))