File size: 1,643 Bytes
18a628a
 
9639dd1
ff38b89
18a628a
468788e
 
ff38b89
 
 
 
 
 
 
 
18a628a
ff38b89
 
 
18a628a
ff38b89
18a628a
 
 
 
 
 
 
 
 
9639dd1
18a628a
 
 
 
 
 
 
 
9639dd1
18a628a
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
# app.py

import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline

model_id = "google/flan-t5-base"  # ~250MB, loads fast


# Load tokenizer and model with trust_remote_code
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id,
                                             trust_remote_code=True,
                                             torch_dtype="auto")

# Create pipeline (no use_cache to avoid past_key_values crash)
polisher = pipeline("text-generation",
                    model=model,
                    tokenizer=tokenizer,
                    device=-1)

def oxford_polish_strict(sentence: str) -> str:
    prompt = (
        "You are an Oxford grammar professor. "
        "Rewrite the following sentence in formal written English, following the Oxford University Style Guide. "
        "Ensure tense matches time expressions (e.g. 'tomorrow' → future, 'yesterday' → past), "
        "use British spelling, apply the Oxford comma, and correct uncountable nouns naturally.\n\n"
        f"Sentence: {sentence}\n\nCorrected:"
    )
    out = polisher(prompt, max_new_tokens=80, do_sample=False)
    return out[0]["generated_text"].split("Corrected:")[-1].strip()

# Gradio interface
demo = gr.Interface(
    fn=oxford_polish,
    inputs=gr.Textbox(lines=2, placeholder="Enter a sentence to correct..."),
    outputs=gr.Textbox(label="Oxford-style Correction"),
    title="Oxford Grammar Polisher",
    description="Rewrite sentences in formal written English using Oxford grammar rules. Powered by Falcon-7B-Instruct."
)

demo.launch()