mayankpuvvala commited on
Commit
4b14914
Β·
verified Β·
1 Parent(s): 5d66bfc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -25
app.py CHANGED
@@ -1,48 +1,45 @@
1
  import gradio as gr
2
- import torch
3
- from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
4
 
5
- # Load Whisper for transcription
6
  asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-large-v3")
7
 
8
- # Load grammar scoring model
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 grammar correction model
14
  correction_pipeline = pipeline("text2text-generation", model="vennify/t5-base-grammar-correction")
15
 
16
- def process_audio(audio_path):
17
- # Transcribe
18
- transcription = asr_pipeline(audio_path)["text"]
19
 
20
- # Score grammar
21
- grammar_result = grammar_pipeline(transcription)[0]
22
- score_label = grammar_result["label"]
23
- score_confidence = grammar_result["score"]
24
 
25
- # Suggest correction
26
- corrected_text = correction_pipeline(transcription, max_length=128)[0]["generated_text"]
 
 
27
 
28
- return transcription, f"{score_label} ({score_confidence:.2f})", corrected_text
 
29
 
30
- # Gradio Interface
31
- interface = gr.Interface(
 
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="✍️ Suggested Correction")
42
  ],
43
  title="πŸŽ™οΈ Voice Grammar Scorer",
44
- description="Record or upload your voice (.wav). This app transcribes it, scores grammar, and suggests corrections."
45
  )
46
 
47
  if __name__ == "__main__":
48
- interface.launch()
 
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()