Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import time
|
|
| 6 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 7 |
from happytransformer import HappyTextToText, TTSettings # Using HappyTransformer
|
| 8 |
|
|
|
|
| 9 |
def load_models():
|
| 10 |
model_name = "prithivida/grammar_error_correcter_v1"
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
@@ -13,6 +14,9 @@ def load_models():
|
|
| 13 |
happy_tt = HappyTextToText("T5", "prithivida/grammar_error_correcter_v1") # Using T5-based model
|
| 14 |
return tokenizer, model, happy_tt
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
def transcribe_audio(audio):
|
| 17 |
recognizer = sr.Recognizer()
|
| 18 |
with sr.AudioFile(audio) as source:
|
|
@@ -25,7 +29,11 @@ def transcribe_audio(audio):
|
|
| 25 |
except sr.RequestError as e:
|
| 26 |
return f"Speech recognition error: {e}"
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
inputs = tokenizer.encode("gec: " + text, return_tensors="pt", max_length=128, truncation=True)
|
| 30 |
with torch.no_grad():
|
| 31 |
outputs = model.generate(inputs, max_length=128, num_return_sequences=1)
|
|
@@ -37,33 +45,27 @@ def correct_grammar(text, tokenizer, model, happy_tt):
|
|
| 37 |
|
| 38 |
return corrected_text, grammar_score, correction
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def gradio_audio_interface(audio):
|
| 46 |
-
text = transcribe_audio(audio)
|
| 47 |
-
return gradio_interface(text)
|
| 48 |
|
|
|
|
| 49 |
def main():
|
| 50 |
iface = gr.Interface(
|
| 51 |
-
fn=
|
| 52 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 53 |
outputs=["text", "number", "text"],
|
| 54 |
title="AI Grammar Checker",
|
| 55 |
-
description="
|
|
|
|
| 56 |
)
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
fn=gradio_audio_interface,
|
| 60 |
-
inputs=gr.Audio(sources=["microphone"], type="filepath"), # Fixed here
|
| 61 |
-
outputs=["text", "text", "number", "text"],
|
| 62 |
-
title="AI Grammar Checker (Audio)",
|
| 63 |
-
description="Speak to check grammar, get suggestions, and see a score."
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
gr.TabbedInterface([iface, audio_iface], ["Text Input", "Speech Input"]).launch()
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
main()
|
|
|
|
| 6 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 7 |
from happytransformer import HappyTextToText, TTSettings # Using HappyTransformer
|
| 8 |
|
| 9 |
+
# Load models only once for efficiency
|
| 10 |
def load_models():
|
| 11 |
model_name = "prithivida/grammar_error_correcter_v1"
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
| 14 |
happy_tt = HappyTextToText("T5", "prithivida/grammar_error_correcter_v1") # Using T5-based model
|
| 15 |
return tokenizer, model, happy_tt
|
| 16 |
|
| 17 |
+
tokenizer, model, happy_tt = load_models() # Load models at startup
|
| 18 |
+
|
| 19 |
+
# Speech-to-text conversion
|
| 20 |
def transcribe_audio(audio):
|
| 21 |
recognizer = sr.Recognizer()
|
| 22 |
with sr.AudioFile(audio) as source:
|
|
|
|
| 29 |
except sr.RequestError as e:
|
| 30 |
return f"Speech recognition error: {e}"
|
| 31 |
|
| 32 |
+
# Grammar correction function
|
| 33 |
+
def correct_grammar(text):
|
| 34 |
+
if not text.strip():
|
| 35 |
+
return "No input provided.", 0, "No correction available."
|
| 36 |
+
|
| 37 |
inputs = tokenizer.encode("gec: " + text, return_tensors="pt", max_length=128, truncation=True)
|
| 38 |
with torch.no_grad():
|
| 39 |
outputs = model.generate(inputs, max_length=128, num_return_sequences=1)
|
|
|
|
| 45 |
|
| 46 |
return corrected_text, grammar_score, correction
|
| 47 |
|
| 48 |
+
# Unified function for both speech and text input
|
| 49 |
+
def process_input(audio, text):
|
| 50 |
+
if audio:
|
| 51 |
+
text = transcribe_audio(audio) # If audio is provided, transcribe it
|
| 52 |
+
return correct_grammar(text)
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# Gradio UI
|
| 55 |
def main():
|
| 56 |
iface = gr.Interface(
|
| 57 |
+
fn=process_input,
|
| 58 |
+
inputs=[
|
| 59 |
+
gr.Audio(sources=["microphone"], type="filepath", label="Speak your sentence"),
|
| 60 |
+
gr.Textbox(placeholder="Or type here if not speaking...", label="Text Input"),
|
| 61 |
+
],
|
| 62 |
outputs=["text", "number", "text"],
|
| 63 |
title="AI Grammar Checker",
|
| 64 |
+
description="Speak or type a sentence to check its grammar, get corrections, and see a score.",
|
| 65 |
+
live=False, # Only processes when user submits
|
| 66 |
)
|
| 67 |
+
|
| 68 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
main()
|