Allanatrix commited on
Commit
8c52526
Β·
verified Β·
1 Parent(s): a5599f8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ """
3
+ nexa-prompts – Gradio Space to craft JSON instruction / template bundles
4
+ for scientific‑ML prompt engineering.
5
+
6
+ Author: Allan (Nexa Ecosystem) – Apache‑2.0
7
+ """
8
+
9
+ import json, re, tempfile, gradio as gr
10
+
11
+ # ───────────────────────── helper functions ──────────────────────────
12
+ def clean_author_block(tex: str) -> str:
13
+ """Remove any \author{ … } block (including embedded newlines)."""
14
+ return re.sub(r"\\author\{.*?\}", "", tex, flags=re.DOTALL)
15
+
16
+ def swap_topic(text: str, old: str, new: str) -> str:
17
+ pat = re.compile(re.escape(old), flags=re.IGNORECASE)
18
+ return pat.sub(new, text)
19
+
20
+ def build_json(system_instr: str,
21
+ user_template: str,
22
+ topic_swap: str,
23
+ strip_author: bool) -> str:
24
+ if strip_author:
25
+ user_template = clean_author_block(user_template)
26
+
27
+ if topic_swap.strip():
28
+ user_template = swap_topic(user_template, "black holes", topic_swap)
29
+ system_instr = swap_topic(system_instr, "black holes", topic_swap)
30
+
31
+ payload = [
32
+ {"role": "system", "content": system_instr.strip()},
33
+ {"role": "user", "content": user_template.strip()}
34
+ ]
35
+ return json.dumps(payload, indent=2)
36
+
37
+ def to_tmp_file(text: str) -> str:
38
+ """Persist text to a temp .json file so Gradio can serve it."""
39
+ f = tempfile.NamedTemporaryFile(delete=False, suffix=".json", mode="w")
40
+ f.write(text); f.close()
41
+ return f.name
42
+
43
+ # ────────────────────────── Gradio UI ────────────────────────────────
44
+ with gr.Blocks(title="Nexaβ€―Prompts") as demo:
45
+ gr.Markdown(
46
+ """
47
+ # Nexaβ€―Prompts
48
+ Quickly build **role‑based JSON conversations** for SciML agents.
49
+ 1. Edit the **System** and **User** boxes
50
+ 2. (Optional) swap topic or strip the `\\author{}` block
51
+ 3. Click **Generate** β†’ copy or download your JSON
52
+ """
53
+ )
54
+
55
+ with gr.Row():
56
+ sys_box = gr.Textbox(lines=10, label="System Instruction")
57
+ usr_box = gr.Textbox(lines=20, label="User Template (LaTeX, etc.)")
58
+
59
+ with gr.Row():
60
+ topic_box = gr.Textbox(label="Swap topic (optional)", placeholder="e.g. quantum computing")
61
+ strip_chk = gr.Checkbox(label="Strip \\author{...} block", value=True)
62
+
63
+ gen_btn = gr.Button("Generate JSON")
64
+ json_box = gr.Textbox(lines=20, label="Generated JSON", interactive=False)
65
+ dl_btn = gr.DownloadButton(label="⬇️ Download .json")
66
+
67
+ def run(system_instr, user_template, topic_swap, strip_author):
68
+ js = build_json(system_instr, user_template, topic_swap, strip_author)
69
+ path = to_tmp_file(js)
70
+ return js, path
71
+
72
+ gen_btn.click(
73
+ run,
74
+ inputs=[sys_box, usr_box, topic_box, strip_chk],
75
+ outputs=[json_box, dl_btn],
76
+ )
77
+
78
+ if __name__ == "__main__":
79
+ demo.launch()