Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 3 |
+
from textblob import TextBlob
|
| 4 |
+
import language_tool_python
|
| 5 |
+
import speech_recognition as sr
|
| 6 |
+
import pyttsx3
|
| 7 |
+
from gtts import gTTS
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Initialize text-to-speech engine
|
| 11 |
+
engine = pyttsx3.init()
|
| 12 |
+
|
| 13 |
+
# Initialize speech recognition
|
| 14 |
+
recognizer = sr.Recognizer()
|
| 15 |
+
|
| 16 |
+
# Load pre-trained model and tokenizer
|
| 17 |
+
model_name = "gpt2"
|
| 18 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
| 19 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
| 20 |
+
|
| 21 |
+
# Function to generate a question based on a prompt
|
| 22 |
+
def generate_question(prompt):
|
| 23 |
+
inputs = tokenizer.encode(prompt, return_tensors='pt')
|
| 24 |
+
outputs = model.generate(inputs, max_length=50, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
|
| 25 |
+
question = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 26 |
+
return question
|
| 27 |
+
|
| 28 |
+
# Function to get voice input and convert to text
|
| 29 |
+
def get_voice_input(prompt):
|
| 30 |
+
# Use TTS to say the prompt
|
| 31 |
+
engine.say(prompt)
|
| 32 |
+
engine.runAndWait()
|
| 33 |
+
|
| 34 |
+
with sr.Microphone() as source:
|
| 35 |
+
print("Listening...")
|
| 36 |
+
audio = recognizer.listen(source)
|
| 37 |
+
try:
|
| 38 |
+
text = recognizer.recognize_google(audio)
|
| 39 |
+
print(f"User said: {text}")
|
| 40 |
+
return text
|
| 41 |
+
except sr.UnknownValueError:
|
| 42 |
+
print("Sorry, I did not understand that.")
|
| 43 |
+
return None
|
| 44 |
+
except sr.RequestError:
|
| 45 |
+
print("Sorry, there was an error with the speech recognition service.")
|
| 46 |
+
return None
|
| 47 |
+
|
| 48 |
+
# Function to convert text to speech
|
| 49 |
+
def text_to_speech(text):
|
| 50 |
+
tts = gTTS(text=text, lang='en')
|
| 51 |
+
tts.save("response.mp3")
|
| 52 |
+
os.system("start response.mp3") # For Windows; use "afplay response.mp3" on macOS
|
| 53 |
+
|
| 54 |
+
# Analyze English using TextBlob
|
| 55 |
+
def analyze_english_textblob(text):
|
| 56 |
+
blob = TextBlob(text)
|
| 57 |
+
corrected_text = blob.correct().string
|
| 58 |
+
sentiment = blob.sentiment.polarity # Range: -1 (negative) to 1 (positive)
|
| 59 |
+
|
| 60 |
+
feedback = "No significant grammar issues detected."
|
| 61 |
+
if corrected_text != text:
|
| 62 |
+
feedback = "Grammar issues detected."
|
| 63 |
+
|
| 64 |
+
# Simple scoring based on sentiment and correction
|
| 65 |
+
score = 100
|
| 66 |
+
if feedback == "Grammar issues detected.":
|
| 67 |
+
score -= 20 # Deduct points for grammar issues
|
| 68 |
+
|
| 69 |
+
# Adjust score based on sentiment (optional)
|
| 70 |
+
score += int((sentiment + 1) * 10) # Normalize sentiment from range [-1,1] to [0, 10]
|
| 71 |
+
score = min(max(score, 0), 100) # Ensure score is between 0 and 100
|
| 72 |
+
|
| 73 |
+
return feedback, score
|
| 74 |
+
|
| 75 |
+
# Analyze English using LanguageTool
|
| 76 |
+
def analyze_english_languagetool(text):
|
| 77 |
+
tool = language_tool_python.LanguageTool('en-US')
|
| 78 |
+
matches = tool.check(text)
|
| 79 |
+
|
| 80 |
+
feedback = "No issues found."
|
| 81 |
+
issues = []
|
| 82 |
+
|
| 83 |
+
if matches:
|
| 84 |
+
feedback = f"Issues detected: {len(matches)}"
|
| 85 |
+
for match in matches:
|
| 86 |
+
issues.append(f"{match.ruleId}: {match.message} (Suggestion: {match.replacements})")
|
| 87 |
+
|
| 88 |
+
# Simple scoring based on number of issues
|
| 89 |
+
score = 100
|
| 90 |
+
if len(matches) > 0:
|
| 91 |
+
score -= len(matches) * 2 # Deduct points for each issue
|
| 92 |
+
|
| 93 |
+
return feedback, issues, max(score, 0) # Ensure score is not below 0
|
| 94 |
+
|
| 95 |
+
# Main process
|
| 96 |
+
def main():
|
| 97 |
+
prompt = "Ask a question about grammar or general knowledge."
|
| 98 |
+
question = generate_question(prompt)
|
| 99 |
+
print("Generated Question:", question)
|
| 100 |
+
|
| 101 |
+
text_to_speech(f"Question: {question}")
|
| 102 |
+
user_answer = get_voice_input(question)
|
| 103 |
+
|
| 104 |
+
if user_answer:
|
| 105 |
+
# Limit response to 30 words
|
| 106 |
+
user_answer = ' '.join(user_answer.split()[:30])
|
| 107 |
+
|
| 108 |
+
# Analyze the response
|
| 109 |
+
feedback_textblob, score_textblob = analyze_english_textblob(user_answer)
|
| 110 |
+
feedback_languagetool, issues, score_languagetool = analyze_english_languagetool(user_answer)
|
| 111 |
+
|
| 112 |
+
# Calculate average score from both tools
|
| 113 |
+
average_score = (score_textblob + score_languagetool) / 2
|
| 114 |
+
|
| 115 |
+
# Provide feedback
|
| 116 |
+
feedback = f"{feedback_textblob}. {feedback_languagetool}."
|
| 117 |
+
text_to_speech(f"Your response: {user_answer}. {feedback}. Your score is {average_score}.")
|
| 118 |
+
|
| 119 |
+
# Print detected issues
|
| 120 |
+
print("\nDetected Issues:")
|
| 121 |
+
for issue in issues:
|
| 122 |
+
print(f" - {issue}")
|
| 123 |
+
print("\nAverage Score:", average_score)
|
| 124 |
+
else:
|
| 125 |
+
text_to_speech("Sorry, I did not catch that. Please try again.")
|
| 126 |
+
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
main()
|