import gradio as gr from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM import json model_name = "google/flan-t5-small" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForSeq2SeqLM.from_pretrained(model_name) generator = pipeline("text2text-generation", model=model, tokenizer=tokenizer) def generate_json(prompt): # Make the instruction explicit: return ONLY JSON, without explanation instruction = ( f"Generate only a valid JSON object without any markdown or additional text. " f"The JSON object should have the following keys: title, author, and tags. " f"Fill in dummy values. Now create a JSON object for: {prompt}" ) result = generator(instruction, max_length=256, do_sample=False) generated_text = result[0]["generated_text"].strip() # remove extra whitespace print(f"Raw Model Output: {generated_text}") # Debug statement 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)