Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,15 @@
|
|
| 1 |
import json
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
import torch
|
| 6 |
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 7 |
|
|
|
|
|
|
|
| 8 |
from prepare_messages import (
|
| 9 |
qdl_prepare_message,
|
| 10 |
mca_prepare_messages,
|
|
@@ -61,6 +66,139 @@ ENGINE_TEXT = {
|
|
| 61 |
"ase": "Atomic configurations, force and potential-energy optimisation.",
|
| 62 |
}
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
# ZeroGPU recommends loading models on CUDA at module startup. Its CUDA
|
| 65 |
# emulation makes this possible even before a real GPU is assigned.
|
| 66 |
# QDL is quantised to 4-bit; both small Qwen models use float16.
|
|
@@ -172,42 +310,66 @@ def make_constraints(engine, particle_kind, spin, sites_min, sites_max,
|
|
| 172 |
"approved_calculators": [calculator]}
|
| 173 |
|
| 174 |
|
| 175 |
-
@spaces.GPU(duration=
|
| 176 |
-
def
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
raise gr.Error("Reward scale must be positive")
|
| 187 |
-
constraints = make_constraints(
|
| 188 |
-
engine, particle_kind, spin, sites_min, sites_max,
|
| 189 |
-
eigenpairs_min, dimension, orbitals, shape_max,
|
| 190 |
-
qubits, init_bits, end_time_min, end_time_max, time_points_min,
|
| 191 |
-
atom_min, atom_max, calculator,
|
| 192 |
)
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
| 205 |
try:
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
@spaces.GPU(duration=120)
|
| 213 |
def coder_respond(message, history):
|
|
@@ -217,121 +379,394 @@ def coder_respond(message, history):
|
|
| 217 |
def sql_respond(message, history):
|
| 218 |
return infer("text-to-sql", text2sql_prepare_messages(message, history))
|
| 219 |
|
| 220 |
-
with gr.Blocks(title="AI
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
with gr.Row():
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
)
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
with gr.Column(visible=False) as kwant_fields:
|
| 255 |
-
dimension = gr.Dropdown([(str(i), i) for i in (1, 2, 3)], value=2, label="Lattice dimensions")
|
| 256 |
-
orbitals = gr.Slider(1, 4, value=1, step=1, label="Orbitals per site")
|
| 257 |
-
shape_max = gr.Textbox(value="8,8", label="Maximum shape per dimension (comma-separated)")
|
| 258 |
-
with gr.Column(visible=True) as qutip_fields:
|
| 259 |
-
qubits = gr.Slider(2, 8, value=4, step=1, label="Qubit count")
|
| 260 |
-
init_bits = gr.Textbox(value="0,0,0,0", label="Initial qubit state (comma-separated)")
|
| 261 |
-
with gr.Row():
|
| 262 |
-
end_time_min = gr.Number(value=2, label="Minimum end time")
|
| 263 |
-
end_time_max = gr.Number(value=10, label="Maximum end time")
|
| 264 |
-
time_points_min = gr.Slider(2, 1000, value=100, step=1, label="Minimum time points")
|
| 265 |
-
with gr.Column(visible=False) as ase_fields:
|
| 266 |
-
with gr.Row():
|
| 267 |
-
atom_min = gr.Slider(2, 30, value=2, step=1, label="Minimum atoms")
|
| 268 |
-
atom_max = gr.Slider(2, 30, value=6, step=1, label="Maximum atoms")
|
| 269 |
-
calculator = gr.Dropdown(
|
| 270 |
-
[("Effective Medium Theory (EMT)", "emt"),
|
| 271 |
-
("Lennard–Jones", "lennard_jones")],
|
| 272 |
-
value="emt", label="Calculator",
|
| 273 |
)
|
| 274 |
|
| 275 |
engine.change(
|
| 276 |
-
fn=
|
| 277 |
-
inputs=
|
| 278 |
-
outputs=[
|
| 279 |
-
|
| 280 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
metric.change(
|
| 283 |
-
fn=
|
| 284 |
inputs=[engine, metric],
|
| 285 |
-
outputs=[direction, target, scale]
|
| 286 |
-
queue=False,
|
| 287 |
-
)
|
| 288 |
-
# Keep state bit input aligned with slider for an easy default experience.
|
| 289 |
-
qubits.change(
|
| 290 |
-
lambda count: ",".join(["0"] * int(count)),
|
| 291 |
-
inputs=[qubits], outputs=[init_bits], queue=False,
|
| 292 |
)
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
)
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
)
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
outputs=[generated, submitted],
|
| 312 |
)
|
| 313 |
-
gr.Markdown("**Note:** This demo generates candidates. It does not run the physics evaluator.")
|
| 314 |
|
| 315 |
-
with gr.Column(visible=False) as coder_page:
|
| 316 |
-
gr.Markdown("# Mini Code Assistant\n**Qwen3-1.7B · Fine-tuned**")
|
| 317 |
gr.ChatInterface(
|
| 318 |
fn=coder_respond,
|
| 319 |
-
examples=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
)
|
| 321 |
-
|
| 322 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
gr.ChatInterface(
|
| 324 |
-
fn=
|
| 325 |
-
examples=[
|
| 326 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
)
|
| 328 |
-
with gr.Column(visible=False) as unknown_page:
|
| 329 |
-
gr.Markdown("## Unknown model\nOpen this demo from its portfolio project page.")
|
| 330 |
|
| 331 |
-
|
| 332 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
demo.queue(default_concurrency_limit=1)
|
| 335 |
|
| 336 |
if __name__ == "__main__":
|
| 337 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
+
import math
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import spaces
|
| 8 |
import torch
|
| 9 |
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 10 |
|
| 11 |
+
from evaluator import evaluate
|
| 12 |
+
|
| 13 |
from prepare_messages import (
|
| 14 |
qdl_prepare_message,
|
| 15 |
mca_prepare_messages,
|
|
|
|
| 66 |
"ase": "Atomic configurations, force and potential-energy optimisation.",
|
| 67 |
}
|
| 68 |
|
| 69 |
+
def extract_json_block(text: str):
|
| 70 |
+
text = text.strip()
|
| 71 |
+
|
| 72 |
+
if text.startswith("```"):
|
| 73 |
+
parts = text.split("```")
|
| 74 |
+
for part in parts:
|
| 75 |
+
part = part.strip()
|
| 76 |
+
if part.startswith("json"):
|
| 77 |
+
part = part[4:].strip()
|
| 78 |
+
if part.startswith("{") and part.endswith("}"):
|
| 79 |
+
return json.loads(part)
|
| 80 |
+
|
| 81 |
+
if text.startswith("{") and text.endswith("}"):
|
| 82 |
+
return json.loads(text)
|
| 83 |
+
|
| 84 |
+
raise ValueError("Expected a JSON object or fenced JSON object.")
|
| 85 |
+
|
| 86 |
+
def visualize_qutip(system: dict):
|
| 87 |
+
dims = system.get("dims", [])
|
| 88 |
+
n = len(dims)
|
| 89 |
+
|
| 90 |
+
if n == 0:
|
| 91 |
+
raise ValueError("No dims found in qutip system.")
|
| 92 |
+
|
| 93 |
+
angles = np.linspace(0, 2 * np.pi, n, endpoint=False)
|
| 94 |
+
xs = np.cos(angles)
|
| 95 |
+
ys = np.sin(angles)
|
| 96 |
+
|
| 97 |
+
fig, ax = plt.subplots(figsize=(6, 6))
|
| 98 |
+
|
| 99 |
+
color_map = {"x": "tab:blue", "y": "tab:orange", "z": "tab:green"}
|
| 100 |
+
|
| 101 |
+
for term in system.get("hamiltonian", []):
|
| 102 |
+
factors = term.get("factors", [])
|
| 103 |
+
if len(factors) == 2:
|
| 104 |
+
a = factors[0]["site"]
|
| 105 |
+
b = factors[1]["site"]
|
| 106 |
+
op = factors[0]["op"]
|
| 107 |
+
coeff = abs(float(term.get("coefficient", 1.0)))
|
| 108 |
+
|
| 109 |
+
ax.plot(
|
| 110 |
+
[xs[a], xs[b]],
|
| 111 |
+
[ys[a], ys[b]],
|
| 112 |
+
linewidth=1 + coeff,
|
| 113 |
+
alpha=0.7,
|
| 114 |
+
color=color_map.get(op, "gray")
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
for i in range(n):
|
| 118 |
+
ax.scatter(xs[i], ys[i], s=700, zorder=3)
|
| 119 |
+
ax.text(xs[i], ys[i], str(i), ha="center", va="center", color="white", fontsize=12)
|
| 120 |
+
|
| 121 |
+
ax.set_title("QuTiP Quantum Interaction Graph")
|
| 122 |
+
ax.set_aspect("equal")
|
| 123 |
+
ax.axis("off")
|
| 124 |
+
return fig
|
| 125 |
+
|
| 126 |
+
def visualize_quspin(system: dict):
|
| 127 |
+
sites = int(system.get("sites", 0))
|
| 128 |
+
if sites <= 0:
|
| 129 |
+
raise ValueError("No valid 'sites' found in quspin system.")
|
| 130 |
+
|
| 131 |
+
xs = np.arange(sites)
|
| 132 |
+
ys = np.zeros(sites)
|
| 133 |
+
|
| 134 |
+
fig, ax = plt.subplots(figsize=(8, 3))
|
| 135 |
+
|
| 136 |
+
for i in range(sites - 1):
|
| 137 |
+
ax.plot([xs[i], xs[i+1]], [ys[i], ys[i+1]], linewidth=2)
|
| 138 |
+
|
| 139 |
+
for term in system.get("terms", []):
|
| 140 |
+
op = term.get("op", "")
|
| 141 |
+
for c in term.get("couplings", []):
|
| 142 |
+
if len(c) >= 3:
|
| 143 |
+
try:
|
| 144 |
+
i = int(c[1])
|
| 145 |
+
j = int(c[2])
|
| 146 |
+
if abs(i - j) > 1:
|
| 147 |
+
ax.plot([xs[i], xs[j]], [ys[i], ys[j]], linestyle="--", alpha=0.5)
|
| 148 |
+
except:
|
| 149 |
+
pass
|
| 150 |
+
|
| 151 |
+
ax.scatter(xs, ys, s=500, zorder=3)
|
| 152 |
+
for i in range(sites):
|
| 153 |
+
ax.text(xs[i], ys[i], str(i), ha="center", va="center", color="white", fontsize=11)
|
| 154 |
+
|
| 155 |
+
ax.set_title("QuSpin Lattice Structure")
|
| 156 |
+
ax.axis("off")
|
| 157 |
+
return fig
|
| 158 |
+
|
| 159 |
+
def visualize_ase(system: dict):
|
| 160 |
+
symbols = system.get("symbols", [])
|
| 161 |
+
positions = system.get("positions", [])
|
| 162 |
+
|
| 163 |
+
if not symbols or not positions:
|
| 164 |
+
raise ValueError("ASE system needs symbols and positions.")
|
| 165 |
+
|
| 166 |
+
pos = np.array(positions, dtype=float)
|
| 167 |
+
|
| 168 |
+
fig = plt.figure(figsize=(7, 6))
|
| 169 |
+
ax = fig.add_subplot(111, projection="3d")
|
| 170 |
+
|
| 171 |
+
ax.scatter(pos[:, 0], pos[:, 1], pos[:, 2], s=300)
|
| 172 |
+
|
| 173 |
+
for i, (x, y, z) in enumerate(pos):
|
| 174 |
+
ax.text(x, y, z, symbols[i], fontsize=10)
|
| 175 |
+
|
| 176 |
+
ax.set_title("ASE Atomic Structure")
|
| 177 |
+
ax.set_xlabel("X")
|
| 178 |
+
ax.set_ylabel("Y")
|
| 179 |
+
ax.set_zlabel("Z")
|
| 180 |
+
return fig
|
| 181 |
+
|
| 182 |
+
def visualize_system(engine: str, system: dict):
|
| 183 |
+
if engine == "qutip":
|
| 184 |
+
return visualize_qutip(system)
|
| 185 |
+
elif engine == "quspin":
|
| 186 |
+
return visualize_quspin(system)
|
| 187 |
+
elif engine == "ase":
|
| 188 |
+
return visualize_ase(system)
|
| 189 |
+
else:
|
| 190 |
+
raise ValueError(f"Visualization not supported for engine: {engine}")
|
| 191 |
+
|
| 192 |
+
def run_simulation(engine: str, system: dict, objective: dict):
|
| 193 |
+
candidate = {
|
| 194 |
+
"engine": engine,
|
| 195 |
+
"system": system,
|
| 196 |
+
"objective": objective,
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
result = evaluate(candidate, detailed=True)
|
| 200 |
+
return result
|
| 201 |
+
|
| 202 |
# ZeroGPU recommends loading models on CUDA at module startup. Its CUDA
|
| 203 |
# emulation makes this possible even before a real GPU is assigned.
|
| 204 |
# QDL is quantised to 4-bit; both small Qwen models use float16.
|
|
|
|
| 310 |
"approved_calculators": [calculator]}
|
| 311 |
|
| 312 |
|
| 313 |
+
@spaces.GPU(duration=90)
|
| 314 |
+
def quantum_respond(goal, question, engine, constraints, objective):
|
| 315 |
+
if engine == "kwant":
|
| 316 |
+
raise gr.Error("Kwant visualization/simulation is disabled for now.")
|
| 317 |
+
|
| 318 |
+
messages = qdl_prepare_message(
|
| 319 |
+
user_goal=goal,
|
| 320 |
+
research_question=question,
|
| 321 |
+
design_constraints=constraints,
|
| 322 |
+
objectives=objective,
|
| 323 |
+
engine=engine
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
)
|
| 325 |
+
|
| 326 |
+
generated_text = generate(
|
| 327 |
+
messages,
|
| 328 |
+
quantum_model,
|
| 329 |
+
quantum_tokenizer,
|
| 330 |
+
max_new_tokens=1536
|
| 331 |
+
)
|
| 332 |
+
|
| 333 |
+
parsed = None
|
| 334 |
+
fig = None
|
| 335 |
+
sim_status = "not_run"
|
| 336 |
+
metrics = {}
|
| 337 |
+
objective_info = {}
|
| 338 |
+
|
| 339 |
try:
|
| 340 |
+
parsed = extract_json_block(generated_text)
|
| 341 |
+
|
| 342 |
+
if "system" not in parsed or not isinstance(parsed["system"], dict):
|
| 343 |
+
raise ValueError("Generated JSON must contain a top-level 'system' object.")
|
| 344 |
+
|
| 345 |
+
system = parsed["system"]
|
| 346 |
+
|
| 347 |
+
fig = visualize_system(engine, system)
|
| 348 |
+
sim = run_simulation(engine, system, objective)
|
| 349 |
+
|
| 350 |
+
sim_status = sim.get("status", "unknown")
|
| 351 |
+
|
| 352 |
+
if sim.get("experiments"):
|
| 353 |
+
exp = sim["experiments"][0]
|
| 354 |
+
metrics = exp.get("metrics", {})
|
| 355 |
+
objective_info = exp.get("objective_result", {})
|
| 356 |
+
else:
|
| 357 |
+
metrics = {}
|
| 358 |
+
objective_info = {"reward": sim.get("reward")}
|
| 359 |
+
|
| 360 |
+
except Exception as e:
|
| 361 |
+
sim_status = f"error: {str(e)}"
|
| 362 |
+
metrics = {}
|
| 363 |
+
objective_info = {}
|
| 364 |
+
|
| 365 |
+
return (
|
| 366 |
+
generated_text,
|
| 367 |
+
parsed if parsed else {},
|
| 368 |
+
fig,
|
| 369 |
+
sim_status,
|
| 370 |
+
metrics,
|
| 371 |
+
objective_info,
|
| 372 |
+
)
|
| 373 |
|
| 374 |
@spaces.GPU(duration=120)
|
| 375 |
def coder_respond(message, history):
|
|
|
|
| 379 |
def sql_respond(message, history):
|
| 380 |
return infer("text-to-sql", text2sql_prepare_messages(message, history))
|
| 381 |
|
| 382 |
+
with gr.Blocks(title="AI Research & Development") as demo:
|
| 383 |
+
|
| 384 |
+
# ==========================
|
| 385 |
+
# QUANTUM DISCOVERY LAB
|
| 386 |
+
# ==========================
|
| 387 |
+
|
| 388 |
+
with gr.Column(visible=False) as quantum_section:
|
| 389 |
+
|
| 390 |
+
gr.Markdown("# Quantum Discovery Lab")
|
| 391 |
+
gr.Markdown(
|
| 392 |
+
"Generate and simulate physically testable systems "
|
| 393 |
+
"using a GRPO fine-tuned Phi-4 14B model."
|
| 394 |
+
)
|
| 395 |
+
|
| 396 |
with gr.Row():
|
| 397 |
+
|
| 398 |
+
# INPUTS
|
| 399 |
+
with gr.Column(scale=1):
|
| 400 |
+
|
| 401 |
+
gr.Markdown("### Research Configuration")
|
| 402 |
+
|
| 403 |
+
engine = gr.Dropdown(
|
| 404 |
+
choices=[
|
| 405 |
+
("QuSpin — Many-body physics", "quspin"),
|
| 406 |
+
("QuTiP — Quantum dynamics", "qutip"),
|
| 407 |
+
("ASE — Atomic simulation", "ase"),
|
| 408 |
+
("Kwant — Coming soon", "kwant"),
|
| 409 |
+
],
|
| 410 |
+
value="qutip",
|
| 411 |
+
label="Simulation Engine",
|
| 412 |
+
interactive=True
|
| 413 |
+
)
|
| 414 |
+
|
| 415 |
+
goal = gr.Textbox(
|
| 416 |
+
label="Research Goal",
|
| 417 |
+
lines=3,
|
| 418 |
+
placeholder=(
|
| 419 |
+
"Design an interacting quantum system "
|
| 420 |
+
"that exhibits strong multipartite entanglement."
|
| 421 |
+
)
|
| 422 |
+
)
|
| 423 |
+
|
| 424 |
+
question = gr.Textbox(
|
| 425 |
+
label="Research Question",
|
| 426 |
+
lines=3,
|
| 427 |
+
placeholder=(
|
| 428 |
+
"Can asymmetric spin interactions produce "
|
| 429 |
+
"nontrivial quantum correlations?"
|
| 430 |
+
)
|
| 431 |
+
)
|
| 432 |
+
|
| 433 |
+
gr.Markdown("### Evaluation Objective")
|
| 434 |
+
|
| 435 |
+
# ENGINE_METRICS:
|
| 436 |
+
# Önceki app.py dosyasında tanımlı olmalı.
|
| 437 |
+
metric = gr.Dropdown(
|
| 438 |
+
choices=ENGINE_METRICS["qutip"],
|
| 439 |
+
label="Metric",
|
| 440 |
+
interactive=True
|
| 441 |
+
)
|
| 442 |
+
|
| 443 |
+
direction = gr.Radio(
|
| 444 |
+
choices=[
|
| 445 |
+
("Maximise", "maximize"),
|
| 446 |
+
("Minimise", "minimize"),
|
| 447 |
+
("Match target", "match"),
|
| 448 |
+
],
|
| 449 |
+
value="maximize",
|
| 450 |
+
label="Optimisation Direction"
|
| 451 |
+
)
|
| 452 |
+
|
| 453 |
+
with gr.Row():
|
| 454 |
+
target = gr.Number(
|
| 455 |
+
value=0.95,
|
| 456 |
+
label="Target"
|
| 457 |
+
)
|
| 458 |
+
|
| 459 |
+
scale = gr.Number(
|
| 460 |
+
value=0.1,
|
| 461 |
+
minimum=0.000001,
|
| 462 |
+
label="Reward Scale"
|
| 463 |
+
)
|
| 464 |
+
|
| 465 |
+
gr.Markdown("### Design Constraints")
|
| 466 |
+
|
| 467 |
+
constraints = gr.JSON(
|
| 468 |
+
label="Constraints",
|
| 469 |
+
value={}
|
| 470 |
+
)
|
| 471 |
+
|
| 472 |
+
with gr.Row():
|
| 473 |
+
|
| 474 |
+
quantum_button = gr.Button(
|
| 475 |
+
"Generate & Simulate",
|
| 476 |
+
variant="primary",
|
| 477 |
+
scale=3
|
| 478 |
+
)
|
| 479 |
+
|
| 480 |
+
clear_button = gr.Button(
|
| 481 |
+
"Clear Results",
|
| 482 |
+
scale=1
|
| 483 |
+
)
|
| 484 |
+
|
| 485 |
+
# OUTPUTS
|
| 486 |
+
with gr.Column(scale=1):
|
| 487 |
+
|
| 488 |
+
gr.Markdown("### Scientific Results")
|
| 489 |
+
|
| 490 |
+
simulation_status = gr.Textbox(
|
| 491 |
+
label="Simulation Status",
|
| 492 |
+
interactive=False
|
| 493 |
+
)
|
| 494 |
+
|
| 495 |
+
with gr.Tabs():
|
| 496 |
+
|
| 497 |
+
with gr.Tab("Visualization"):
|
| 498 |
+
|
| 499 |
+
quantum_plot = gr.Plot(
|
| 500 |
+
label="System Visualization"
|
| 501 |
+
)
|
| 502 |
+
|
| 503 |
+
with gr.Tab("Simulation Metrics"):
|
| 504 |
+
|
| 505 |
+
simulation_metrics = gr.JSON(
|
| 506 |
+
label="Measured Physics Metrics"
|
| 507 |
+
)
|
| 508 |
+
|
| 509 |
+
objective_result = gr.JSON(
|
| 510 |
+
label="Evaluation Result"
|
| 511 |
+
)
|
| 512 |
+
|
| 513 |
+
with gr.Tab("Generated System"):
|
| 514 |
+
|
| 515 |
+
parsed_output = gr.JSON(
|
| 516 |
+
label="Parsed Candidate"
|
| 517 |
+
)
|
| 518 |
+
|
| 519 |
+
quantum_output = gr.Code(
|
| 520 |
+
label="Raw Model Output",
|
| 521 |
+
language="json",
|
| 522 |
+
lines=20,
|
| 523 |
+
interactive=False
|
| 524 |
+
)
|
| 525 |
+
|
| 526 |
+
gr.Markdown(
|
| 527 |
+
"Simulation results are obtained from numerical physics "
|
| 528 |
+
"engines and do not establish experimental verification "
|
| 529 |
+
"or scientific novelty."
|
| 530 |
+
)
|
| 531 |
+
|
| 532 |
+
# --------------------------
|
| 533 |
+
# ENGINE EVENTS
|
| 534 |
+
# --------------------------
|
| 535 |
+
|
| 536 |
+
def on_engine_change(selected_engine):
|
| 537 |
+
|
| 538 |
+
if selected_engine == "kwant":
|
| 539 |
+
gr.Warning(
|
| 540 |
+
"Kwant simulation is temporarily unavailable."
|
| 541 |
+
)
|
| 542 |
+
|
| 543 |
+
defaults = ENGINE_DEFAULTS.get(
|
| 544 |
+
selected_engine,
|
| 545 |
+
{"constraints": {}, "objective": {}}
|
| 546 |
)
|
| 547 |
+
|
| 548 |
+
obj = defaults["objective"]
|
| 549 |
+
|
| 550 |
+
available_metrics = ENGINE_METRICS.get(
|
| 551 |
+
selected_engine, []
|
| 552 |
+
)
|
| 553 |
+
|
| 554 |
+
selected_metric = (
|
| 555 |
+
obj.get("metric")
|
| 556 |
+
if obj.get("metric") in available_metrics
|
| 557 |
+
else (available_metrics[0] if available_metrics else None)
|
| 558 |
+
)
|
| 559 |
+
|
| 560 |
+
return (
|
| 561 |
+
gr.update(
|
| 562 |
+
choices=available_metrics,
|
| 563 |
+
value=selected_metric
|
| 564 |
+
),
|
| 565 |
+
defaults["constraints"],
|
| 566 |
+
obj.get("direction", "maximize"),
|
| 567 |
+
obj.get("target", 0.95),
|
| 568 |
+
obj.get("scale", 0.1),
|
| 569 |
+
gr.update(
|
| 570 |
+
interactive=selected_engine != "kwant"
|
| 571 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 572 |
)
|
| 573 |
|
| 574 |
engine.change(
|
| 575 |
+
fn=on_engine_change,
|
| 576 |
+
inputs=engine,
|
| 577 |
+
outputs=[
|
| 578 |
+
metric,
|
| 579 |
+
constraints,
|
| 580 |
+
direction,
|
| 581 |
+
target,
|
| 582 |
+
scale,
|
| 583 |
+
quantum_button
|
| 584 |
+
]
|
| 585 |
)
|
| 586 |
+
|
| 587 |
+
# --------------------------
|
| 588 |
+
# METRIC EVENTS
|
| 589 |
+
# --------------------------
|
| 590 |
+
|
| 591 |
+
def on_metric_change(selected_engine, selected_metric):
|
| 592 |
+
|
| 593 |
+
defaults = ENGINE_DEFAULTS.get(
|
| 594 |
+
selected_engine, {}
|
| 595 |
+
)
|
| 596 |
+
|
| 597 |
+
obj = defaults.get("objective", {})
|
| 598 |
+
|
| 599 |
+
return (
|
| 600 |
+
obj.get("direction", "maximize"),
|
| 601 |
+
obj.get("target", 0.95),
|
| 602 |
+
obj.get("scale", 0.1)
|
| 603 |
+
)
|
| 604 |
+
|
| 605 |
metric.change(
|
| 606 |
+
fn=on_metric_change,
|
| 607 |
inputs=[engine, metric],
|
| 608 |
+
outputs=[direction, target, scale]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 609 |
)
|
| 610 |
+
|
| 611 |
+
# --------------------------
|
| 612 |
+
# GENERATION
|
| 613 |
+
# --------------------------
|
| 614 |
+
|
| 615 |
+
def submit_quantum(
|
| 616 |
+
goal,
|
| 617 |
+
question,
|
| 618 |
+
engine,
|
| 619 |
+
constraints,
|
| 620 |
+
metric,
|
| 621 |
+
direction,
|
| 622 |
+
target,
|
| 623 |
+
scale
|
| 624 |
+
):
|
| 625 |
+
|
| 626 |
+
objective = {
|
| 627 |
+
"metric": metric,
|
| 628 |
+
"direction": direction,
|
| 629 |
+
"target": float(target),
|
| 630 |
+
"scale": float(scale)
|
| 631 |
+
}
|
| 632 |
+
|
| 633 |
+
return quantum_respond(
|
| 634 |
+
goal,
|
| 635 |
+
question,
|
| 636 |
+
engine,
|
| 637 |
+
constraints,
|
| 638 |
+
objective
|
| 639 |
+
)
|
| 640 |
+
|
| 641 |
+
quantum_button.click(
|
| 642 |
+
fn=submit_quantum,
|
| 643 |
+
inputs=[
|
| 644 |
+
goal,
|
| 645 |
+
question,
|
| 646 |
+
engine,
|
| 647 |
+
constraints,
|
| 648 |
+
metric,
|
| 649 |
+
direction,
|
| 650 |
+
target,
|
| 651 |
+
scale
|
| 652 |
+
],
|
| 653 |
+
outputs=[
|
| 654 |
+
quantum_output,
|
| 655 |
+
parsed_output,
|
| 656 |
+
quantum_plot,
|
| 657 |
+
simulation_status,
|
| 658 |
+
simulation_metrics,
|
| 659 |
+
objective_result
|
| 660 |
+
],
|
| 661 |
+
concurrency_limit=1
|
| 662 |
)
|
| 663 |
+
|
| 664 |
+
# --------------------------
|
| 665 |
+
# CLEAR
|
| 666 |
+
# --------------------------
|
| 667 |
+
|
| 668 |
+
clear_button.click(
|
| 669 |
+
fn=lambda: ("", {}, None, "", {}, {}),
|
| 670 |
+
outputs=[
|
| 671 |
+
quantum_output,
|
| 672 |
+
parsed_output,
|
| 673 |
+
quantum_plot,
|
| 674 |
+
simulation_status,
|
| 675 |
+
simulation_metrics,
|
| 676 |
+
objective_result
|
| 677 |
+
],
|
| 678 |
+
queue=False
|
| 679 |
)
|
| 680 |
+
|
| 681 |
+
# ==========================
|
| 682 |
+
# MINI CODE ASSISTANT
|
| 683 |
+
# ==========================
|
| 684 |
+
|
| 685 |
+
with gr.Column(visible=False) as coder_section:
|
| 686 |
+
|
| 687 |
+
gr.Markdown("# Mini Code Assistant")
|
| 688 |
+
gr.Markdown(
|
| 689 |
+
"**Qwen3-1.7B — Fine-Tuned by Oguz Yesil**"
|
|
|
|
| 690 |
)
|
|
|
|
| 691 |
|
|
|
|
|
|
|
| 692 |
gr.ChatInterface(
|
| 693 |
fn=coder_respond,
|
| 694 |
+
examples=[
|
| 695 |
+
"Explain async/await in Python.",
|
| 696 |
+
"Write a Python function to find prime numbers.",
|
| 697 |
+
"How does binary search work?",
|
| 698 |
+
"Explain the difference between a list and a tuple."
|
| 699 |
+
]
|
| 700 |
)
|
| 701 |
+
|
| 702 |
+
# ==========================
|
| 703 |
+
# TEXT TO SQL
|
| 704 |
+
# ==========================
|
| 705 |
+
|
| 706 |
+
with gr.Column(visible=False) as sql_section:
|
| 707 |
+
|
| 708 |
+
gr.Markdown("# Text to SQL")
|
| 709 |
+
gr.Markdown(
|
| 710 |
+
"**Qwen3-0.6B — Fine-Tuned by Oguz Yesil**"
|
| 711 |
+
)
|
| 712 |
+
|
| 713 |
gr.ChatInterface(
|
| 714 |
+
fn=text2sql_respond,
|
| 715 |
+
examples=[
|
| 716 |
+
"Select all customers who placed more than five orders.",
|
| 717 |
+
"Find the second highest salary from an employees table.",
|
| 718 |
+
"Explain INNER JOIN versus LEFT JOIN.",
|
| 719 |
+
"Write a PostgreSQL query to calculate monthly revenue."
|
| 720 |
+
]
|
| 721 |
+
)
|
| 722 |
+
|
| 723 |
+
# ==========================
|
| 724 |
+
# INVALID MODEL URL
|
| 725 |
+
# ==========================
|
| 726 |
+
|
| 727 |
+
with gr.Column(visible=False) as unknown_section:
|
| 728 |
+
|
| 729 |
+
gr.Markdown("## Model Not Found")
|
| 730 |
+
|
| 731 |
+
gr.Markdown(
|
| 732 |
+
"This AI demo is not available. "
|
| 733 |
+
"Please open it from the corresponding portfolio page."
|
| 734 |
)
|
|
|
|
|
|
|
| 735 |
|
| 736 |
+
# ==========================
|
| 737 |
+
# URL ROUTING
|
| 738 |
+
# ==========================
|
| 739 |
+
|
| 740 |
+
demo.load(
|
| 741 |
+
fn=detect_model,
|
| 742 |
+
outputs=[
|
| 743 |
+
quantum_section,
|
| 744 |
+
coder_section,
|
| 745 |
+
sql_section,
|
| 746 |
+
unknown_section
|
| 747 |
+
],
|
| 748 |
+
queue=False
|
| 749 |
+
)
|
| 750 |
+
|
| 751 |
+
# ==========================
|
| 752 |
+
# FOOTER
|
| 753 |
+
# ==========================
|
| 754 |
+
|
| 755 |
+
gr.Markdown(
|
| 756 |
+
"---\n"
|
| 757 |
+
"Developed by **Oguz Yesil** · "
|
| 758 |
+
"[Portfolio](https://oguzyesil.dev)"
|
| 759 |
+
)
|
| 760 |
+
|
| 761 |
+
|
| 762 |
+
# ==============================
|
| 763 |
+
# LAUNCH
|
| 764 |
+
# ==============================
|
| 765 |
|
| 766 |
demo.queue(default_concurrency_limit=1)
|
| 767 |
|
| 768 |
if __name__ == "__main__":
|
| 769 |
+
demo.launch(
|
| 770 |
+
ssr_mode=False,
|
| 771 |
+
share=False
|
| 772 |
+
)
|