lakshmikarpolam commited on
Commit
f8600e7
1 Parent(s): 47c1e5e
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import gradio as gr
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ from jsonformer.format import highlight_values
5
+ from jsonformer.main import Jsonformer
6
+
7
+ print("Loading model and tokenizer...")
8
+ model_name = "databricks/dolly-v2-3b"
9
+ model = AutoModelForCausalLM.from_pretrained(model_name, use_cache=True, device_map="auto")
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True, use_cache=True)
11
+ print("Loaded model and tokenizer")
12
+
13
+ def generate(input_prompt, input_schema):
14
+ try:
15
+ if not input_prompt:
16
+ raise ValueError("Prompt is empty")
17
+ if not input_schema:
18
+ raise ValueError("JSON Schema is empty")
19
+ input_schema = json.loads(input_schema)
20
+ builder = Jsonformer(
21
+ model=model,
22
+ tokenizer=tokenizer,
23
+ json_schema=input_schema,
24
+ prompt=input_prompt,
25
+ )
26
+ print("Generating...")
27
+ output_json = builder()
28
+ return output_json
29
+ except Exception as e:
30
+ raise gr.Error(e)
31
+
32
+ examples = [
33
+ [
34
+ "Generate a json where it is silver Aston Martin DB5 manufactured in 1964",
35
+ '{\n "type": "object",\n "properties": {\n "car": {\n "type": "object",\n "properties": {\n "make": {\n "type": "string"\n },\n "model": {\n "type": "string"\n },\n "year": {\n "type": "number"\n },\n "colors": {\n "type": "array",\n "items": {\n "type": "string"\n }\n }\n }\n }\n }\n}'
36
+ ],
37
+ [
38
+ "Generate a person's information based on the following schema. The person is Lionel Messi, aged 26. Messi is a student at Georgia Tech, and take the following courses: Chemistry, Mathematics, and a minor in Japanese.",
39
+ '{\n "type": "object",\n "properties": {\n "name": {\n "type": "string"\n },\n "age": {\n "type": "number"\n },\n "is_student": {\n "type": "boolean"\n },\n "courses": {\n "type": "array",\n "items": {\n "type": "string"\n }\n }\n }\n}'
40
+ ],
41
+ ]
42
+
43
+ css = """
44
+ #examples {
45
+ width: 35rem;
46
+ }
47
+ """
48
+
49
+ with gr.Blocks(css=css) as demo:
50
+ gr.HTML(
51
+ """
52
+ <div style="text-align: center; margin: 0 auto;">
53
+ <div
54
+ style="
55
+ display: inline-flex;
56
+ align-items: center;
57
+ gap: 0.8rem;
58
+ font-size: 1.75rem;
59
+ "
60
+ >
61
+ <h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
62
+ Jsonformer
63
+ </h1>
64
+ </div>
65
+ <p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
66
+ <a
67
+ href="https://github.com/1rgs/jsonformer"
68
+ style="text-decoration: underline;"
69
+ target="_blank"
70
+ >Jsonformer</a>: A Bulletproof Way to Generate Structured JSON from Language Models.
71
+ </p>
72
+ <p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
73
+ Jsonformer generates <b>syntactically correct</b> jsons by constraining/shrinking output space of Language Models.
74
+ </p>
75
+ </div>
76
+ """
77
+ )
78
+ with gr.Row():
79
+ with gr.Column(scale=1, min_width=600):
80
+ input_prompt = gr.TextArea("Generate a json where it is silver Aston Martin DB5 manufactured in 1964", label="Prompt", lines=2)
81
+ input_schema = gr.Code('{\n "type": "object",\n "properties": {\n "car": {\n "type": "object",\n "properties": {\n "make": {\n "type": "string"\n },\n "model": {\n "type": "string"\n },\n "year": {\n "type": "number"\n },\n "colors": {\n "type": "array",\n "items": {\n "type": "string"\n }\n }\n }\n }\n }\n}', label="JSON Schema")
82
+ generate_btn = gr.Button("Generate")
83
+ with gr.Column(scale=1, min_width=600):
84
+ output_json = gr.JSON(label="Generated JSON")
85
+
86
+ ex = gr.Examples(examples=examples, fn=generate, inputs=[input_prompt, input_schema], outputs=output_json, cache_examples=False, elem_id="examples",)
87
+ ex.dataset.headers = [""]
88
+ generate_btn.click(fn=generate, inputs=[input_prompt, input_schema], outputs=output_json, api_name="greet")
89
+
90
+ demo.launch()