Spaces:
Runtime error
Runtime error
File size: 1,253 Bytes
65757ca 4def57d 65757ca 4def57d 65757ca 4def57d 65757ca 4def57d 65757ca 4def57d 65757ca 4def57d 65757ca |
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 |
import gradio as gr
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
import json
# Load lightweight model
model_name = "google/flan-t5-small" # You can change this to another small instruct model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
generator = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
def generate_json(prompt):
instruction = f"Generate a JSON object from the following description:\n{prompt}"
result = generator(instruction, max_length=256, do_sample=False)
generated_text = result[0]["generated_text"]
try:
parsed = eval(generated_text) # Not recommended in production; use json.loads if output is valid JSON
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
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."
).launch()
|