Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,36 @@
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
-
from transformers import AutoTokenizer,
|
| 5 |
|
| 6 |
model_id = "google/flan-t5-base" # ~250MB, loads fast
|
| 7 |
|
| 8 |
-
|
| 9 |
-
# Load tokenizer and model with trust_remote_code
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 11 |
-
model =
|
| 12 |
-
trust_remote_code=True,
|
| 13 |
-
torch_dtype="auto")
|
| 14 |
|
| 15 |
-
# Create pipeline
|
| 16 |
-
polisher = pipeline("
|
| 17 |
model=model,
|
| 18 |
-
tokenizer=tokenizer
|
| 19 |
-
device=-1)
|
| 20 |
|
| 21 |
def oxford_polish_strict(sentence: str) -> str:
|
| 22 |
prompt = (
|
| 23 |
-
"
|
| 24 |
-
"Rewrite the following sentence in formal written English, following the Oxford University Style Guide. "
|
| 25 |
"Ensure tense matches time expressions (e.g. 'tomorrow' → future, 'yesterday' → past), "
|
| 26 |
-
"use British spelling, apply the Oxford comma, and correct uncountable nouns naturally
|
| 27 |
-
|
| 28 |
)
|
| 29 |
out = polisher(prompt, max_new_tokens=80, do_sample=False)
|
| 30 |
-
return out[0]["generated_text"].
|
| 31 |
|
| 32 |
# Gradio interface
|
| 33 |
demo = gr.Interface(
|
| 34 |
-
fn=
|
| 35 |
inputs=gr.Textbox(lines=2, placeholder="Enter a sentence to correct..."),
|
| 36 |
outputs=gr.Textbox(label="Oxford-style Correction"),
|
| 37 |
title="Oxford Grammar Polisher",
|
| 38 |
-
description="Rewrite sentences in formal written English using Oxford grammar rules. Powered by
|
| 39 |
)
|
| 40 |
|
| 41 |
demo.launch()
|
|
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 5 |
|
| 6 |
model_id = "google/flan-t5-base" # ~250MB, loads fast
|
| 7 |
|
| 8 |
+
# Load tokenizer and model correctly for FLAN-T5
|
|
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 10 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Create pipeline for text2text generation
|
| 13 |
+
polisher = pipeline("text2text-generation",
|
| 14 |
model=model,
|
| 15 |
+
tokenizer=tokenizer)
|
|
|
|
| 16 |
|
| 17 |
def oxford_polish_strict(sentence: str) -> str:
|
| 18 |
prompt = (
|
| 19 |
+
"Rewrite this sentence in formal written English, following the Oxford University Style Guide. "
|
|
|
|
| 20 |
"Ensure tense matches time expressions (e.g. 'tomorrow' → future, 'yesterday' → past), "
|
| 21 |
+
"use British spelling, apply the Oxford comma, and correct uncountable nouns naturally. "
|
| 22 |
+
"Sentence: " + sentence
|
| 23 |
)
|
| 24 |
out = polisher(prompt, max_new_tokens=80, do_sample=False)
|
| 25 |
+
return out[0]["generated_text"].strip()
|
| 26 |
|
| 27 |
# Gradio interface
|
| 28 |
demo = gr.Interface(
|
| 29 |
+
fn=oxford_polish_strict,
|
| 30 |
inputs=gr.Textbox(lines=2, placeholder="Enter a sentence to correct..."),
|
| 31 |
outputs=gr.Textbox(label="Oxford-style Correction"),
|
| 32 |
title="Oxford Grammar Polisher",
|
| 33 |
+
description="Rewrite sentences in formal written English using Oxford grammar rules. Powered by FLAN-T5."
|
| 34 |
)
|
| 35 |
|
| 36 |
demo.launch()
|