Spaces:
Sleeping
Sleeping
Update grammar_chatbot.py
Browse files- grammar_chatbot.py +166 -158
grammar_chatbot.py
CHANGED
|
@@ -1,159 +1,167 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
self.
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
"
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
"
|
| 43 |
-
"
|
| 44 |
-
"What's
|
| 45 |
-
"Do you
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
"
|
| 50 |
-
"
|
| 51 |
-
"Do you prefer
|
| 52 |
-
"What's
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
"What's
|
| 57 |
-
"Do you
|
| 58 |
-
"What's
|
| 59 |
-
"
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
"
|
| 64 |
-
"
|
| 65 |
-
"
|
| 66 |
-
"
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
"
|
| 71 |
-
"
|
| 72 |
-
"
|
| 73 |
-
"What'
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
)
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
self.
|
| 149 |
-
self.
|
| 150 |
-
|
| 151 |
-
return
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
return self.conversation_history
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
# Set cache dirs (must match Dockerfile env vars)
|
| 4 |
+
os.environ['HOME'] = '/app'
|
| 5 |
+
os.environ['HF_HOME'] = '/app/.hf_cache'
|
| 6 |
+
os.environ['LANGTOOL_HOME'] = '/app/.ltool_cache'
|
| 7 |
+
os.environ['XDG_CACHE_HOME'] = '/app/.cache'
|
| 8 |
+
|
| 9 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 10 |
+
import torch
|
| 11 |
+
import random
|
| 12 |
+
from typing import Dict, List
|
| 13 |
+
|
| 14 |
+
class GrammarChatbot:
|
| 15 |
+
def __init__(self):
|
| 16 |
+
"""Initialize models for fluent corrections"""
|
| 17 |
+
# Initialize models
|
| 18 |
+
self.grammar_model = pipeline(
|
| 19 |
+
"text2text-generation",
|
| 20 |
+
model="vennify/t5-base-grammar-correction",
|
| 21 |
+
device=0 if torch.cuda.is_available() else -1
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# Fluent paraphrasing model
|
| 25 |
+
self.paraphrase_tokenizer = AutoTokenizer.from_pretrained("humarin/chatgpt_paraphraser_on_T5_base")
|
| 26 |
+
self.paraphrase_model = AutoModelForSeq2SeqLM.from_pretrained("humarin/chatgpt_paraphraser_on_T5_base")
|
| 27 |
+
|
| 28 |
+
# Enhanced conversation components
|
| 29 |
+
self.compliments = [
|
| 30 |
+
"Great job! Your English is improving!",
|
| 31 |
+
"Nice improvement! Keep it up!",
|
| 32 |
+
"You're doing well with your practice!",
|
| 33 |
+
"Good effort! I can see you're trying hard!",
|
| 34 |
+
"Excellent try! You're getting better!",
|
| 35 |
+
"Well done! That was much better!",
|
| 36 |
+
"Impressive! Your sentence structure is improving!"
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
# Organized question bank by categories
|
| 40 |
+
self.question_categories = {
|
| 41 |
+
"daily_life": [
|
| 42 |
+
"What did you do this morning?",
|
| 43 |
+
"How do you usually spend your evenings?",
|
| 44 |
+
"What's your morning routine like?",
|
| 45 |
+
"Do you have any plans for this weekend?",
|
| 46 |
+
"What time do you usually wake up?"
|
| 47 |
+
],
|
| 48 |
+
"hobbies": [
|
| 49 |
+
"What hobbies do you enjoy in your free time?",
|
| 50 |
+
"Have you picked up any new hobbies recently?",
|
| 51 |
+
"Do you prefer indoor or outdoor activities?",
|
| 52 |
+
"What's something you've always wanted to try?",
|
| 53 |
+
"Do you play any musical instruments?"
|
| 54 |
+
],
|
| 55 |
+
"food": [
|
| 56 |
+
"What's your favorite comfort food?",
|
| 57 |
+
"Do you enjoy cooking? What's your specialty?",
|
| 58 |
+
"What's the most unusual food you've ever tried?",
|
| 59 |
+
"Do you prefer sweet or savory snacks?",
|
| 60 |
+
"What's your go-to breakfast?"
|
| 61 |
+
],
|
| 62 |
+
"travel": [
|
| 63 |
+
"If you could visit any country, where would you go?",
|
| 64 |
+
"What's the most beautiful place you've ever seen?",
|
| 65 |
+
"Do you prefer beach vacations or city trips?",
|
| 66 |
+
"What's your favorite travel memory?",
|
| 67 |
+
"What's the next place you'd like to visit?"
|
| 68 |
+
],
|
| 69 |
+
"technology": [
|
| 70 |
+
"How do you use technology in your daily life?",
|
| 71 |
+
"What's your opinion about social media?",
|
| 72 |
+
"Do you think AI will change our lives significantly?",
|
| 73 |
+
"What tech gadget couldn't you live without?",
|
| 74 |
+
"How has technology changed your work/studies?"
|
| 75 |
+
],
|
| 76 |
+
"future": [
|
| 77 |
+
"Where do you see yourself in five years?",
|
| 78 |
+
"What's one skill you'd like to learn?",
|
| 79 |
+
"Do you have any big goals for this year?",
|
| 80 |
+
"What would your perfect day look like?",
|
| 81 |
+
"What's something you want to achieve?"
|
| 82 |
+
]
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
self.current_question = None
|
| 86 |
+
self.current_category = None
|
| 87 |
+
self.conversation_history = []
|
| 88 |
+
|
| 89 |
+
def correct_grammar(self, text: str) -> str:
|
| 90 |
+
"""Basic grammar correction"""
|
| 91 |
+
result = self.grammar_model(
|
| 92 |
+
text,
|
| 93 |
+
max_length=256,
|
| 94 |
+
num_beams=4,
|
| 95 |
+
early_stopping=True
|
| 96 |
+
)
|
| 97 |
+
return result[0]['generated_text']
|
| 98 |
+
|
| 99 |
+
def fluent_correction(self, text: str) -> str:
|
| 100 |
+
"""Create fluent, natural rewrites"""
|
| 101 |
+
input_ids = self.paraphrase_tokenizer(
|
| 102 |
+
f"paraphrase: {text}",
|
| 103 |
+
return_tensors="pt",
|
| 104 |
+
max_length=256,
|
| 105 |
+
truncation=True
|
| 106 |
+
).input_ids
|
| 107 |
+
|
| 108 |
+
outputs = self.paraphrase_model.generate(
|
| 109 |
+
input_ids,
|
| 110 |
+
temperature=0.7,
|
| 111 |
+
max_length=256,
|
| 112 |
+
num_beams=5,
|
| 113 |
+
early_stopping=True
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
return self.paraphrase_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 117 |
+
|
| 118 |
+
def _get_next_question(self) -> str:
|
| 119 |
+
"""Select next question based on conversation context"""
|
| 120 |
+
# If we're continuing a category, use follow-up questions
|
| 121 |
+
if self.current_category and random.random() < 0.6: # 60% chance to stay on topic
|
| 122 |
+
return random.choice(self.question_categories[self.current_category])
|
| 123 |
+
|
| 124 |
+
# Otherwise select a new random category
|
| 125 |
+
self.current_category = random.choice(list(self.question_categories.keys()))
|
| 126 |
+
return random.choice(self.question_categories[self.current_category])
|
| 127 |
+
|
| 128 |
+
def generate_response(self, user_input: str) -> Dict:
|
| 129 |
+
"""Generate a conversational response"""
|
| 130 |
+
# Store conversation history
|
| 131 |
+
self.conversation_history.append(("user", user_input))
|
| 132 |
+
|
| 133 |
+
# Correct the input
|
| 134 |
+
corrected = self.fluent_correction(user_input)
|
| 135 |
+
is_corrected = corrected.lower() != user_input.lower()
|
| 136 |
+
|
| 137 |
+
# Generate response
|
| 138 |
+
response = {
|
| 139 |
+
"original_text": user_input,
|
| 140 |
+
"corrected_text": corrected,
|
| 141 |
+
"is_corrected": is_corrected,
|
| 142 |
+
"compliment": random.choice(self.compliments) if is_corrected else "",
|
| 143 |
+
"next_question": self._get_next_question(),
|
| 144 |
+
"end_conversation": False
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
# Update state
|
| 148 |
+
self.current_question = response["next_question"]
|
| 149 |
+
self.conversation_history.append(("bot", response["next_question"]))
|
| 150 |
+
|
| 151 |
+
return response
|
| 152 |
+
|
| 153 |
+
def start_conversation(self) -> Dict:
|
| 154 |
+
"""Initialize a new conversation"""
|
| 155 |
+
self.conversation_history = []
|
| 156 |
+
self.current_category = random.choice(list(self.question_categories.keys()))
|
| 157 |
+
self.current_question = random.choice(self.question_categories[self.current_category])
|
| 158 |
+
|
| 159 |
+
return {
|
| 160 |
+
"message": "Hello! I'm your English practice partner. Let's chat!",
|
| 161 |
+
"next_question": self.current_question,
|
| 162 |
+
"end_conversation": False
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
def get_conversation_history(self) -> List[tuple]:
|
| 166 |
+
"""Get the complete conversation history"""
|
| 167 |
return self.conversation_history
|