Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Create app.py
Browse files
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,69 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import torch
         | 
| 2 | 
            +
            import gradio as gr
         | 
| 3 | 
            +
            import speech_recognition as sr
         | 
| 4 | 
            +
            import pyttsx3
         | 
| 5 | 
            +
            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)
         | 
| 12 | 
            +
                model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
         | 
| 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:
         | 
| 19 | 
            +
                    audio_data = recognizer.record(source)
         | 
| 20 | 
            +
                try:
         | 
| 21 | 
            +
                    text = recognizer.recognize_google(audio_data)
         | 
| 22 | 
            +
                    return text
         | 
| 23 | 
            +
                except sr.UnknownValueError:
         | 
| 24 | 
            +
                    return "Could not understand the audio."
         | 
| 25 | 
            +
                except sr.RequestError as e:
         | 
| 26 | 
            +
                    return f"Speech recognition error: {e}"
         | 
| 27 | 
            +
             | 
| 28 | 
            +
            def correct_grammar(text, tokenizer, model, happy_tt):
         | 
| 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)
         | 
| 32 | 
            +
                corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
         | 
| 33 | 
            +
                
         | 
| 34 | 
            +
                args = TTSettings(num_beams=5, min_length=1)
         | 
| 35 | 
            +
                correction = happy_tt.generate_text("gec: " + text, args=args).text  # Better correction method
         | 
| 36 | 
            +
                grammar_score = 100 - abs(len(text) - len(correction))  # Scoring based on text change ratio
         | 
| 37 | 
            +
                
         | 
| 38 | 
            +
                return corrected_text, grammar_score, correction
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            def gradio_interface(text):
         | 
| 41 | 
            +
                tokenizer, model, happy_tt = load_models()
         | 
| 42 | 
            +
                corrected_text, grammar_score, correction = correct_grammar(text, tokenizer, model, happy_tt)
         | 
| 43 | 
            +
                return corrected_text, grammar_score, correction
         | 
| 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=gradio_interface,
         | 
| 52 | 
            +
                    inputs=gr.Textbox(placeholder="Enter a sentence..."),
         | 
| 53 | 
            +
                    outputs=["text", "number", "text"],
         | 
| 54 | 
            +
                    title="AI Grammar Checker",
         | 
| 55 | 
            +
                    description="Enter text to check grammar, get suggestions, and see a score."
         | 
| 56 | 
            +
                )
         | 
| 57 | 
            +
                
         | 
| 58 | 
            +
                audio_iface = gr.Interface(
         | 
| 59 | 
            +
                    fn=gradio_audio_interface,
         | 
| 60 | 
            +
                    inputs=gr.Audio(source="microphone", type="filepath"),
         | 
| 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()
         |