Update app.py
Browse files
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"
|
| 23 |
response = model(prompt)[0]["generated_text"]
|
| 24 |
|
| 25 |
-
# Attempt to parse the response into JSON
|
| 26 |
try:
|
| 27 |
-
generated_json =
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 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="
|
| 42 |
-
description="
|
| 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()
|