File size: 3,164 Bytes
94bf81c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import gradio as gr
from transformers import pipeline

MODEL_ID = "HuggingFaceTB/SmolLM2-360M-Instruct"

generator = pipeline(
    "text-generation",
    model=MODEL_ID,
    device="cpu",
)

SYSTEM_PROMPT = (
    "You are a writing assistant. You are given a paragraph and a quotation. "
    "Insert the quotation into the paragraph at a sensible point. Wrap the "
    "quotation in double quote marks. Do not change the wording of the "
    "paragraph. Do not change the wording of the quotation. Output only the "
    "revised paragraph, with the quotation inserted."
)


def incorporate_quote(paragraph, quote):
    paragraph = (paragraph or "").strip()
    quote = (quote or "").strip()
    if not paragraph or not quote:
        return "Please provide both a paragraph and a quotation."

    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {
            "role": "user",
            "content": f"Paragraph:\n{paragraph}\n\nQuotation:\n{quote}",
        },
    ]

    output = generator(
        messages,
        max_new_tokens=300,
        do_sample=False,
    )

    return output[0]["generated_text"][-1]["content"].strip()


EXAMPLES = [
    [
        "Joan Didion's evening walks are not nature writing in the conventional "
        "sense. They are an attempt to mark time, to register the gradient of "
        "light from one shade of blue to the next.",
        "In certain latitudes there comes a span of time approaching and "
        "following the summer solstice, some weeks in all, when the twilights "
        "turn long and blue.",
    ],
    [
        "Writing teachers often tell their students to use specific imagery "
        "rather than abstractions. The advice is harder to follow than it "
        "sounds, especially for students who have been rewarded all through "
        "school for sounding general and serious.",
        "Show, don't tell.",
    ],
]


with gr.Blocks(title="Quote Incorporation — Simple") as demo:
    gr.Markdown(
        """
        # Quote Incorporation — Space 1 (Simple)

        Drop a quotation into a paragraph with quote marks. Uses a small
        instruction-tuned model (SmolLM2-360M) on Hugging Face free CPU.
        Fast, basic, no cleverness.
        """
    )

    with gr.Row():
        with gr.Column():
            paragraph_in = gr.Textbox(
                label="Paragraph",
                placeholder="Paste a paragraph of writing here…",
                lines=8,
            )
            quote_in = gr.Textbox(
                label="Quotation",
                placeholder="Paste the quotation (without quote marks)…",
                lines=3,
            )
            submit_btn = gr.Button("Incorporate Quote", variant="primary")

        with gr.Column():
            result_out = gr.Textbox(
                label="Revised Paragraph",
                lines=12,
            )

    submit_btn.click(
        fn=incorporate_quote,
        inputs=[paragraph_in, quote_in],
        outputs=[result_out],
    )

    gr.Examples(
        examples=EXAMPLES,
        inputs=[paragraph_in, quote_in],
    )


if __name__ == "__main__":
    demo.launch()