File size: 2,681 Bytes
ee94055
e7b16a5
ee94055
e7b16a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee94055
75b10dc
e7b16a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75b10dc
e7b16a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75b10dc
 
 
e7b16a5
 
75b10dc
 
e7b16a5
 
 
 
 
 
 
75b10dc
 
e7b16a5
 
75b10dc
 
e7b16a5
 
 
75b10dc
e7b16a5
75b10dc
 
e7b16a5
75b10dc
ee94055
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
import gradio as gr
from functools import lru_cache

@lru_cache(maxsize=1)
def get_rewriter():
    """
    Use a lightweight instruction-following model that can run on CPU.
    flan-t5-small is generally more reliable for "rewrite" than GPT2-style models.
    """
    from transformers import pipeline
    return pipeline(
        task="text2text-generation",
        model="google/flan-t5-small",
        device=-1,  # CPU
    )

def build_prompt(text: str, style: str) -> str:
    text = (text or "").strip()
    if not text:
        return ""

    if style == "More formal":
        return (
            "Rewrite the text in a more formal tone. "
            "Keep the original meaning. Output only the rewritten text.\n\n"
            f"Text: {text}"
        )
    if style == "More friendly":
        return (
            "Rewrite the text in a friendly, warm tone. "
            "Keep the original meaning. Output only the rewritten text.\n\n"
            f"Text: {text}"
        )
    return (
        "Rewrite the text to be shorter and clearer. "
        "Keep the original meaning. Output only the rewritten text.\n\n"
        f"Text: {text}"
    )

def rewrite(text: str, style: str) -> str:
    text = (text or "").strip()
    if not text:
        return "Please enter some text."

    prompt = build_prompt(text, style)

    try:
        rewriter = get_rewriter()
        out = rewriter(
            prompt,
            max_new_tokens=128,
            do_sample=False,
        )
        result = (out[0].get("generated_text") or "").strip()
        return result if result else "No output. Try a shorter input."
    except Exception as e:
        return (
            "Error: failed to run the model.\n"
            "If this is the first run, the Space may still be downloading the model.\n\n"
            f"Details: {type(e).__name__}: {e}"
        )

with gr.Blocks(title="AI Text Rewriter") as demo:
    gr.Markdown(
        "AI Text Rewriter\n"
        "Paste a sentence or short paragraph, choose a style, then rewrite with AI."
    )

    with gr.Row():
        style = gr.Radio(
            ["More formal", "More friendly", "Shorter"],
            value="More friendly",
            label="Rewrite style",
        )

    text_input = gr.Textbox(
        label="Your text",
        placeholder="Type or paste text here...",
        lines=5,
    )

    with gr.Row():
        btn = gr.Button("Rewrite with AI")
        clear = gr.Button("Clear")

    output = gr.Textbox(label="Result", lines=6)

    btn.click(fn=rewrite, inputs=[text_input, style], outputs=output)
    clear.click(fn=lambda: ("", ""), inputs=None, outputs=[text_input, output])

demo.launch()