Spaces:
Running
Running
Upload 3 files
Browse files- README.md +15 -14
- app.py +31 -0
- requirements.txt +4 -0
README.md
CHANGED
|
@@ -1,14 +1,15 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
| 1 |
+
# Grammar Fixer (Phi-4 Mini Edition)
|
| 2 |
+
|
| 3 |
+
A lightweight web app that corrects English grammar using Microsoft's `phi-4-mini-instruct` model.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
- Instruction-following prompt
|
| 7 |
+
- Fast response
|
| 8 |
+
- Fits Hugging Face Spaces Free Tier
|
| 9 |
+
|
| 10 |
+
## Model Info
|
| 11 |
+
- [microsoft/phi-4-mini-instruct](https://huggingface.co/microsoft/phi-4-mini-instruct)
|
| 12 |
+
|
| 13 |
+
## Example Prompt
|
| 14 |
+
Input: `he go to school yesterday`
|
| 15 |
+
Output: `He went to school yesterday.`
|
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
|
| 3 |
+
import os
|
| 4 |
+
from huggingface_hub import login
|
| 5 |
+
|
| 6 |
+
# Load token from environment variable
|
| 7 |
+
HUGGINGFACE_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
+
login(token=HUGGINGFACE_TOKEN)
|
| 9 |
+
|
| 10 |
+
model_name = "microsoft/phi-4-mini-instruct"
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, token=HUGGINGFACE_TOKEN)
|
| 12 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map="auto", token=HUGGINGFACE_TOKEN)
|
| 13 |
+
|
| 14 |
+
pipe = TextGenerationPipeline(model=model, tokenizer=tokenizer)
|
| 15 |
+
|
| 16 |
+
def correct_grammar(text):
|
| 17 |
+
prompt = f"Correct the grammar:\n{text}\nCorrected:"
|
| 18 |
+
result = pipe(prompt, max_new_tokens=64, do_sample=False, temperature=0.3)[0]["generated_text"]
|
| 19 |
+
if "Corrected:" in result:
|
| 20 |
+
corrected = result.split("Corrected:")[1].strip()
|
| 21 |
+
else:
|
| 22 |
+
corrected = result.strip()
|
| 23 |
+
return corrected
|
| 24 |
+
|
| 25 |
+
gr.Interface(
|
| 26 |
+
fn=correct_grammar,
|
| 27 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter a grammatically incorrect sentence..."),
|
| 28 |
+
outputs="text",
|
| 29 |
+
title="Grammar Fixer",
|
| 30 |
+
description="Fix grammar using Microsoft's Phi-4 Mini Instruct model."
|
| 31 |
+
).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio
|
| 4 |
+
huggingface_hub
|