jcmachicao commited on
Commit
cfad9c5
verified
1 Parent(s): 58b3d32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
  # Define the JSON structure
5
  predefined_json = {
@@ -19,17 +20,18 @@ model = pipeline("text-generation", model="tiiuae/falcon-7b-instruct", max_lengt
19
 
20
  def convert_text_to_json(input_text):
21
  # Generate JSON using the LLM
22
- prompt = f"Extract the following fields into JSON format: {list(predefined_json.keys())}\nInput: {input_text}"
23
  response = model(prompt)[0]["generated_text"]
24
 
25
- # Attempt to parse the response into JSON
26
  try:
27
- generated_json = eval(response) # Unsafe for prod, use `json.loads()` for proper JSON
28
- missing_fields = [key for key in predefined_json if not generated_json.get(key)]
29
- missing_message = f"Missing fields: {', '.join(missing_fields)}" if missing_fields else "All fields are complete."
30
- except Exception as e:
31
- generated_json = {}
32
- missing_message = f"Error parsing JSON: {e}"
 
33
 
34
  return generated_json, missing_message
35
 
@@ -38,8 +40,8 @@ interface = gr.Interface(
38
  fn=convert_text_to_json,
39
  inputs="text",
40
  outputs=["json", "text"],
41
- title="Text to JSON Converter",
42
- description="Enter text to extract information into predefined JSON format."
43
  )
44
 
45
- interface.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import json
4
 
5
  # Define the JSON structure
6
  predefined_json = {
 
20
 
21
  def convert_text_to_json(input_text):
22
  # Generate JSON using the LLM
23
+ prompt = f"Extrae los campos incluidos en el siguiente formato JSON: {list(predefined_json.keys())}\nInput: {input_text}"
24
  response = model(prompt)[0]["generated_text"]
25
 
26
+ # Attempt to parse the response into a JSON object
27
  try:
28
+ generated_json = json.loads(response) # Safer than eval()
29
+ except json.JSONDecodeError:
30
+ return {}, "Error: El modelo no retorn贸 un formato JSON v谩lido."
31
+
32
+ # Check for missing fields
33
+ missing_fields = [key for key in predefined_json if key not in generated_json or not generated_json[key]]
34
+ missing_message = f"Campos faltantes: {', '.join(missing_fields)}" if missing_fields else "Todos los campos est谩n completos."
35
 
36
  return generated_json, missing_message
37
 
 
40
  fn=convert_text_to_json,
41
  inputs="text",
42
  outputs=["json", "text"],
43
+ title="Convertidor Texto en JSON",
44
+ description="Ingrese el texto para extraer informaci贸n en un formato JSON predefinido."
45
  )
46
 
47
+ interface.launch()