Spaces:
Sleeping

File size: 3,817 Bytes
05cac50
 
 
 
 
 
 
 
 
 
b9b90e5
05cac50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9b90e5
05cac50
 
 
 
 
 
 
b9b90e5
05cac50
 
 
 
 
 
 
 
 
 
b9b90e5
 
 
 
 
 
 
 
05cac50
 
 
 
 
b9b90e5
05cac50
 
 
 
 
 
b9b90e5
 
05cac50
 
a653226
05cac50
 
 
 
 
 
 
 
b9b90e5
05cac50
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from __future__ import annotations

import os

import gradio as gr

from awkweave.execution import run_awk
from awkweave.model import generate_program

MODEL_ID = os.environ.get("AWKWEAVE_MODEL", "asterbraid/awkweave-1.5b")
SMALL_MODEL_ID = os.environ.get("AWKWEAVE_SMALL_MODEL", "asterbraid/awkweave-0.5b")
EXAMPLES = [
    [
        "Print each name and the running total of the second column.",
        "alice 10 east\nbruno 15 west\nchen 7 east\ndina 12 west\n",
    ],
    [
        "The first row is a header. Print display_name for data rows whose score is greater than 15.",
        "record_id display_name score state\nr001 alice 12 ready\nr002 bruno 22 ready\nr003 chen 18 paused\n",
    ],
    [
        "Count records for each value in column 2. Print each value and its count in order of first appearance.",
        "alice east 10\nbruno west 15\nchen east 7\ndina west 12\neli north 9\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_program(request, input_text, model_id)
    except Exception as error:
        return "", "", f"Model generation failed: {error}"
    try:
        result = run_awk(program, input_text, timeout=3.0)
        status = "Executed successfully with local awk. 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_small(request: str, input_text: str):
    return predict_with_model(request, input_text, SMALL_MODEL_ID)


with gr.Blocks(title="Awkweave — natural language to awk") as demo:
    gr.Markdown(
        """# Awkweave
Generate a small, local `awk` program from a plain-language request and representative input.
The model emits a program—not a shell command. This demo executes it without file, environment,
pipe, or process access. Choose 1.5B for accuracy or 0.5B for speed. Use simple space-, tab-, or comma-separated text; quoted CSV is not supported.
"""
    )
    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 (1.5B)", variant="primary")
            small_button = gr.Button("Generate and run fast (0.5B)")
        with gr.Column():
            program = gr.Code(label="Generated awk program", 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 `awk` 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])
    small_button.click(predict_small, 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")