kevalfst commited on
Commit
52642dd
·
verified ·
1 Parent(s): c7f4c73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -5
app.py CHANGED
@@ -1,8 +1,27 @@
1
  import gradio as gr
 
2
 
3
- # Load the deployed interface
4
- interface = gr.load("spaces/kevalfst/json-generator")
 
 
 
5
 
6
- # Call it with your prompt
7
- output = interface("A blog post with title, author, and tags")
8
- print("Generated JSON:\n", output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
 
4
+ # Load the model
5
+ model_id = "mistralai/Mistral-7B-Instruct-v0.2" # or another instruct-tuned model
6
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
7
+ model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype="auto")
8
+ generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
9
 
10
+ # AI Function
11
+ def generate_json(prompt):
12
+ system_prompt = "You are an AI that generates valid and clean JSON objects based on descriptions."
13
+ input_prompt = f"{system_prompt}\n\nDescription:\n{prompt}\n\nJSON:\n"
14
+ output = generator(input_prompt, max_new_tokens=256, do_sample=False)[0]["generated_text"]
15
+
16
+ # Remove original prompt from generated text
17
+ json_part = output.replace(input_prompt, "").strip()
18
+ return json_part
19
+
20
+ # Gradio UI
21
+ gr.Interface(
22
+ fn=generate_json,
23
+ inputs=gr.Textbox(label="Describe the JSON you want", lines=4),
24
+ outputs=gr.Textbox(label="Generated JSON", lines=20),
25
+ title="🧠 JSON Generator with LLM",
26
+ description="Enter a plain language description and get a structured JSON output."
27
+ ).launch()