Spaces:
Sleeping

sedweave-demo / app.py
asterbraid's picture
Add 1.5B option while preserving the 0.5B prediction endpoint
72cbab6 verified
Raw History Blame Contribute Delete
3.48 kB
from __future__ import annotations
import os
import gradio as gr
from sedweave.execution import run_sed
from sedweave.model import generate_command
MODEL_ID = os.environ.get("SEDWEAVE_MODEL", "asterbraid/sedweave-0.5b")
MODEL_1_5B = os.environ.get("SEDWEAVE_MODEL_1_5B", "asterbraid/sedweave-1.5b")
EXAMPLES = [
[
"Replace every occurrence of amber with delta.",
"amber birch\ncobalt amber\n",
],
[
"Delete every line containing failed.",
"alice active\nbruno failed\nchen ready\n",
],
[
"Print only lines 2 through 4.",
"line one\nline two\nline three\nline four\nline five\n",
],
]
def predict_with_model(request: str, input_text: str, model_id: str):
if not request.strip():
return "", "", "Enter a text-processing request."
if not input_text.strip():
return "", "", "Provide representative sample input."
if len(input_text) > 30_000:
return "", "", "Keep the sample below 30,000 characters for this demo."
try:
program = generate_command(request, input_text, model_id)
except Exception as error:
return "", "", f"Model generation failed: {error}"
try:
result = run_sed(program, input_text, timeout=3.0)
status = "Executed successfully with local sed. Review the program before using it on important data."
return program, result, status
except Exception as error:
return program, "", f"Execution rejected or failed: {error}"
def predict(request: str, input_text: str):
return predict_with_model(request, input_text, MODEL_ID)
def predict_1_5b(request: str, input_text: str):
return predict_with_model(request, input_text, MODEL_1_5B)
with gr.Blocks(title="Sedweave — natural language to sed") as demo:
gr.Markdown(
"""# Sedweave
Generate a small, local `sed` command from a plain-language request and representative input.
The model emits a command—not a shell pipeline. This demo executes it without file, environment,
pipe, or process access. Use plain text input.
"""
)
with gr.Row():
with gr.Column():
request = gr.Textbox(label="Request", placeholder="Sum the third column for each category in column 2")
input_text = gr.Textbox(label="Sample input", lines=14, max_lines=30)
button = gr.Button("Generate and run (0.5B)", variant="primary")
button_1_5b = gr.Button("Generate and run (1.5B)", variant="secondary")
with gr.Column():
program = gr.Code(label="Generated sed command", language="shell")
result = gr.Textbox(label="Output", lines=14, max_lines=30)
status = gr.Markdown()
gr.Examples(EXAMPLES, inputs=[request, input_text])
gr.Markdown(
"""**Scope:** English requests and POSIX-style `sed` on supplied text. Generation is not a
correctness guarantee. Do not use the model for destructive transformations without reviewing its output.
Model and data: [Asterbraid on Hugging Face](https://huggingface.co/asterbraid)."""
)
button.click(predict, inputs=[request, input_text], outputs=[program, result, status])
button_1_5b.click(predict_1_5b, inputs=[request, input_text], outputs=[program, result, status])
request.submit(predict, inputs=[request, input_text], outputs=[program, result, status])
if __name__ == "__main__":
demo.queue(default_concurrency_limit=1, max_size=16).launch(server_name="0.0.0.0")