Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
|
| 5 |
-
# Load Whisper for
|
| 6 |
asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-large-v3")
|
| 7 |
|
| 8 |
-
# Load
|
| 9 |
cola_model = AutoModelForSequenceClassification.from_pretrained("textattack/roberta-base-CoLA")
|
| 10 |
cola_tokenizer = AutoTokenizer.from_pretrained("textattack/roberta-base-CoLA")
|
| 11 |
grammar_pipeline = pipeline("text-classification", model=cola_model, tokenizer=cola_tokenizer)
|
| 12 |
|
| 13 |
-
# Load
|
| 14 |
correction_pipeline = pipeline("text2text-generation", model="vennify/t5-base-grammar-correction")
|
| 15 |
|
| 16 |
-
def process_audio(
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
score_label = grammar_result["label"]
|
| 23 |
-
score_confidence = grammar_result["score"]
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
fn=process_audio,
|
| 33 |
-
inputs=gr.Audio(
|
| 34 |
-
source="microphone", # enables both mic recording and upload
|
| 35 |
-
type="filepath",
|
| 36 |
-
label="π€ Record or Upload Audio (.wav)"
|
| 37 |
-
),
|
| 38 |
outputs=[
|
| 39 |
gr.Textbox(label="π Transcription"),
|
| 40 |
gr.Textbox(label="β
Grammar Score"),
|
| 41 |
-
gr.Textbox(label="βοΈ
|
| 42 |
],
|
| 43 |
title="ποΈ Voice Grammar Scorer",
|
| 44 |
-
description="Record or upload
|
| 45 |
)
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|
| 48 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
|
|
|
|
| 3 |
|
| 4 |
+
# Load Whisper for ASR
|
| 5 |
asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-large-v3")
|
| 6 |
|
| 7 |
+
# Load Grammar Scoring Model (CoLA)
|
| 8 |
cola_model = AutoModelForSequenceClassification.from_pretrained("textattack/roberta-base-CoLA")
|
| 9 |
cola_tokenizer = AutoTokenizer.from_pretrained("textattack/roberta-base-CoLA")
|
| 10 |
grammar_pipeline = pipeline("text-classification", model=cola_model, tokenizer=cola_tokenizer)
|
| 11 |
|
| 12 |
+
# Load Grammar Correction Model (T5)
|
| 13 |
correction_pipeline = pipeline("text2text-generation", model="vennify/t5-base-grammar-correction")
|
| 14 |
|
| 15 |
+
def process_audio(audio):
|
| 16 |
+
if audio is None:
|
| 17 |
+
return "No audio provided.", "", ""
|
| 18 |
|
| 19 |
+
# Step 1: Transcription
|
| 20 |
+
transcription = asr_pipeline(audio)["text"]
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Step 2: Grammar Scoring
|
| 23 |
+
score_output = grammar_pipeline(transcription)[0]
|
| 24 |
+
label = score_output["label"]
|
| 25 |
+
confidence = score_output["score"]
|
| 26 |
|
| 27 |
+
# Step 3: Grammar Correction
|
| 28 |
+
corrected = correction_pipeline(transcription, max_length=128)[0]["generated_text"]
|
| 29 |
|
| 30 |
+
return transcription, f"{label} ({confidence:.2f})", corrected
|
| 31 |
+
|
| 32 |
+
demo = gr.Interface(
|
| 33 |
fn=process_audio,
|
| 34 |
+
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="π€ Speak or Upload Audio (.wav)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
outputs=[
|
| 36 |
gr.Textbox(label="π Transcription"),
|
| 37 |
gr.Textbox(label="β
Grammar Score"),
|
| 38 |
+
gr.Textbox(label="βοΈ Grammar Correction")
|
| 39 |
],
|
| 40 |
title="ποΈ Voice Grammar Scorer",
|
| 41 |
+
description="Record or upload a WAV file. This app transcribes your voice, scores its grammar, and suggests corrections.",
|
| 42 |
)
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
| 45 |
+
demo.launch()
|