Delete app1.py
Browse files
app1.py
DELETED
|
@@ -1,63 +0,0 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
|
| 3 |
-
import gradio as gr
|
| 4 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 5 |
-
|
| 6 |
-
# Only the official Google FLAN-T5 models
|
| 7 |
-
MODEL_OPTIONS = {
|
| 8 |
-
"FLAN-T5-small (Google)": "google/flan-t5-small",
|
| 9 |
-
"FLAN-T5-base (Google)": "google/flan-t5-base"
|
| 10 |
-
}
|
| 11 |
-
|
| 12 |
-
# Cache loaded pipelines
|
| 13 |
-
loaded_pipelines = {}
|
| 14 |
-
|
| 15 |
-
def get_pipeline(model_id: str):
|
| 16 |
-
if model_id not in loaded_pipelines:
|
| 17 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 18 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 19 |
-
model_id,
|
| 20 |
-
low_cpu_mem_usage=True, # CPU optimization
|
| 21 |
-
torch_dtype="auto"
|
| 22 |
-
)
|
| 23 |
-
pipe = pipeline("text2text-generation",
|
| 24 |
-
model=model,
|
| 25 |
-
tokenizer=tokenizer,
|
| 26 |
-
device=-1)
|
| 27 |
-
# Warm-up to avoid first-call lag
|
| 28 |
-
_ = pipe("Correct the grammar: test", max_new_tokens=8, do_sample=False)
|
| 29 |
-
loaded_pipelines[model_id] = pipe
|
| 30 |
-
return loaded_pipelines[model_id]
|
| 31 |
-
|
| 32 |
-
def oxford_polish(sentence: str, model_choice: str) -> str:
|
| 33 |
-
model_id = MODEL_OPTIONS[model_choice]
|
| 34 |
-
polisher = get_pipeline(model_id)
|
| 35 |
-
|
| 36 |
-
# Minimal prompt for FLAN-T5
|
| 37 |
-
prompt = f"You are an English grammar corrector and teacher. Return the corrected version: {sentence}"
|
| 38 |
-
out = polisher(prompt,
|
| 39 |
-
max_new_tokens=60,
|
| 40 |
-
do_sample=False,
|
| 41 |
-
num_beams=2)
|
| 42 |
-
text = out[0]["generated_text"].strip()
|
| 43 |
-
|
| 44 |
-
# Strip accidental echo
|
| 45 |
-
if text.startswith(prompt):
|
| 46 |
-
text = text[len(prompt):].strip()
|
| 47 |
-
return text
|
| 48 |
-
|
| 49 |
-
# Gradio interface
|
| 50 |
-
demo = gr.Interface(
|
| 51 |
-
fn=oxford_polish,
|
| 52 |
-
inputs=[
|
| 53 |
-
gr.Textbox(lines=2, placeholder="Enter a sentence to correct..."),
|
| 54 |
-
gr.Dropdown(choices=list(MODEL_OPTIONS.keys()),
|
| 55 |
-
value="FLAN-T5-base (Google)",
|
| 56 |
-
label="Choose Model")
|
| 57 |
-
],
|
| 58 |
-
outputs=gr.Textbox(label="Oxford-grammar Correction"),
|
| 59 |
-
title="Oxford Grammar Polisher",
|
| 60 |
-
description="Compare Google’s official FLAN-T5 small and base models for grammar correction."
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|