File size: 1,537 Bytes
65757ca
 
 
4def57d
2b9d671
4def57d
 
 
 
65757ca
3c742aa
0c46b11
3c742aa
 
 
 
 
0c46b11
3c742aa
 
0c46b11
3c742aa
 
fb24b48
4def57d
e360b55
65757ca
4def57d
65757ca
0c46b11
65757ca
4def57d
233b266
65757ca
 
 
 
 
233b266
 
67656e6
1510ad8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import gradio as gr
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
import json

model_name = "google/flan-t5-xl"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
generator = pipeline("text2text-generation", model=model, tokenizer=tokenizer)

def generate_json(prompt):
    # Revise the instruction to make it explicit for valid JSON output
    instruction = (
        "Return only a valid JSON object, with no additional text. "
        "The JSON object must contain exactly the keys: "
        '"title", "author", "tags". '
        "For the following prompt, generate this JSON object. "
        f"Prompt: {prompt}"
    )
    result = generator(instruction, max_length=512, do_sample=False)
    generated_text = result[0]["generated_text"].strip()
    
    # Debug: print the raw output to inspect the format
    print(f"Raw Model Output: {generated_text}")
    
    try:
        parsed = json.loads(generated_text)
        formatted_json = json.dumps(parsed, indent=2)
    except Exception as e:
        formatted_json = f"Raw Output:\n{generated_text}\n\nError parsing JSON: {e}"
    
    return formatted_json

demo = gr.Interface(
    fn=generate_json,
    inputs=gr.Textbox(lines=4, label="Enter Prompt"),
    outputs=gr.Textbox(lines=20, label="Generated JSON"),
    title="Lightweight JSON Generator",
    description="Enter a prompt describing the structure or content you want in JSON format."
)

demo.queue()
demo.launch(show_error=True)