Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|