Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -150,4 +150,67 @@ def pretty_markdown(data: dict) -> str:
|
|
| 150 |
|
| 151 |
## Extracted Fields
|
| 152 |
```json
|
| 153 |
-
{json.dumps(extracted, indent=2)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
## Extracted Fields
|
| 152 |
```json
|
| 153 |
+
{json.dumps(extracted, indent=2)}
|
| 154 |
+
|
| 155 |
+
## Clean Schema
|
| 156 |
+
{json.dumps(schema, indent=2)}
|
| 157 |
+
|
| 158 |
+
Model: {meta.get("model", "")}
|
| 159 |
+
Request ID: {meta.get("request_id", "")}
|
| 160 |
+
Latency: {meta.get("elapsed_seconds", "")}s
|
| 161 |
+
""".strip()
|
| 162 |
+
|
| 163 |
+
def run_extraction(user_text: str):
|
| 164 |
+
if not user_text or not user_text.strip():
|
| 165 |
+
raise gr.Error("Please paste some natural language input.")
|
| 166 |
+
|
| 167 |
+
result = extract_json(user_text.strip())
|
| 168 |
+
return pretty_markdown(result), json.dumps(result, indent=2)
|
| 169 |
+
|
| 170 |
+
EXAMPLES = [
|
| 171 |
+
[
|
| 172 |
+
"Need a birthday gift for my wife, budget maybe 80 to 150, she likes elegant things not overly flashy, probably jewellery or perfume, need it by next Friday, preferably something easy to return."
|
| 173 |
+
],
|
| 174 |
+
[
|
| 175 |
+
"Set up a workflow that watches a shared inbox, spots urgent customer complaints, checks CRM details, drafts a reply, and sends the risky ones to a manager before anything goes out."
|
| 176 |
+
],
|
| 177 |
+
[
|
| 178 |
+
"I'm planning a Scotland trip in May for 5 days, two adults, want scenic places, not too much driving, budget around 1200 excluding flights, prefer cosy stays and at least one castle."
|
| 179 |
+
],
|
| 180 |
+
[
|
| 181 |
+
"Need to hire a Python automation engineer in Glasgow, ideally 5+ years, strong APIs and RPA, salary flexible, hybrid preferred, must be good with stakeholders, start within 2 months."
|
| 182 |
+
],
|
| 183 |
+
]
|
| 184 |
+
|
| 185 |
+
with gr.Blocks(title="Prompt → JSON Extractor") as demo:
|
| 186 |
+
gr.Markdown("# Prompt → JSON Extractor")
|
| 187 |
+
gr.Markdown(
|
| 188 |
+
"Paste messy natural language and turn it into clean, structured JSON."
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
input_text = gr.Textbox(
|
| 192 |
+
label="Messy Natural Language Input",
|
| 193 |
+
placeholder="Paste raw instructions, requirements, notes, or a messy request here...",
|
| 194 |
+
lines=10,
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
run_btn = gr.Button("Extract Structured JSON")
|
| 198 |
+
|
| 199 |
+
output_md = gr.Markdown()
|
| 200 |
+
output_json = gr.Code(label="JSON Output", language="json")
|
| 201 |
+
|
| 202 |
+
gr.Examples(
|
| 203 |
+
examples=EXAMPLES,
|
| 204 |
+
inputs=[input_text],
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
run_btn.click(
|
| 208 |
+
fn=run_extraction,
|
| 209 |
+
inputs=[input_text],
|
| 210 |
+
outputs=[output_md, output_json],
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
if name == "main":
|
| 214 |
+
demo.launch(ssr_mode=False)
|
| 215 |
+
|
| 216 |
+
|