Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
MODEL_ID = "HuggingFaceTB/SmolLM2-360M-Instruct"
|
| 5 |
+
|
| 6 |
+
generator = pipeline(
|
| 7 |
+
"text-generation",
|
| 8 |
+
model=MODEL_ID,
|
| 9 |
+
device="cpu",
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
SYSTEM_PROMPT = (
|
| 13 |
+
"You are a writing assistant. You are given a paragraph and a quotation. "
|
| 14 |
+
"Insert the quotation into the paragraph at a sensible point. Wrap the "
|
| 15 |
+
"quotation in double quote marks. Do not change the wording of the "
|
| 16 |
+
"paragraph. Do not change the wording of the quotation. Output only the "
|
| 17 |
+
"revised paragraph, with the quotation inserted."
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def incorporate_quote(paragraph, quote):
|
| 22 |
+
paragraph = (paragraph or "").strip()
|
| 23 |
+
quote = (quote or "").strip()
|
| 24 |
+
if not paragraph or not quote:
|
| 25 |
+
return "Please provide both a paragraph and a quotation."
|
| 26 |
+
|
| 27 |
+
messages = [
|
| 28 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 29 |
+
{
|
| 30 |
+
"role": "user",
|
| 31 |
+
"content": f"Paragraph:\n{paragraph}\n\nQuotation:\n{quote}",
|
| 32 |
+
},
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
output = generator(
|
| 36 |
+
messages,
|
| 37 |
+
max_new_tokens=300,
|
| 38 |
+
do_sample=False,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
return output[0]["generated_text"][-1]["content"].strip()
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
EXAMPLES = [
|
| 45 |
+
[
|
| 46 |
+
"Joan Didion's evening walks are not nature writing in the conventional "
|
| 47 |
+
"sense. They are an attempt to mark time, to register the gradient of "
|
| 48 |
+
"light from one shade of blue to the next.",
|
| 49 |
+
"In certain latitudes there comes a span of time approaching and "
|
| 50 |
+
"following the summer solstice, some weeks in all, when the twilights "
|
| 51 |
+
"turn long and blue.",
|
| 52 |
+
],
|
| 53 |
+
[
|
| 54 |
+
"Writing teachers often tell their students to use specific imagery "
|
| 55 |
+
"rather than abstractions. The advice is harder to follow than it "
|
| 56 |
+
"sounds, especially for students who have been rewarded all through "
|
| 57 |
+
"school for sounding general and serious.",
|
| 58 |
+
"Show, don't tell.",
|
| 59 |
+
],
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
with gr.Blocks(title="Quote Incorporation — Simple") as demo:
|
| 64 |
+
gr.Markdown(
|
| 65 |
+
"""
|
| 66 |
+
# Quote Incorporation — Space 1 (Simple)
|
| 67 |
+
|
| 68 |
+
Drop a quotation into a paragraph with quote marks. Uses a small
|
| 69 |
+
instruction-tuned model (SmolLM2-360M) on Hugging Face free CPU.
|
| 70 |
+
Fast, basic, no cleverness.
|
| 71 |
+
"""
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
with gr.Row():
|
| 75 |
+
with gr.Column():
|
| 76 |
+
paragraph_in = gr.Textbox(
|
| 77 |
+
label="Paragraph",
|
| 78 |
+
placeholder="Paste a paragraph of writing here…",
|
| 79 |
+
lines=8,
|
| 80 |
+
)
|
| 81 |
+
quote_in = gr.Textbox(
|
| 82 |
+
label="Quotation",
|
| 83 |
+
placeholder="Paste the quotation (without quote marks)…",
|
| 84 |
+
lines=3,
|
| 85 |
+
)
|
| 86 |
+
submit_btn = gr.Button("Incorporate Quote", variant="primary")
|
| 87 |
+
|
| 88 |
+
with gr.Column():
|
| 89 |
+
result_out = gr.Textbox(
|
| 90 |
+
label="Revised Paragraph",
|
| 91 |
+
lines=12,
|
| 92 |
+
show_copy_button=True,
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
submit_btn.click(
|
| 96 |
+
fn=incorporate_quote,
|
| 97 |
+
inputs=[paragraph_in, quote_in],
|
| 98 |
+
outputs=[result_out],
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
gr.Examples(
|
| 102 |
+
examples=EXAMPLES,
|
| 103 |
+
inputs=[paragraph_in, quote_in],
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
if __name__ == "__main__":
|
| 108 |
+
demo.launch()
|