Hasarindu Perera commited on
feat: show input data and encoded parameters previews
Browse files
app.py
CHANGED
|
@@ -81,6 +81,9 @@ ENCODING_DESC = {
|
|
| 81 |
# Core run function
|
| 82 |
# ---------------------------------------------------------------------------
|
| 83 |
|
|
|
|
|
|
|
|
|
|
| 84 |
def run_quprep(
|
| 85 |
csv_file,
|
| 86 |
sample_name: str,
|
|
@@ -88,19 +91,21 @@ def run_quprep(
|
|
| 88 |
framework: str,
|
| 89 |
n_samples: int,
|
| 90 |
n_qubits: int,
|
| 91 |
-
) -> tuple[str, str, str]:
|
| 92 |
"""
|
| 93 |
-
Returns (circuit_output, cost_info, status_message).
|
| 94 |
"""
|
| 95 |
try:
|
| 96 |
import quprep as qd
|
| 97 |
except ImportError:
|
| 98 |
-
return "", "", "❌ quprep is not installed in this Space."
|
| 99 |
|
| 100 |
# --- load data ---
|
| 101 |
try:
|
| 102 |
if csv_file is not None:
|
| 103 |
-
|
|
|
|
|
|
|
| 104 |
elif sample_name and sample_name in SAMPLES:
|
| 105 |
df = pd.read_csv(io.StringIO(SAMPLES[sample_name]()))
|
| 106 |
else:
|
|
@@ -123,7 +128,7 @@ def run_quprep(
|
|
| 123 |
tmp_path = tmp.name
|
| 124 |
|
| 125 |
except Exception as exc:
|
| 126 |
-
return "", "", f"❌ Data loading error: {exc}"
|
| 127 |
|
| 128 |
# --- run pipeline ---
|
| 129 |
try:
|
|
@@ -143,13 +148,13 @@ def run_quprep(
|
|
| 143 |
|
| 144 |
except ImportError as exc:
|
| 145 |
missing = str(exc)
|
| 146 |
-
return "", "", (
|
| 147 |
f"⚠️ Optional dependency not installed in this Space: {missing}\n"
|
| 148 |
f"Try selecting **qasm** as the framework, or install the extra."
|
| 149 |
)
|
| 150 |
except Exception as exc:
|
| 151 |
tb = traceback.format_exc()
|
| 152 |
-
return "", "", f"❌ Pipeline error:\n{tb}"
|
| 153 |
finally:
|
| 154 |
try:
|
| 155 |
os.unlink(tmp_path)
|
|
@@ -159,7 +164,26 @@ def run_quprep(
|
|
| 159 |
# --- format output ---
|
| 160 |
circuits = result.circuits or []
|
| 161 |
if not circuits:
|
| 162 |
-
return "", "", "⚠️ No circuits produced."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
# show first circuit as text
|
| 165 |
first = circuits[0]
|
|
@@ -193,7 +217,7 @@ def run_quprep(
|
|
| 193 |
f"→ {n_total} circuit(s). Showing sample 0."
|
| 194 |
)
|
| 195 |
|
| 196 |
-
return circuit_text, cost_text, status
|
| 197 |
|
| 198 |
|
| 199 |
# ---------------------------------------------------------------------------
|
|
@@ -208,7 +232,8 @@ def run_recommend(csv_file, sample_name: str, task: str, n_qubits: int) -> str:
|
|
| 208 |
|
| 209 |
try:
|
| 210 |
if csv_file is not None:
|
| 211 |
-
|
|
|
|
| 212 |
elif sample_name and sample_name in SAMPLES:
|
| 213 |
df = pd.read_csv(io.StringIO(SAMPLES[sample_name]()))
|
| 214 |
else:
|
|
@@ -250,7 +275,8 @@ def run_compare(csv_file, sample_name: str, task: str, n_qubits: int) -> str:
|
|
| 250 |
|
| 251 |
try:
|
| 252 |
if csv_file is not None:
|
| 253 |
-
|
|
|
|
| 254 |
elif sample_name and sample_name in SAMPLES:
|
| 255 |
df = pd.read_csv(io.StringIO(SAMPLES[sample_name]()))
|
| 256 |
else:
|
|
@@ -341,8 +367,11 @@ Framework-agnostic: export to Qiskit, PennyLane, Cirq, TKET, Braket, Q#, IQM, or
|
|
| 341 |
|
| 342 |
with gr.Column(scale=2):
|
| 343 |
status_out = gr.Textbox(label="Status", lines=2, interactive=False)
|
| 344 |
-
|
|
|
|
| 345 |
cost_out = gr.Textbox(label="Cost estimate", lines=8, interactive=False)
|
|
|
|
|
|
|
| 346 |
|
| 347 |
enc_dd.change(
|
| 348 |
fn=lambda e: f"*{ENCODING_DESC.get(e, '')}*",
|
|
@@ -353,7 +382,7 @@ Framework-agnostic: export to Qiskit, PennyLane, Cirq, TKET, Braket, Q#, IQM, or
|
|
| 353 |
convert_btn.click(
|
| 354 |
fn=run_quprep,
|
| 355 |
inputs=[csv_upload, sample_dd, enc_dd, fw_dd, n_samples_sl, n_qubits_sl],
|
| 356 |
-
outputs=[circuit_out, cost_out, status_out],
|
| 357 |
)
|
| 358 |
|
| 359 |
# ── Tab 2: Recommend ────────────────────────────────────────────────
|
|
|
|
| 81 |
# Core run function
|
| 82 |
# ---------------------------------------------------------------------------
|
| 83 |
|
| 84 |
+
_EMPTY_DF = pd.DataFrame()
|
| 85 |
+
|
| 86 |
+
|
| 87 |
def run_quprep(
|
| 88 |
csv_file,
|
| 89 |
sample_name: str,
|
|
|
|
| 91 |
framework: str,
|
| 92 |
n_samples: int,
|
| 93 |
n_qubits: int,
|
| 94 |
+
) -> tuple[pd.DataFrame, pd.DataFrame, str, str, str]:
|
| 95 |
"""
|
| 96 |
+
Returns (input_preview, encoded_preview, circuit_output, cost_info, status_message).
|
| 97 |
"""
|
| 98 |
try:
|
| 99 |
import quprep as qd
|
| 100 |
except ImportError:
|
| 101 |
+
return _EMPTY_DF, _EMPTY_DF, "", "", "❌ quprep is not installed in this Space."
|
| 102 |
|
| 103 |
# --- load data ---
|
| 104 |
try:
|
| 105 |
if csv_file is not None:
|
| 106 |
+
# Gradio 6.x returns a filepath string; older versions return a file object
|
| 107 |
+
csv_path = csv_file if isinstance(csv_file, str) else csv_file.name
|
| 108 |
+
df = pd.read_csv(csv_path)
|
| 109 |
elif sample_name and sample_name in SAMPLES:
|
| 110 |
df = pd.read_csv(io.StringIO(SAMPLES[sample_name]()))
|
| 111 |
else:
|
|
|
|
| 128 |
tmp_path = tmp.name
|
| 129 |
|
| 130 |
except Exception as exc:
|
| 131 |
+
return _EMPTY_DF, _EMPTY_DF, "", "", f"❌ Data loading error: {exc}"
|
| 132 |
|
| 133 |
# --- run pipeline ---
|
| 134 |
try:
|
|
|
|
| 148 |
|
| 149 |
except ImportError as exc:
|
| 150 |
missing = str(exc)
|
| 151 |
+
return _EMPTY_DF, _EMPTY_DF, "", "", (
|
| 152 |
f"⚠️ Optional dependency not installed in this Space: {missing}\n"
|
| 153 |
f"Try selecting **qasm** as the framework, or install the extra."
|
| 154 |
)
|
| 155 |
except Exception as exc:
|
| 156 |
tb = traceback.format_exc()
|
| 157 |
+
return _EMPTY_DF, _EMPTY_DF, "", "", f"❌ Pipeline error:\n{tb}"
|
| 158 |
finally:
|
| 159 |
try:
|
| 160 |
os.unlink(tmp_path)
|
|
|
|
| 164 |
# --- format output ---
|
| 165 |
circuits = result.circuits or []
|
| 166 |
if not circuits:
|
| 167 |
+
return _EMPTY_DF, _EMPTY_DF, "", "", "⚠️ No circuits produced."
|
| 168 |
+
|
| 169 |
+
# input preview — first 5 rows of cleaned df
|
| 170 |
+
input_preview = df.head(5).round(4)
|
| 171 |
+
|
| 172 |
+
# encoded parameters preview — parameters array from each encoded result
|
| 173 |
+
encoded_list = result.encoded or []
|
| 174 |
+
if encoded_list:
|
| 175 |
+
try:
|
| 176 |
+
rows = []
|
| 177 |
+
for i, enc in enumerate(encoded_list[:5]):
|
| 178 |
+
params = enc.parameters
|
| 179 |
+
row = {f"q{j}": round(float(p), 4) for j, p in enumerate(params)}
|
| 180 |
+
row = {"sample": i, **row}
|
| 181 |
+
rows.append(row)
|
| 182 |
+
encoded_preview = pd.DataFrame(rows).set_index("sample")
|
| 183 |
+
except Exception:
|
| 184 |
+
encoded_preview = _EMPTY_DF
|
| 185 |
+
else:
|
| 186 |
+
encoded_preview = _EMPTY_DF
|
| 187 |
|
| 188 |
# show first circuit as text
|
| 189 |
first = circuits[0]
|
|
|
|
| 217 |
f"→ {n_total} circuit(s). Showing sample 0."
|
| 218 |
)
|
| 219 |
|
| 220 |
+
return input_preview, encoded_preview, circuit_text, cost_text, status
|
| 221 |
|
| 222 |
|
| 223 |
# ---------------------------------------------------------------------------
|
|
|
|
| 232 |
|
| 233 |
try:
|
| 234 |
if csv_file is not None:
|
| 235 |
+
csv_path = csv_file if isinstance(csv_file, str) else csv_file.name
|
| 236 |
+
df = pd.read_csv(csv_path)
|
| 237 |
elif sample_name and sample_name in SAMPLES:
|
| 238 |
df = pd.read_csv(io.StringIO(SAMPLES[sample_name]()))
|
| 239 |
else:
|
|
|
|
| 275 |
|
| 276 |
try:
|
| 277 |
if csv_file is not None:
|
| 278 |
+
csv_path = csv_file if isinstance(csv_file, str) else csv_file.name
|
| 279 |
+
df = pd.read_csv(csv_path)
|
| 280 |
elif sample_name and sample_name in SAMPLES:
|
| 281 |
df = pd.read_csv(io.StringIO(SAMPLES[sample_name]()))
|
| 282 |
else:
|
|
|
|
| 367 |
|
| 368 |
with gr.Column(scale=2):
|
| 369 |
status_out = gr.Textbox(label="Status", lines=2, interactive=False)
|
| 370 |
+
input_table = gr.Dataframe(label="Input data (first 5 rows)", interactive=False)
|
| 371 |
+
circuit_out = gr.Code(label="Circuit output (sample 0)", language="python", lines=20)
|
| 372 |
cost_out = gr.Textbox(label="Cost estimate", lines=8, interactive=False)
|
| 373 |
+
with gr.Accordion("Encoded parameters (first 5 rows)", open=False):
|
| 374 |
+
encoded_table = gr.Dataframe(interactive=False)
|
| 375 |
|
| 376 |
enc_dd.change(
|
| 377 |
fn=lambda e: f"*{ENCODING_DESC.get(e, '')}*",
|
|
|
|
| 382 |
convert_btn.click(
|
| 383 |
fn=run_quprep,
|
| 384 |
inputs=[csv_upload, sample_dd, enc_dd, fw_dd, n_samples_sl, n_qubits_sl],
|
| 385 |
+
outputs=[input_table, encoded_table, circuit_out, cost_out, status_out],
|
| 386 |
)
|
| 387 |
|
| 388 |
# ── Tab 2: Recommend ────────────────────────────────────────────────
|