Spaces:
Paused
Paused
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -76,6 +76,27 @@ def to_code(keys):
|
|
| 76 |
return "\n".join(lines + [" return r"])
|
| 77 |
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
def run(code):
|
| 80 |
src = f"{code}\n\nprint(solve({DEMO}))\n"
|
| 81 |
with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f:
|
|
@@ -195,6 +216,10 @@ def _respond(msg, history):
|
|
| 195 |
time.sleep(0.4)
|
| 196 |
buf += f"\n🛠️ **Composing the code…**\n```python\n{code}\n```\n"
|
| 197 |
yield buf
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
time.sleep(0.5)
|
| 199 |
buf += f"\n▶️ **Running it:** `solve({DEMO})` → **`{run(code)}`**"
|
| 200 |
yield buf
|
|
|
|
| 76 |
return "\n".join(lines + [" return r"])
|
| 77 |
|
| 78 |
|
| 79 |
+
JS = { # same primitives rendered as JavaScript — symbols carry intent, not syntax
|
| 80 |
+
"evens": "r = r.filter(x => x % 2 === 0);", "pos": "r = r.filter(x => x > 0);",
|
| 81 |
+
"double": "r = r.map(x => x * 2);", "square": "r = r.map(x => x * x);",
|
| 82 |
+
"inc": "r = r.map(x => x + 1);", "negate": "r = r.map(x => -x);",
|
| 83 |
+
"absval": "r = r.map(x => Math.abs(x));", "rev": "r = r.slice().reverse();",
|
| 84 |
+
"sorta": "r = r.slice().sort((a, b) => a - b);", "sortd": "r = r.slice().sort((a, b) => b - a);",
|
| 85 |
+
"uniq": "r = [...new Set(r)];", "dec": "r = r.map(x => x - 1);",
|
| 86 |
+
"sum": "return r.reduce((a, b) => a + b, 0);", "max": "return r.length ? Math.max(...r) : 0;",
|
| 87 |
+
"len": "return r.length;", "cnt": "return r.length;",
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def to_js(keys):
|
| 92 |
+
lines = ["function solve(xs) {", " let r = [...xs];"]
|
| 93 |
+
for k in keys:
|
| 94 |
+
lines.append(" " + JS[k])
|
| 95 |
+
if JS[k].startswith("return"):
|
| 96 |
+
return "\n".join(lines + ["}"])
|
| 97 |
+
return "\n".join(lines + [" return r;", "}"])
|
| 98 |
+
|
| 99 |
+
|
| 100 |
def run(code):
|
| 101 |
src = f"{code}\n\nprint(solve({DEMO}))\n"
|
| 102 |
with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f:
|
|
|
|
| 216 |
time.sleep(0.4)
|
| 217 |
buf += f"\n🛠️ **Composing the code…**\n```python\n{code}\n```\n"
|
| 218 |
yield buf
|
| 219 |
+
time.sleep(0.4)
|
| 220 |
+
buf += (f"\n🌐 **Same message, different language** (the glyphs encode intent, "
|
| 221 |
+
f"not Python):\n```javascript\n{to_js(keys)}\n```\n")
|
| 222 |
+
yield buf
|
| 223 |
time.sleep(0.5)
|
| 224 |
buf += f"\n▶️ **Running it:** `solve({DEMO})` → **`{run(code)}`**"
|
| 225 |
yield buf
|