-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
205 lines (174 loc) · 6.33 KB
/
main.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from fasthtml.common import *
from pydantic import BaseModel, Field
from enum import Enum
from typing import Optional
from openai import OpenAI
from textwrap import dedent
from dotenv import load_dotenv
load_dotenv()
MODEL = "gpt-4o-2024-08-06"
client = OpenAI()
# App with custom styling to override the pico defaults
css = Style(":root { --pico-font-size: 100%; --pico-font-family: Pacifico, cursive;}")
app = FastHTML(hdrs=(picolink, css))
rt = app.route
class TypeEnum(str, Enum):
text = "text"
number = "number"
date = "date"
radio = "radio"
checkbox = "checkbox"
select = "select"
textarea = "textarea"
class Options(BaseModel):
label: str = Field(description="unique label for the option")
value: str = Field(description="unique value for the option")
class FormField(BaseModel):
label: str = Field(description="Title of the field")
type: TypeEnum = Field(description="Type of the field")
name: str = Field(description="unique name to access the field")
required: bool = Field(description="Whether the field is required")
placeholder: Optional[str] = Field(
description="Placeholder for the field. only applicable for the type text and text area"
)
options: Optional[List[Options]] = Field(
description="Options for the field. only applicable for the type radio, checkbox and select"
)
class DynamicForm(BaseModel):
title: str = Field(description="Title of the form")
fields: List[FormField] = Field(description="List of fields")
form_prompt = ""
dynamic_form_data: DynamicForm = None
def get_form_response(prompt):
system_prompt = """
You are a helpful dynamic html form creator. You will be provided with a dynamic form requirement,
and your goal will be to output form fields.
For each field, just provide the correct configuration.
"""
completion = client.beta.chat.completions.parse(
model=MODEL,
messages=[
{"role": "system", "content": dedent(system_prompt)},
{"role": "user", "content": prompt},
],
response_format=DynamicForm,
)
return completion.choices[0].message
def create_dynamic_form(form_data: DynamicForm | None):
if dynamic_form_data is None:
return None
fields = []
for field in form_data.fields:
field_type = field.type
if field_type in ["text", "number", "date"]:
fields.append(
Div(
Label(field.label, _for=field.name),
Input(
type=field_type,
name=field.name,
placeholder=field.placeholder,
required=field.required,
),
style="margin-bottom: 15px;",
)
)
elif field_type == "select":
options = [
Option(value=opt.value, label=opt.label) for opt in field.options
]
fields.append(
Div(
Label(field.label, _for=field.label),
Select(*options, name=field.name, required=field.required),
style="margin-bottom: 15px;",
)
)
elif field_type == "checkbox":
checkboxes = [
Div(
Input(
type="checkbox",
name=field.name,
value=opt.value,
id=f"{field.name}_{opt.value}",
),
Label(opt.label, _for=f"{field.name}_{opt.value}"),
)
for opt in field.options
]
fields.append(
Div(Label(field.label), Div(*checkboxes), style="margin-bottom: 15px;")
)
elif field_type == "radio":
radios = [
Div(
Input(
type="radio",
name=field.name,
value=opt.value,
id=f"{field.name}_{opt.value}",
required=field.required,
),
Label(opt.label, _for=f"{field.name}_{opt.label}"),
)
for opt in field.options
]
fields.append(
Div(Label(field.label), Div(*radios), style="margin-bottom: 15px;")
)
elif field_type == "textarea":
fields.append(
Div(
Label(field.label, _for=field.name),
Textarea(
name=field.name,
placeholder=field.placeholder,
required=field.required,
),
style="margin-bottom: 15px;",
)
)
form = Form(
*fields,
Button("Submit", type="submit", style="margin-top: 20px;"),
method="post",
action="/submit",
)
return Container(H1(form_data.title), form)
@app.get("/")
def get():
prompt_area = Form(
H2("Enter your prompt here to create a dynamic form"),
Textarea(
name="prompt",
placeholder="Enter your prompt...",
style="width: 100%; margin-bottom: 5px;",
id="form_prompt",
)(form_prompt),
Button(
"Create Form",
type="button", # Ensure it's a button, not submit
id="create_form_button",
style="margin-top: 15px;",
hx_post="/update-prompt",
hx_trigger="click",
hx_target="#form_area",
hx_swap="innerHTML",
onclick="document.getElementById('form_area').innerHTML = 'Loading...';",
),
)
form_area = Div(id="form_area")
return Container(prompt_area, form_area)
@app.post("/update-prompt")
async def update_prompt(request):
global form_prompt, dynamic_form_data
form_data = await request.form()
form_prompt = form_data.get("prompt", form_prompt)
print(f"Updated Prompt: {form_prompt}")
if form_prompt:
dynamic_form_data = get_form_response(form_prompt)
return Div(create_dynamic_form(dynamic_form_data.parsed), id="form_area")
# Fallback in case of issues
return Div("Error generating form. Please try again.", id="form_area")
serve()