Update response_generator.py
Browse files- response_generator.py +98 -178
response_generator.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
# response_generator.py
|
| 2 |
-
import
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 4 |
from utils import setup_logger
|
| 5 |
from config import Config
|
| 6 |
|
|
@@ -9,213 +8,134 @@ logger = setup_logger('response_generator')
|
|
| 9 |
class ResponseGenerator:
|
| 10 |
def __init__(self):
|
| 11 |
"""
|
| 12 |
-
|
| 13 |
"""
|
| 14 |
-
|
| 15 |
-
model_name = "aubmindlab/aragpt2-base" # Free Arabic GPT model
|
| 16 |
-
|
| 17 |
-
try:
|
| 18 |
-
logger.info(f"Loading Arabic generation model: {model_name}")
|
| 19 |
-
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 20 |
-
self.model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 21 |
-
|
| 22 |
-
# Set pad token
|
| 23 |
-
if self.tokenizer.pad_token is None:
|
| 24 |
-
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 25 |
-
|
| 26 |
-
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 27 |
-
self.model.to(self.device)
|
| 28 |
-
logger.info(f"Model loaded successfully on {self.device}")
|
| 29 |
-
self.use_generation = True
|
| 30 |
-
|
| 31 |
-
except Exception as e:
|
| 32 |
-
logger.warning(f"Could not load generation model: {e}")
|
| 33 |
-
self.use_generation = False
|
| 34 |
-
self.tokenizer = None
|
| 35 |
-
self.model = None
|
| 36 |
|
| 37 |
def generate_response(self, query, relevant_docs):
|
| 38 |
"""
|
| 39 |
-
Generate
|
| 40 |
"""
|
| 41 |
try:
|
| 42 |
if len(relevant_docs) == 0:
|
| 43 |
return "عذرًا، لم أجد أي معلومات ذات صلة في المستندات."
|
| 44 |
|
| 45 |
-
|
| 46 |
-
if self.use_generation and self.model is not None:
|
| 47 |
-
return self._generate_smart_answer(query, relevant_docs)
|
| 48 |
-
else:
|
| 49 |
-
# Fallback: smart extraction
|
| 50 |
-
return self._generate_extractive_answer(query, relevant_docs)
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
logger.error(f"Error generating response: {e}")
|
| 54 |
-
return "عذرًا، لم أتمكن من إنشاء استجابة
|
| 55 |
|
| 56 |
-
def
|
| 57 |
"""
|
| 58 |
-
|
| 59 |
"""
|
| 60 |
-
# Get top
|
| 61 |
top_chunks = relevant_docs.head(3)
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
for idx, row in top_chunks.iterrows():
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
combined_context = combined_context[:400]
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
السؤال: {query}
|
| 78 |
-
|
| 79 |
-
الإجابة المختصرة:"""
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
truncation=True,
|
| 88 |
-
padding=True
|
| 89 |
-
).to(self.device)
|
| 90 |
-
|
| 91 |
-
# Generate answer
|
| 92 |
-
with torch.no_grad():
|
| 93 |
-
outputs = self.model.generate(
|
| 94 |
-
inputs['input_ids'],
|
| 95 |
-
attention_mask=inputs['attention_mask'],
|
| 96 |
-
max_new_tokens=100, # Generate max 100 new tokens
|
| 97 |
-
min_length=20, # Minimum length
|
| 98 |
-
num_beams=3, # Beam search for better quality
|
| 99 |
-
no_repeat_ngram_size=3, # Avoid repetition
|
| 100 |
-
do_sample=True, # Sampling for diversity
|
| 101 |
-
top_k=50,
|
| 102 |
-
top_p=0.9,
|
| 103 |
-
temperature=0.7,
|
| 104 |
-
pad_token_id=self.tokenizer.pad_token_id,
|
| 105 |
-
eos_token_id=self.tokenizer.eos_token_id
|
| 106 |
-
)
|
| 107 |
-
|
| 108 |
-
# Decode the answer
|
| 109 |
-
full_response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 110 |
-
|
| 111 |
-
# Extract only the generated answer (remove the prompt)
|
| 112 |
-
if "الإجابة المختصرة:" in full_response:
|
| 113 |
-
answer = full_response.split("الإجابة المختصرة:")[-1].strip()
|
| 114 |
else:
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
---
|
| 128 |
-
**عدد المصادر:** {len(top_chunks)} chunks"""
|
| 129 |
-
|
| 130 |
-
if len(scores) > 0:
|
| 131 |
-
response += f" | **دقة المطابقة:** {scores[-1]:.0%} - {scores[0]:.0%}"
|
| 132 |
-
|
| 133 |
-
logger.info(f"Generated answer length: {len(answer)} chars")
|
| 134 |
-
|
| 135 |
-
return response
|
| 136 |
-
|
| 137 |
-
except Exception as e:
|
| 138 |
-
logger.error(f"Generation failed: {e}")
|
| 139 |
-
# Fallback to extractive method
|
| 140 |
-
return self._generate_extractive_answer(query, relevant_docs)
|
| 141 |
|
| 142 |
-
def
|
| 143 |
"""
|
| 144 |
-
|
| 145 |
"""
|
| 146 |
-
# Remove
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
# Remove extra whitespace
|
| 154 |
-
answer = ' '.join(answer.split())
|
| 155 |
-
|
| 156 |
-
# Limit length
|
| 157 |
-
if len(answer) > 500:
|
| 158 |
-
answer = answer[:500]
|
| 159 |
-
# Try to end at sentence
|
| 160 |
-
last_period = max(answer.rfind('.'), answer.rfind('؟'))
|
| 161 |
-
if last_period > 200:
|
| 162 |
-
answer = answer[:last_period + 1]
|
| 163 |
|
| 164 |
-
return
|
| 165 |
|
| 166 |
-
def
|
| 167 |
"""
|
| 168 |
-
|
| 169 |
"""
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
-
|
| 179 |
-
content = row['content']
|
| 180 |
-
score = row.get('similarity_score', 0)
|
| 181 |
-
|
| 182 |
-
# Boost score if query keywords appear in content
|
| 183 |
-
content_lower = content.lower()
|
| 184 |
-
keyword_matches = sum(1 for word in query_lower.split() if len(word) > 2 and word in content_lower)
|
| 185 |
-
adjusted_score = score + (keyword_matches * 0.05)
|
| 186 |
-
|
| 187 |
-
if adjusted_score > best_score:
|
| 188 |
-
best_score = adjusted_score
|
| 189 |
-
best_chunk = content
|
| 190 |
|
| 191 |
-
|
| 192 |
-
if best_chunk:
|
| 193 |
-
# Truncate if too long
|
| 194 |
-
if len(best_chunk) > 400:
|
| 195 |
-
best_chunk = best_chunk[:400]
|
| 196 |
-
# Find sentence end
|
| 197 |
-
last_period = max(best_chunk.rfind('.'), best_chunk.rfind('؟'))
|
| 198 |
-
if last_period > 100:
|
| 199 |
-
best_chunk = best_chunk[:last_period + 1]
|
| 200 |
-
|
| 201 |
-
response = f"""**الإجابة:**
|
| 202 |
-
|
| 203 |
-
{best_chunk}
|
| 204 |
|
| 205 |
-
|
| 206 |
-
**المصدر:** {len(top_chunks)} chunks | **المطابقة:** {best_score:.0%}"""
|
| 207 |
-
|
| 208 |
-
return response
|
| 209 |
-
|
| 210 |
-
return "عذرًا، لم أتمكن من إيجاد إجابة مناسبة."
|
| 211 |
-
|
| 212 |
-
def update_model(self, new_model_name):
|
| 213 |
-
try:
|
| 214 |
-
self.tokenizer = AutoTokenizer.from_pretrained(new_model_name)
|
| 215 |
-
self.model = AutoModelForCausalLM.from_pretrained(new_model_name)
|
| 216 |
-
if self.tokenizer.pad_token is None:
|
| 217 |
-
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 218 |
-
self.model.to(self.device)
|
| 219 |
-
logger.info(f"Model updated to {new_model_name}")
|
| 220 |
-
except Exception as e:
|
| 221 |
-
logger.error(f"Error updating model: {e}")
|
|
|
|
| 1 |
# response_generator.py
|
| 2 |
+
import re
|
|
|
|
| 3 |
from utils import setup_logger
|
| 4 |
from config import Config
|
| 5 |
|
|
|
|
| 8 |
class ResponseGenerator:
|
| 9 |
def __init__(self):
|
| 10 |
"""
|
| 11 |
+
Smart extraction without complex LLM
|
| 12 |
"""
|
| 13 |
+
logger.info("Response generator initialized (extractive mode)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def generate_response(self, query, relevant_docs):
|
| 16 |
"""
|
| 17 |
+
Generate smart answer by extracting and summarizing
|
| 18 |
"""
|
| 19 |
try:
|
| 20 |
if len(relevant_docs) == 0:
|
| 21 |
return "عذرًا، لم أجد أي معلومات ذات صلة في المستندات."
|
| 22 |
|
| 23 |
+
return self._generate_smart_extractive_answer(query, relevant_docs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
except Exception as e:
|
| 26 |
logger.error(f"Error generating response: {e}")
|
| 27 |
+
return "عذرًا، لم أتمكن من إنشاء استجابة."
|
| 28 |
|
| 29 |
+
def _generate_smart_extractive_answer(self, query, relevant_docs):
|
| 30 |
"""
|
| 31 |
+
Smart extraction with sentence selection
|
| 32 |
"""
|
| 33 |
+
# Get top chunks
|
| 34 |
top_chunks = relevant_docs.head(3)
|
| 35 |
|
| 36 |
+
# Extract query keywords
|
| 37 |
+
query_keywords = self._extract_keywords(query)
|
| 38 |
+
|
| 39 |
+
# Find best sentences
|
| 40 |
+
best_sentences = []
|
| 41 |
+
|
| 42 |
for idx, row in top_chunks.iterrows():
|
| 43 |
+
content = row['content']
|
| 44 |
+
similarity = row.get('similarity_score', 0)
|
| 45 |
+
|
| 46 |
+
# Split into sentences
|
| 47 |
+
sentences = self._split_sentences(content)
|
| 48 |
+
|
| 49 |
+
# Score each sentence
|
| 50 |
+
for sentence in sentences:
|
| 51 |
+
if len(sentence.strip()) < 20: # Skip very short sentences
|
| 52 |
+
continue
|
| 53 |
+
|
| 54 |
+
# Calculate sentence score
|
| 55 |
+
score = similarity
|
| 56 |
+
|
| 57 |
+
# Boost if contains query keywords
|
| 58 |
+
sentence_lower = sentence.lower()
|
| 59 |
+
for keyword in query_keywords:
|
| 60 |
+
if keyword in sentence_lower:
|
| 61 |
+
score += 0.1
|
| 62 |
+
|
| 63 |
+
best_sentences.append({
|
| 64 |
+
'text': sentence.strip(),
|
| 65 |
+
'score': score,
|
| 66 |
+
'chunk_id': idx
|
| 67 |
+
})
|
| 68 |
|
| 69 |
+
# Sort by score
|
| 70 |
+
best_sentences.sort(key=lambda x: x['score'], reverse=True)
|
| 71 |
|
| 72 |
+
# Take top 3 sentences
|
| 73 |
+
top_sentences = best_sentences[:3]
|
|
|
|
| 74 |
|
| 75 |
+
# Format answer
|
| 76 |
+
if not top_sentences:
|
| 77 |
+
return self._format_simple_answer(top_chunks)
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
# Build answer
|
| 80 |
+
answer_parts = ["**الإجابة:**\n"]
|
| 81 |
+
|
| 82 |
+
for i, sent in enumerate(top_sentences, 1):
|
| 83 |
+
if len(top_sentences) > 1:
|
| 84 |
+
answer_parts.append(f"\n**[{i}]** {sent['text']}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
else:
|
| 86 |
+
answer_parts.append(f"\n{sent['text']}")
|
| 87 |
+
|
| 88 |
+
# Add metadata
|
| 89 |
+
answer_parts.append("\n\n---")
|
| 90 |
+
|
| 91 |
+
scores = top_chunks['similarity_score'].values if 'similarity_score' in top_chunks.columns else []
|
| 92 |
+
answer_parts.append(f"**عدد المصادر:** {len(top_chunks)} chunks")
|
| 93 |
+
|
| 94 |
+
if len(scores) > 0:
|
| 95 |
+
answer_parts.append(f" | **دقة المطابقة:** {scores[-1]:.0%} - {scores[0]:.0%}")
|
| 96 |
+
|
| 97 |
+
return "\n".join(answer_parts)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
def _extract_keywords(self, query):
|
| 100 |
"""
|
| 101 |
+
Extract meaningful keywords from query
|
| 102 |
"""
|
| 103 |
+
# Remove stop words
|
| 104 |
+
stop_words = {'ما', 'هي', 'هو', 'في', 'من', 'إلى', 'على', 'عن', 'ال', 'و', 'أو'}
|
| 105 |
+
|
| 106 |
+
# Split and clean
|
| 107 |
+
words = query.lower().split()
|
| 108 |
+
keywords = [w for w in words if len(w) > 2 and w not in stop_words]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
return keywords
|
| 111 |
|
| 112 |
+
def _split_sentences(self, text):
|
| 113 |
"""
|
| 114 |
+
Split text into sentences
|
| 115 |
"""
|
| 116 |
+
# Split by Arabic and English punctuation
|
| 117 |
+
sentences = re.split(r'[.؟!]\s+', text)
|
| 118 |
+
return [s.strip() for s in sentences if s.strip()]
|
| 119 |
+
|
| 120 |
+
def _format_simple_answer(self, top_chunks):
|
| 121 |
+
"""
|
| 122 |
+
Fallback: show top chunk
|
| 123 |
+
"""
|
| 124 |
+
best_chunk = top_chunks.iloc[0]
|
| 125 |
+
content = best_chunk['content']
|
| 126 |
|
| 127 |
+
# Truncate if too long
|
| 128 |
+
if len(content) > 400:
|
| 129 |
+
content = content[:400]
|
| 130 |
+
# Try to end at sentence
|
| 131 |
+
last_period = max(content.rfind('.'), content.rfind('؟'), content.rfind('!'))
|
| 132 |
+
if last_period > 100:
|
| 133 |
+
content = content[:last_period + 1]
|
| 134 |
+
else:
|
| 135 |
+
content += "..."
|
| 136 |
|
| 137 |
+
score = best_chunk.get('similarity_score', 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
+
return f"""**الإجابة:**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
{content}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|