Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,14 @@
|
|
| 1 |
"""
|
| 2 |
-
Molecule Maestro – Category Shuffle Edition (Gradio ≥ 4, OpenAI ≥ 1.0)
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
```bash
|
| 9 |
pip install "gradio>=4" "openai>=1.0"
|
| 10 |
-
export OPENAI_API_KEY="sk
|
| 11 |
python molecule_maestro_gradio.py
|
| 12 |
```
|
| 13 |
"""
|
|
@@ -31,14 +32,14 @@ CATEGORIES = [
|
|
| 31 |
|
| 32 |
SYSTEM_PROMPT_PALETTE = (
|
| 33 |
"You are a helpful chemistry assistant. Provide a JSON array named 'palette' with exactly 12 reactants used in school labs. "
|
| 34 |
-
"Each item: {name, category} where category
|
| 35 |
)
|
| 36 |
|
| 37 |
SYSTEM_PROMPT_REACTION = (
|
| 38 |
-
"You are a chemistry mentor. The user
|
| 39 |
"Return STRICT JSON:\n"
|
| 40 |
-
"{ 'reactions': [ { 'equation':'string', 'thermo':'exothermic|endothermic', 'deltaH_kJ':number, 'explanation':'string(≤25
|
| 41 |
-
"Only single‑step
|
| 42 |
)
|
| 43 |
|
| 44 |
|
|
@@ -58,6 +59,7 @@ def gen_palette() -> List[Dict[str, str]]:
|
|
| 58 |
try:
|
| 59 |
return json.loads(raw)["palette"]
|
| 60 |
except Exception:
|
|
|
|
| 61 |
return [
|
| 62 |
{"name": "HCl", "category": "acid"},
|
| 63 |
{"name": "NaOH", "category": "base"},
|
|
@@ -89,6 +91,7 @@ def build_prompt(reactants: List[str], cond: str) -> str:
|
|
| 89 |
def evaluate(reactants: List[str], cond: str) -> str:
|
| 90 |
if not reactants:
|
| 91 |
return "⚠️ Select at least one reactant."
|
|
|
|
| 92 |
raw = chat(build_prompt(reactants, cond), SYSTEM_PROMPT_REACTION, 0.3)
|
| 93 |
try:
|
| 94 |
data = json.loads(raw)
|
|
@@ -116,7 +119,6 @@ with gr.Blocks() as demo:
|
|
| 116 |
cat_groups[cat] = gr.CheckboxGroup(
|
| 117 |
choices=labels_by_cat(cat),
|
| 118 |
label=title,
|
| 119 |
-
show_label=True,
|
| 120 |
)
|
| 121 |
with gr.Column():
|
| 122 |
shuffle_btn = gr.Button("🔀 Reshuffle (50 % new)")
|
|
@@ -127,11 +129,12 @@ with gr.Blocks() as demo:
|
|
| 127 |
|
| 128 |
# ---------- Callbacks ----------
|
| 129 |
def _shuffle():
|
| 130 |
-
"""Keep
|
| 131 |
global palette
|
| 132 |
keep = random.sample(palette, len(palette)//2)
|
| 133 |
kept_names = {i['name'] for i in keep}
|
| 134 |
-
|
|
|
|
| 135 |
attempts = 0
|
| 136 |
while len(fresh) < len(palette)//2 and attempts < 5:
|
| 137 |
batch = gen_palette()
|
|
@@ -141,28 +144,28 @@ with gr.Blocks() as demo:
|
|
| 141 |
if len(fresh) == len(palette)//2:
|
| 142 |
break
|
| 143 |
attempts += 1
|
|
|
|
| 144 |
palette = keep + fresh
|
| 145 |
random.shuffle(palette)
|
| 146 |
-
|
| 147 |
-
updates = []
|
| 148 |
-
|
| 149 |
-
updates.append(gr.update(choices=labels_by_cat(cat), value=[]))
|
| 150 |
-
updates.append("—") # result_md
|
| 151 |
return updates
|
| 152 |
|
| 153 |
-
def _run(*
|
| 154 |
-
|
|
|
|
| 155 |
for lst in checkbox_lists:
|
| 156 |
selected.extend(lst)
|
| 157 |
return evaluate(selected, cond)
|
| 158 |
|
| 159 |
-
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
result_md,
|
| 165 |
-
)
|
| 166 |
|
| 167 |
if __name__ == "__main__":
|
| 168 |
demo.launch()
|
|
|
|
| 1 |
"""
|
| 2 |
+
Molecule Maestro – Category Shuffle Edition (Gradio ≥ 4, OpenAI ≥ 1.0)
|
| 3 |
+
--------------------------------------------------------------------
|
| 4 |
+
* Six category boxes (🔴 Acids, 🔵 Bases, …).
|
| 5 |
+
* **Reshuffle** swaps out 50 % of reactants but keeps the rest.
|
| 6 |
+
* One free‑form **Conditions** textbox.
|
| 7 |
+
* Results come back as a tidy Markdown table.
|
| 8 |
|
| 9 |
```bash
|
| 10 |
pip install "gradio>=4" "openai>=1.0"
|
| 11 |
+
export OPENAI_API_KEY="sk‑…"
|
| 12 |
python molecule_maestro_gradio.py
|
| 13 |
```
|
| 14 |
"""
|
|
|
|
| 32 |
|
| 33 |
SYSTEM_PROMPT_PALETTE = (
|
| 34 |
"You are a helpful chemistry assistant. Provide a JSON array named 'palette' with exactly 12 reactants used in school labs. "
|
| 35 |
+
"Each item: {name, category} where category is one of acid, base, solvent, gas, oxidizer, other."
|
| 36 |
)
|
| 37 |
|
| 38 |
SYSTEM_PROMPT_REACTION = (
|
| 39 |
+
"You are a chemistry mentor. The user gives 1‑5 reactants and an optional conditions string. "
|
| 40 |
"Return STRICT JSON:\n"
|
| 41 |
+
"{ 'reactions': [ { 'equation':'string', 'thermo':'exothermic|endothermic', 'deltaH_kJ':number, 'explanation':'string (≤25 words)' } … ≤5 ] }"\n"
|
| 42 |
+
"Only common, single‑step reactions; empty list if none plausible."
|
| 43 |
)
|
| 44 |
|
| 45 |
|
|
|
|
| 59 |
try:
|
| 60 |
return json.loads(raw)["palette"]
|
| 61 |
except Exception:
|
| 62 |
+
# fallback list
|
| 63 |
return [
|
| 64 |
{"name": "HCl", "category": "acid"},
|
| 65 |
{"name": "NaOH", "category": "base"},
|
|
|
|
| 91 |
def evaluate(reactants: List[str], cond: str) -> str:
|
| 92 |
if not reactants:
|
| 93 |
return "⚠️ Select at least one reactant."
|
| 94 |
+
|
| 95 |
raw = chat(build_prompt(reactants, cond), SYSTEM_PROMPT_REACTION, 0.3)
|
| 96 |
try:
|
| 97 |
data = json.loads(raw)
|
|
|
|
| 119 |
cat_groups[cat] = gr.CheckboxGroup(
|
| 120 |
choices=labels_by_cat(cat),
|
| 121 |
label=title,
|
|
|
|
| 122 |
)
|
| 123 |
with gr.Column():
|
| 124 |
shuffle_btn = gr.Button("🔀 Reshuffle (50 % new)")
|
|
|
|
| 129 |
|
| 130 |
# ---------- Callbacks ----------
|
| 131 |
def _shuffle():
|
| 132 |
+
"""Keep 50 % of current palette, replace the rest, refresh UI."""
|
| 133 |
global palette
|
| 134 |
keep = random.sample(palette, len(palette)//2)
|
| 135 |
kept_names = {i['name'] for i in keep}
|
| 136 |
+
|
| 137 |
+
fresh: List[Dict[str, str]] = []
|
| 138 |
attempts = 0
|
| 139 |
while len(fresh) < len(palette)//2 and attempts < 5:
|
| 140 |
batch = gen_palette()
|
|
|
|
| 144 |
if len(fresh) == len(palette)//2:
|
| 145 |
break
|
| 146 |
attempts += 1
|
| 147 |
+
|
| 148 |
palette = keep + fresh
|
| 149 |
random.shuffle(palette)
|
| 150 |
+
|
| 151 |
+
updates = [gr.update(choices=labels_by_cat(cat), value=[]) for cat, _ in CATEGORIES]
|
| 152 |
+
updates.append("—") # clear result_md
|
|
|
|
|
|
|
| 153 |
return updates
|
| 154 |
|
| 155 |
+
def _run(*inputs):
|
| 156 |
+
*checkbox_lists, cond = inputs
|
| 157 |
+
selected: List[str] = []
|
| 158 |
for lst in checkbox_lists:
|
| 159 |
selected.extend(lst)
|
| 160 |
return evaluate(selected, cond)
|
| 161 |
|
| 162 |
+
# Reshuffle outputs: one per category checkbox + result_md
|
| 163 |
+
shuffle_outputs = [cat_groups[c] for c, _ in CATEGORIES] + [result_md]
|
| 164 |
+
shuffle_btn.click(_shuffle, None, shuffle_outputs)
|
| 165 |
|
| 166 |
+
# Run inputs: all checkbox groups + conditions textbox
|
| 167 |
+
run_inputs = [cat_groups[c] for c, _ in CATEGORIES] + [conditions_tb]
|
| 168 |
+
run_btn.click(_run, run_inputs, result_md)
|
|
|
|
|
|
|
| 169 |
|
| 170 |
if __name__ == "__main__":
|
| 171 |
demo.launch()
|