Update app.py
Browse files
app.py
CHANGED
|
@@ -8,94 +8,81 @@ import random
|
|
| 8 |
import hashlib
|
| 9 |
from datetime import datetime
|
| 10 |
from collections import defaultdict, Counter
|
| 11 |
-
import pickle
|
| 12 |
-
import os
|
| 13 |
-
import threading
|
| 14 |
import time
|
| 15 |
|
| 16 |
class QuestionAnsweringAI:
|
| 17 |
def __init__(self):
|
| 18 |
# Token database e vocabulary
|
| 19 |
-
self.vocabulary = {}
|
| 20 |
-
self.token_to_id = {}
|
| 21 |
self.vocab_size = 0
|
| 22 |
|
| 23 |
-
# Neural Network
|
| 24 |
self.embedding_dim = 256
|
| 25 |
self.hidden_dim = 512
|
| 26 |
self.context_length = 32
|
| 27 |
|
| 28 |
-
# Knowledge
|
| 29 |
-
self.knowledge_base = defaultdict(list)
|
| 30 |
-
self.qa_patterns = defaultdict(list)
|
| 31 |
-
self.context_memory = []
|
| 32 |
|
| 33 |
-
#
|
| 34 |
self.embeddings = None
|
| 35 |
self.hidden_weights = None
|
| 36 |
self.output_weights = None
|
| 37 |
|
| 38 |
-
# Pattern
|
| 39 |
-
self.token_patterns = defaultdict(list)
|
| 40 |
self.bigram_counts = defaultdict(Counter)
|
| 41 |
self.trigram_counts = defaultdict(Counter)
|
| 42 |
-
self.sentence_starts = []
|
| 43 |
|
| 44 |
-
#
|
| 45 |
self.data_sources = {
|
| 46 |
"news_rss": [
|
| 47 |
"https://feeds.reuters.com/reuters/worldNews",
|
| 48 |
"https://feeds.bbci.co.uk/news/world/rss.xml",
|
| 49 |
-
"https://feeds.bbci.co.uk/news/science_and_environment/rss.xml",
|
| 50 |
"https://feeds.bbci.co.uk/news/technology/rss.xml"
|
| 51 |
-
]
|
| 52 |
-
"wikipedia_api": "https://en.wikipedia.org/api/rest_v1/page/random/summary",
|
| 53 |
-
"arxiv_rss": "http://export.arxiv.org/rss/cs"
|
| 54 |
}
|
| 55 |
|
| 56 |
-
# Training
|
| 57 |
self.total_tokens_collected = 0
|
| 58 |
self.epochs_trained = 0
|
| 59 |
self.learning_rate = 0.001
|
| 60 |
-
self.max_response_length =
|
| 61 |
|
| 62 |
self.initialize_network()
|
| 63 |
|
| 64 |
def initialize_network(self):
|
| 65 |
-
"""
|
| 66 |
-
self.embeddings = np.random.normal(0, 0.1, (
|
| 67 |
self.hidden_weights = np.random.normal(0, 0.1, (self.embedding_dim * self.context_length, self.hidden_dim))
|
| 68 |
self.hidden_bias = np.zeros(self.hidden_dim)
|
| 69 |
-
self.output_weights = np.random.normal(0, 0.1, (self.hidden_dim,
|
| 70 |
-
self.output_bias = np.zeros(
|
| 71 |
-
|
| 72 |
-
print("π§ Neural Network per Q&A inizializzato")
|
| 73 |
|
| 74 |
-
def
|
| 75 |
-
"""
|
| 76 |
-
print("π·οΈ
|
| 77 |
|
| 78 |
collected_texts = []
|
| 79 |
|
| 80 |
-
#
|
| 81 |
news_texts = self.scrape_news_feeds()
|
| 82 |
collected_texts.extend(news_texts)
|
| 83 |
-
print(f"π°
|
| 84 |
-
|
| 85 |
-
# 2. Wikipedia (per factual Q&A)
|
| 86 |
-
wiki_texts = self.scrape_wikipedia_content()
|
| 87 |
-
collected_texts.extend(wiki_texts)
|
| 88 |
-
print(f"π Raccolti {len(wiki_texts)} contenuti Wikipedia")
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
|
| 92 |
-
collected_texts.extend(
|
| 93 |
-
print(f"β
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
quality_texts =
|
| 97 |
|
| 98 |
-
#
|
| 99 |
all_tokens = []
|
| 100 |
for text in quality_texts:
|
| 101 |
tokens = self.tokenize_text(text)
|
|
@@ -104,18 +91,17 @@ class QuestionAnsweringAI:
|
|
| 104 |
break
|
| 105 |
|
| 106 |
self.total_tokens_collected = len(all_tokens)
|
| 107 |
-
print(f"π―
|
| 108 |
|
| 109 |
# Build systems
|
| 110 |
self.build_vocabulary(all_tokens)
|
| 111 |
-
self.extract_qa_patterns(quality_texts)
|
| 112 |
self.build_knowledge_base(quality_texts)
|
| 113 |
-
self.
|
| 114 |
|
| 115 |
return all_tokens
|
| 116 |
|
| 117 |
def scrape_news_feeds(self):
|
| 118 |
-
"""Scrape news
|
| 119 |
texts = []
|
| 120 |
|
| 121 |
for rss_url in self.data_sources["news_rss"]:
|
|
@@ -136,214 +122,47 @@ class QuestionAnsweringAI:
|
|
| 136 |
|
| 137 |
return texts
|
| 138 |
|
| 139 |
-
def scrape_wikipedia_content(self):
|
| 140 |
-
"""Scrape Wikipedia per factual knowledge"""
|
| 141 |
-
texts = []
|
| 142 |
-
|
| 143 |
-
try:
|
| 144 |
-
for i in range(5): # 5 articoli casuali
|
| 145 |
-
response = requests.get(self.data_sources["wikipedia_api"], timeout=5)
|
| 146 |
-
if response.status_code == 200:
|
| 147 |
-
data = response.json()
|
| 148 |
-
content = ""
|
| 149 |
-
if 'title' in data:
|
| 150 |
-
content += f"Topic: {data['title']}. "
|
| 151 |
-
if 'extract' in data:
|
| 152 |
-
content += data['extract']
|
| 153 |
-
if content:
|
| 154 |
-
texts.append(self.clean_text(content))
|
| 155 |
-
except:
|
| 156 |
-
pass
|
| 157 |
-
|
| 158 |
-
return texts
|
| 159 |
-
|
| 160 |
def create_qa_patterns(self):
|
| 161 |
-
"""
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
# Question templates
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
"answers": ["is located in", "The capital is", "is situated in"]
|
| 175 |
-
},
|
| 176 |
-
{
|
| 177 |
-
"questions": ["How does", "How do"],
|
| 178 |
-
"topics": ["computers work", "algorithms function", "neural networks learn"],
|
| 179 |
-
"answers": ["works by", "functions through", "operates using"]
|
| 180 |
-
},
|
| 181 |
-
{
|
| 182 |
-
"questions": ["Why is", "Why does"],
|
| 183 |
-
"topics": ["the sky blue", "water important", "education valuable"],
|
| 184 |
-
"answers": ["because of", "due to the fact that", "as a result of"]
|
| 185 |
-
}
|
| 186 |
]
|
| 187 |
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
for topic in template["topics"]:
|
| 192 |
-
for answer in template["answers"]:
|
| 193 |
-
qa_text = f"Question: {question} {topic}? Answer: {topic} {answer} various factors."
|
| 194 |
-
qa_patterns.append(qa_text)
|
| 195 |
-
|
| 196 |
-
return qa_patterns
|
| 197 |
-
|
| 198 |
-
def extract_qa_patterns(self, texts):
|
| 199 |
-
"""Estrae pattern Question-Answer dai testi"""
|
| 200 |
-
for text in texts:
|
| 201 |
-
# Cerca pattern di domande nei testi
|
| 202 |
-
question_patterns = re.findall(r'[^.]*\?[^.]*\.', text)
|
| 203 |
-
for pattern in question_patterns:
|
| 204 |
-
if len(pattern.split()) > 3: # Pattern abbastanza lunghi
|
| 205 |
-
question_type = self.classify_question(pattern)
|
| 206 |
-
self.qa_patterns[question_type].append(pattern)
|
| 207 |
-
|
| 208 |
-
def classify_question(self, text):
|
| 209 |
-
"""Classifica il tipo di domanda"""
|
| 210 |
-
text_lower = text.lower()
|
| 211 |
|
| 212 |
-
|
| 213 |
-
return 'definition'
|
| 214 |
-
elif any(word in text_lower for word in ['where', 'location']):
|
| 215 |
-
return 'location'
|
| 216 |
-
elif any(word in text_lower for word in ['how', 'method']):
|
| 217 |
-
return 'process'
|
| 218 |
-
elif any(word in text_lower for word in ['why', 'reason']):
|
| 219 |
-
return 'explanation'
|
| 220 |
-
elif any(word in text_lower for word in ['when', 'time']):
|
| 221 |
-
return 'temporal'
|
| 222 |
-
else:
|
| 223 |
-
return 'general'
|
| 224 |
-
|
| 225 |
-
def build_knowledge_base(self, texts):
|
| 226 |
-
"""Costruisce knowledge base dai testi"""
|
| 227 |
-
for text in texts:
|
| 228 |
-
# Estrai facts (frasi dichiarative)
|
| 229 |
-
sentences = re.split(r'[.!?]+', text)
|
| 230 |
-
for sentence in sentences:
|
| 231 |
-
sentence = sentence.strip()
|
| 232 |
-
if len(sentence) > 20 and not sentence.endswith('?'):
|
| 233 |
-
# Estrai topic principale
|
| 234 |
-
topic = self.extract_main_topic(sentence)
|
| 235 |
-
if topic:
|
| 236 |
-
self.knowledge_base[topic].append(sentence)
|
| 237 |
-
|
| 238 |
-
def extract_main_topic(self, sentence):
|
| 239 |
-
"""Estrae topic principale da una frase"""
|
| 240 |
-
# Semplice estrazione di named entities
|
| 241 |
-
words = sentence.split()
|
| 242 |
-
|
| 243 |
-
# Cerca nomi propri (capitalized words)
|
| 244 |
-
for word in words:
|
| 245 |
-
if word[0].isupper() and len(word) > 3:
|
| 246 |
-
return word.lower()
|
| 247 |
-
|
| 248 |
-
# Cerca keywords importanti
|
| 249 |
-
important_keywords = ['technology', 'science', 'politics', 'economy', 'climate', 'health']
|
| 250 |
-
for keyword in important_keywords:
|
| 251 |
-
if keyword in sentence.lower():
|
| 252 |
-
return keyword
|
| 253 |
-
|
| 254 |
-
return None
|
| 255 |
-
|
| 256 |
-
def extract_generation_patterns(self, tokens):
|
| 257 |
-
"""Estrae pattern per text generation"""
|
| 258 |
-
token_ids = [self.token_to_id.get(token, 1) for token in tokens]
|
| 259 |
-
|
| 260 |
-
# Extract patterns per generation
|
| 261 |
-
for i in range(len(token_ids) - 1):
|
| 262 |
-
current_token = token_ids[i]
|
| 263 |
-
next_token = token_ids[i + 1]
|
| 264 |
-
self.bigram_counts[current_token][next_token] += 1
|
| 265 |
-
|
| 266 |
-
for i in range(len(token_ids) - 2):
|
| 267 |
-
context = (token_ids[i], token_ids[i + 1])
|
| 268 |
-
next_token = token_ids[i + 2]
|
| 269 |
-
self.trigram_counts[context][next_token] += 1
|
| 270 |
-
|
| 271 |
-
# Trova sentence starters
|
| 272 |
-
sentences = ' '.join(tokens).split('.')
|
| 273 |
-
for sentence in sentences:
|
| 274 |
-
words = sentence.strip().split()
|
| 275 |
-
if len(words) > 2:
|
| 276 |
-
starter = ' '.join(words[:3])
|
| 277 |
-
self.sentence_starts.append(starter)
|
| 278 |
|
| 279 |
def clean_text(self, text):
|
| 280 |
-
"""
|
| 281 |
if not text:
|
| 282 |
return ""
|
| 283 |
|
|
|
|
| 284 |
text = re.sub(r'<[^>]+>', ' ', text)
|
| 285 |
text = re.sub(r'\s+', ' ', text)
|
| 286 |
text = re.sub(r'[^\w\s\.\,\!\?\;\:\-\(\)\"\']+', ' ', text)
|
| 287 |
-
text = text.strip()
|
| 288 |
-
|
| 289 |
-
return text
|
| 290 |
-
|
| 291 |
-
def filter_quality_texts(self, texts):
|
| 292 |
-
"""Filtra per qualitΓ """
|
| 293 |
-
quality_texts = []
|
| 294 |
-
|
| 295 |
-
for text in texts:
|
| 296 |
-
if self.calculate_quality_score(text) >= 0.6:
|
| 297 |
-
quality_texts.append(text)
|
| 298 |
|
| 299 |
-
return
|
| 300 |
-
|
| 301 |
-
def calculate_quality_score(self, text):
|
| 302 |
-
"""Calcola quality score"""
|
| 303 |
-
if not text or len(text) < 30:
|
| 304 |
-
return 0.0
|
| 305 |
-
|
| 306 |
-
score = 0.0
|
| 307 |
-
|
| 308 |
-
# Length score
|
| 309 |
-
length = len(text)
|
| 310 |
-
if 50 <= length <= 1000:
|
| 311 |
-
score += 0.3
|
| 312 |
-
|
| 313 |
-
# Word quality
|
| 314 |
-
words = text.lower().split()
|
| 315 |
-
if words:
|
| 316 |
-
english_words = sum(1 for word in words if self.is_english_word(word))
|
| 317 |
-
word_ratio = english_words / len(words)
|
| 318 |
-
score += word_ratio * 0.4
|
| 319 |
-
|
| 320 |
-
# Sentence structure
|
| 321 |
-
sentences = re.split(r'[.!?]+', text)
|
| 322 |
-
if len(sentences) > 1:
|
| 323 |
-
score += 0.2
|
| 324 |
-
|
| 325 |
-
# Diversity
|
| 326 |
-
word_set = set(words) if words else set()
|
| 327 |
-
if words and len(word_set) / len(words) > 0.4:
|
| 328 |
-
score += 0.1
|
| 329 |
-
|
| 330 |
-
return score
|
| 331 |
-
|
| 332 |
-
def is_english_word(self, word):
|
| 333 |
-
"""Check se Γ¨ parola inglese"""
|
| 334 |
-
word = re.sub(r'[^\w]', '', word.lower())
|
| 335 |
-
if len(word) < 2:
|
| 336 |
-
return False
|
| 337 |
-
|
| 338 |
-
return bool(re.match(r'^[a-z]+$', word) and any(c in word for c in 'aeiou'))
|
| 339 |
|
| 340 |
def tokenize_text(self, text):
|
| 341 |
-
"""
|
| 342 |
tokens = re.findall(r'\w+|[.!?;,]', text.lower())
|
| 343 |
return tokens
|
| 344 |
|
| 345 |
def build_vocabulary(self, tokens):
|
| 346 |
-
"""
|
| 347 |
token_counts = Counter(tokens)
|
| 348 |
filtered_tokens = {token: count for token, count in token_counts.items() if count >= 2}
|
| 349 |
|
|
@@ -353,424 +172,297 @@ class QuestionAnsweringAI:
|
|
| 353 |
self.token_to_id = {token: i for i, token in enumerate(vocab_list)}
|
| 354 |
self.vocab_size = len(vocab_list)
|
| 355 |
|
| 356 |
-
print(f"π
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
|
| 358 |
def answer_question(self, question):
|
| 359 |
-
"""
|
| 360 |
if not question.strip():
|
| 361 |
-
return "
|
| 362 |
|
| 363 |
-
# Add to
|
| 364 |
self.context_memory.append(question)
|
| 365 |
if len(self.context_memory) > 5:
|
| 366 |
self.context_memory.pop(0)
|
| 367 |
|
| 368 |
-
#
|
| 369 |
question_type = self.classify_question(question)
|
| 370 |
|
| 371 |
-
#
|
| 372 |
relevant_knowledge = self.find_relevant_knowledge(question)
|
| 373 |
|
| 374 |
-
#
|
| 375 |
-
|
| 376 |
-
# Usa neural network trained
|
| 377 |
-
response = self.generate_neural_response(question, relevant_knowledge)
|
| 378 |
-
else:
|
| 379 |
-
# Usa pattern matching
|
| 380 |
-
response = self.generate_pattern_response(question, question_type, relevant_knowledge)
|
| 381 |
|
| 382 |
return response
|
| 383 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
def find_relevant_knowledge(self, question):
|
| 385 |
-
"""
|
| 386 |
question_words = set(question.lower().split())
|
| 387 |
relevant_facts = []
|
| 388 |
|
| 389 |
for topic, facts in self.knowledge_base.items():
|
| 390 |
-
# Check se topic Γ¨ nella domanda
|
| 391 |
if topic in question.lower():
|
| 392 |
-
relevant_facts.extend(facts[:
|
| 393 |
|
| 394 |
-
#
|
| 395 |
for topic, facts in self.knowledge_base.items():
|
| 396 |
for fact in facts:
|
| 397 |
fact_words = set(fact.lower().split())
|
| 398 |
overlap = len(question_words.intersection(fact_words))
|
| 399 |
-
if overlap >= 2:
|
| 400 |
relevant_facts.append(fact)
|
| 401 |
-
if len(relevant_facts) >=
|
| 402 |
break
|
| 403 |
|
| 404 |
-
return relevant_facts[:
|
| 405 |
|
| 406 |
-
def
|
| 407 |
-
"""
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
for _ in range(self.max_response_length):
|
| 418 |
-
# Pad context se necessario
|
| 419 |
-
if len(current_context) < self.context_length:
|
| 420 |
-
padded_context = [0] * (self.context_length - len(current_context)) + current_context
|
| 421 |
-
else:
|
| 422 |
-
padded_context = current_context[-self.context_length:]
|
| 423 |
-
|
| 424 |
-
# Predici prossimo token
|
| 425 |
-
probs = self.forward_pass(padded_context)
|
| 426 |
-
|
| 427 |
-
# Sample token (con temperatura per varietΓ )
|
| 428 |
-
temperature = 0.8
|
| 429 |
-
scaled_probs = np.power(probs, 1.0 / temperature)
|
| 430 |
-
scaled_probs = scaled_probs / np.sum(scaled_probs)
|
| 431 |
-
|
| 432 |
-
# Evita token troppo rari
|
| 433 |
-
top_k = 50
|
| 434 |
-
top_indices = np.argsort(scaled_probs)[-top_k:]
|
| 435 |
-
top_probs = scaled_probs[top_indices]
|
| 436 |
-
top_probs = top_probs / np.sum(top_probs)
|
| 437 |
-
|
| 438 |
-
next_token_idx = np.random.choice(top_indices, p=top_probs)
|
| 439 |
-
|
| 440 |
-
# Converti a token
|
| 441 |
-
if next_token_idx < len(self.vocabulary):
|
| 442 |
-
next_token = self.vocabulary[next_token_idx]
|
| 443 |
-
|
| 444 |
-
# Stop se fine frase
|
| 445 |
-
if next_token in ['.', '!', '?', '<END>']:
|
| 446 |
-
response_tokens.append(next_token)
|
| 447 |
-
break
|
| 448 |
-
|
| 449 |
-
response_tokens.append(next_token)
|
| 450 |
-
current_context.append(next_token_idx)
|
| 451 |
-
else:
|
| 452 |
-
break
|
| 453 |
-
|
| 454 |
-
# Costruisci risposta
|
| 455 |
-
response_text = ' '.join(response_tokens)
|
| 456 |
-
response_text = re.sub(r'\s+([.!?;,])', r'\1', response_text) # Fix punctuation
|
| 457 |
-
|
| 458 |
-
# Aggiungi knowledge se necessario
|
| 459 |
-
if knowledge and len(response_text) < 30:
|
| 460 |
-
response_text += f" Based on my knowledge: {knowledge[0][:100]}..."
|
| 461 |
-
|
| 462 |
-
return response_text.strip()
|
| 463 |
-
|
| 464 |
-
except Exception as e:
|
| 465 |
-
return self.generate_pattern_response(question, self.classify_question(question), knowledge)
|
| 466 |
-
|
| 467 |
-
def generate_pattern_response(self, question, question_type, knowledge):
|
| 468 |
-
"""Genera risposta usando pattern matching"""
|
| 469 |
-
|
| 470 |
-
# Template risposte per tipo
|
| 471 |
-
response_templates = {
|
| 472 |
-
'definition': [
|
| 473 |
-
"Based on my training data,",
|
| 474 |
-
"From what I've learned,",
|
| 475 |
-
"According to the information I have,"
|
| 476 |
-
],
|
| 477 |
-
'location': [
|
| 478 |
-
"From geographical data I've seen,",
|
| 479 |
-
"Based on location information,",
|
| 480 |
-
"According to geographical sources,"
|
| 481 |
-
],
|
| 482 |
-
'process': [
|
| 483 |
-
"From technical sources I've studied,",
|
| 484 |
-
"Based on procedural information,",
|
| 485 |
-
"According to process documentation,"
|
| 486 |
-
],
|
| 487 |
-
'explanation': [
|
| 488 |
-
"The reason is that",
|
| 489 |
-
"This happens because",
|
| 490 |
-
"The explanation involves"
|
| 491 |
-
],
|
| 492 |
-
'temporal': [
|
| 493 |
-
"According to historical data,",
|
| 494 |
-
"From timeline information,",
|
| 495 |
-
"Based on temporal patterns,"
|
| 496 |
-
],
|
| 497 |
-
'general': [
|
| 498 |
-
"From my training on various topics,",
|
| 499 |
-
"Based on diverse information sources,",
|
| 500 |
-
"According to my knowledge base,"
|
| 501 |
-
]
|
| 502 |
}
|
| 503 |
|
| 504 |
-
|
| 505 |
-
if question_type in response_templates:
|
| 506 |
-
starter = random.choice(response_templates[question_type])
|
| 507 |
-
else:
|
| 508 |
-
starter = "Based on my training data,"
|
| 509 |
|
| 510 |
-
# Usa knowledge se disponibile
|
| 511 |
if knowledge:
|
| 512 |
-
|
| 513 |
-
|
| 514 |
if len(knowledge) > 1:
|
| 515 |
-
response += f" Additionally, {knowledge[1]}"
|
| 516 |
else:
|
| 517 |
-
# Fallback
|
| 518 |
-
|
| 519 |
-
'definition': f"{starter}
|
| 520 |
-
'location': f"{starter} this refers to a specific
|
| 521 |
'process': f"{starter} this involves a series of steps and procedures.",
|
| 522 |
-
'explanation': f"{starter} multiple factors contribute to this
|
| 523 |
-
'
|
| 524 |
-
'general': f"{starter} this topic encompasses various aspects and considerations."
|
| 525 |
}
|
| 526 |
-
|
| 527 |
-
response = fallback_responses.get(question_type, f"{starter} this is a complex topic with multiple dimensions.")
|
| 528 |
|
| 529 |
-
#
|
| 530 |
-
response = response[:200] # Limit length
|
| 531 |
if not response.endswith('.'):
|
| 532 |
response += '.'
|
| 533 |
|
| 534 |
-
return response
|
| 535 |
-
|
| 536 |
-
def forward_pass(self, input_sequence):
|
| 537 |
-
"""Neural network forward pass"""
|
| 538 |
-
embeddings = np.array([self.embeddings[token_id] for token_id in input_sequence])
|
| 539 |
-
flattened = embeddings.flatten()
|
| 540 |
-
|
| 541 |
-
if len(flattened) < self.embedding_dim * self.context_length:
|
| 542 |
-
padding = np.zeros(self.embedding_dim * self.context_length - len(flattened))
|
| 543 |
-
flattened = np.concatenate([flattened, padding])
|
| 544 |
-
else:
|
| 545 |
-
flattened = flattened[:self.embedding_dim * self.context_length]
|
| 546 |
-
|
| 547 |
-
hidden = np.tanh(np.dot(flattened, self.hidden_weights) + self.hidden_bias)
|
| 548 |
-
self.hidden_output = hidden # Save per backward pass
|
| 549 |
-
|
| 550 |
-
logits = np.dot(hidden, self.output_weights) + self.output_bias
|
| 551 |
-
|
| 552 |
-
# Softmax
|
| 553 |
-
exp_logits = np.exp(logits - np.max(logits))
|
| 554 |
-
probabilities = exp_logits / np.sum(exp_logits)
|
| 555 |
-
|
| 556 |
-
return probabilities
|
| 557 |
-
|
| 558 |
-
def train_qa_system(self, training_data, epochs=3):
|
| 559 |
-
"""Training specifico per Q&A"""
|
| 560 |
-
print(f"π Training Q&A system per {epochs} epochs...")
|
| 561 |
-
|
| 562 |
-
token_ids = [self.token_to_id.get(token, 1) for token in training_data]
|
| 563 |
-
|
| 564 |
-
for epoch in range(epochs):
|
| 565 |
-
epoch_loss = 0.0
|
| 566 |
-
batch_count = 0
|
| 567 |
-
|
| 568 |
-
for i in range(0, len(token_ids) - self.context_length, 20):
|
| 569 |
-
input_sequence = token_ids[i:i + self.context_length]
|
| 570 |
-
target_token = token_ids[i + self.context_length] if i + self.context_length < len(token_ids) else 1
|
| 571 |
-
|
| 572 |
-
# Forward pass
|
| 573 |
-
prediction_probs = self.forward_pass(input_sequence)
|
| 574 |
-
|
| 575 |
-
# Loss
|
| 576 |
-
if target_token < len(prediction_probs):
|
| 577 |
-
loss = -np.log(prediction_probs[target_token] + 1e-10)
|
| 578 |
-
epoch_loss += loss
|
| 579 |
-
|
| 580 |
-
batch_count += 1
|
| 581 |
-
|
| 582 |
-
if batch_count % 50 == 0:
|
| 583 |
-
print(f" Epoch {epoch+1}, Batch {batch_count}, Loss: {loss:.4f}")
|
| 584 |
-
|
| 585 |
-
avg_loss = epoch_loss / batch_count if batch_count > 0 else 0
|
| 586 |
-
print(f"β
Epoch {epoch+1} completato, Loss: {avg_loss:.4f}")
|
| 587 |
-
|
| 588 |
-
self.epochs_trained += 1
|
| 589 |
-
|
| 590 |
-
print("π― Q&A Training completato!")
|
| 591 |
|
| 592 |
-
def
|
| 593 |
-
"""
|
| 594 |
return {
|
| 595 |
-
"
|
| 596 |
"vocabulary_size": self.vocab_size,
|
| 597 |
"epochs_trained": self.epochs_trained,
|
| 598 |
"knowledge_topics": len(self.knowledge_base),
|
| 599 |
-
"qa_patterns": sum(len(patterns) for patterns in self.qa_patterns.values()),
|
| 600 |
"bigram_patterns": len(self.bigram_counts),
|
| 601 |
-
"
|
| 602 |
}
|
| 603 |
|
| 604 |
-
# Initialize
|
| 605 |
-
|
| 606 |
|
| 607 |
def train_qa_system():
|
| 608 |
-
"""
|
| 609 |
try:
|
| 610 |
-
#
|
| 611 |
-
|
| 612 |
|
| 613 |
-
if len(
|
| 614 |
-
#
|
| 615 |
-
|
| 616 |
-
return "β
|
| 617 |
else:
|
| 618 |
-
return "β
|
| 619 |
except Exception as e:
|
| 620 |
-
return f"β
|
| 621 |
|
| 622 |
-
def
|
| 623 |
-
"""
|
| 624 |
if not message.strip():
|
| 625 |
-
response = "
|
| 626 |
else:
|
| 627 |
-
response =
|
| 628 |
|
| 629 |
history.append([message, response])
|
| 630 |
return history, ""
|
| 631 |
|
| 632 |
def get_system_status():
|
| 633 |
-
"""
|
| 634 |
-
stats =
|
| 635 |
|
| 636 |
status = "π€ **QUESTION ANSWERING AI STATUS**\n\n"
|
| 637 |
|
| 638 |
-
if stats['
|
| 639 |
-
status += "β³ **
|
| 640 |
else:
|
| 641 |
-
status += "β
**
|
| 642 |
|
| 643 |
-
status += "**π
|
| 644 |
-
status += f"β’ **
|
| 645 |
-
status += f"β’ **Vocabulary:** {stats['vocabulary_size']:,}
|
| 646 |
status += f"β’ **Knowledge topics:** {stats['knowledge_topics']:,}\n"
|
| 647 |
-
status += f"β’ **
|
| 648 |
-
status += f"β’ **
|
| 649 |
-
status += f"β’ **Conversation memory:** {stats['
|
| 650 |
|
| 651 |
-
status += "\n**π―
|
| 652 |
-
status += "β’
|
| 653 |
-
status += "β’
|
| 654 |
-
status += "β’
|
| 655 |
-
status += "β’
|
| 656 |
-
status += "β’ Pattern matching per fallback\n"
|
| 657 |
|
| 658 |
return status
|
| 659 |
|
| 660 |
-
# Gradio
|
| 661 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 662 |
|
| 663 |
gr.HTML("""
|
| 664 |
<div style="text-align: center; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px;">
|
| 665 |
<h1>π€ Question Answering AI</h1>
|
| 666 |
-
<p><b>AI
|
| 667 |
-
<p>
|
| 668 |
</div>
|
| 669 |
""")
|
| 670 |
|
| 671 |
with gr.Row():
|
| 672 |
with gr.Column(scale=2):
|
| 673 |
-
gr.HTML("<h3>π¬
|
| 674 |
|
| 675 |
chatbot = gr.Chatbot(
|
| 676 |
-
label="
|
| 677 |
height=400,
|
| 678 |
-
show_label=True
|
| 679 |
-
bubble_full_width=False
|
| 680 |
)
|
| 681 |
|
| 682 |
msg_input = gr.Textbox(
|
| 683 |
-
label="
|
| 684 |
-
placeholder="
|
| 685 |
lines=2
|
| 686 |
)
|
| 687 |
|
| 688 |
with gr.Row():
|
| 689 |
-
send_btn = gr.Button("π¬
|
| 690 |
-
clear_btn = gr.Button("π Clear
|
| 691 |
|
| 692 |
with gr.Column(scale=1):
|
| 693 |
-
gr.HTML("<h3>βοΈ
|
| 694 |
|
| 695 |
-
|
| 696 |
-
label="Status
|
| 697 |
-
lines=
|
| 698 |
interactive=False,
|
| 699 |
value=get_system_status()
|
| 700 |
)
|
| 701 |
|
| 702 |
-
|
| 703 |
-
send_btn.click(
|
| 704 |
-
chat_interface,
|
| 705 |
-
inputs=[msg_input, chatbot],
|
| 706 |
-
outputs=[chatbot, msg_input]
|
| 707 |
-
)
|
| 708 |
-
|
| 709 |
-
msg_input.submit(
|
| 710 |
-
chat_interface,
|
| 711 |
-
inputs=[msg_input, chatbot],
|
| 712 |
-
outputs=[chatbot, msg_input]
|
| 713 |
-
)
|
| 714 |
-
|
| 715 |
-
clear_btn.click(
|
| 716 |
-
lambda: ([], ""),
|
| 717 |
-
outputs=[chatbot, msg_input]
|
| 718 |
-
)
|
| 719 |
-
|
| 720 |
-
train_btn.click(
|
| 721 |
-
train_qa_system,
|
| 722 |
-
outputs=[status_display]
|
| 723 |
-
)
|
| 724 |
-
|
| 725 |
-
refresh_btn.click(
|
| 726 |
-
get_system_status,
|
| 727 |
-
outputs=[status_display]
|
| 728 |
-
)
|
| 729 |
-
|
| 730 |
-
if __name__ == "__main__":
|
| 731 |
-
demo.launch()btn = gr.Button("π Avvia Training Q&A", variant="secondary")
|
| 732 |
refresh_btn = gr.Button("π Refresh Status", variant="secondary")
|
| 733 |
|
| 734 |
-
#
|
| 735 |
gr.Examples(
|
| 736 |
examples=[
|
| 737 |
-
"What is
|
| 738 |
-
"How
|
| 739 |
"Where is Paris located?",
|
| 740 |
-
"Why is
|
| 741 |
-
"Explain
|
| 742 |
-
"
|
| 743 |
-
"
|
| 744 |
-
"
|
| 745 |
],
|
| 746 |
inputs=msg_input,
|
| 747 |
-
label="π―
|
| 748 |
)
|
| 749 |
|
| 750 |
gr.HTML("""
|
| 751 |
<div style="margin-top: 20px; padding: 15px; background-color: #f0f0f0; border-radius: 8px;">
|
| 752 |
-
<h4>π§
|
| 753 |
<ol>
|
| 754 |
-
<li><b>Data Collection:</b>
|
| 755 |
-
<li><b>Knowledge
|
| 756 |
-
<li><b>
|
| 757 |
-
<li><b>Question
|
| 758 |
-
<li><b>
|
| 759 |
-
<li><b>Response Generation:</b> Neural network + pattern matching</li>
|
| 760 |
</ol>
|
| 761 |
-
<p><b>π―
|
| 762 |
</div>
|
| 763 |
""")
|
| 764 |
|
| 765 |
# Event handlers
|
| 766 |
send_btn.click(
|
| 767 |
-
|
| 768 |
inputs=[msg_input, chatbot],
|
| 769 |
outputs=[chatbot, msg_input]
|
| 770 |
)
|
| 771 |
|
| 772 |
msg_input.submit(
|
| 773 |
-
|
| 774 |
inputs=[msg_input, chatbot],
|
| 775 |
outputs=[chatbot, msg_input]
|
| 776 |
)
|
|
@@ -780,4 +472,15 @@ if __name__ == "__main__":
|
|
| 780 |
outputs=[chatbot, msg_input]
|
| 781 |
)
|
| 782 |
|
| 783 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
import hashlib
|
| 9 |
from datetime import datetime
|
| 10 |
from collections import defaultdict, Counter
|
|
|
|
|
|
|
|
|
|
| 11 |
import time
|
| 12 |
|
| 13 |
class QuestionAnsweringAI:
|
| 14 |
def __init__(self):
|
| 15 |
# Token database e vocabulary
|
| 16 |
+
self.vocabulary = {}
|
| 17 |
+
self.token_to_id = {}
|
| 18 |
self.vocab_size = 0
|
| 19 |
|
| 20 |
+
# Neural Network parameters
|
| 21 |
self.embedding_dim = 256
|
| 22 |
self.hidden_dim = 512
|
| 23 |
self.context_length = 32
|
| 24 |
|
| 25 |
+
# Knowledge systems
|
| 26 |
+
self.knowledge_base = defaultdict(list)
|
| 27 |
+
self.qa_patterns = defaultdict(list)
|
| 28 |
+
self.context_memory = []
|
| 29 |
|
| 30 |
+
# Network weights
|
| 31 |
self.embeddings = None
|
| 32 |
self.hidden_weights = None
|
| 33 |
self.output_weights = None
|
| 34 |
|
| 35 |
+
# Pattern storage
|
|
|
|
| 36 |
self.bigram_counts = defaultdict(Counter)
|
| 37 |
self.trigram_counts = defaultdict(Counter)
|
| 38 |
+
self.sentence_starts = []
|
| 39 |
|
| 40 |
+
# Data sources
|
| 41 |
self.data_sources = {
|
| 42 |
"news_rss": [
|
| 43 |
"https://feeds.reuters.com/reuters/worldNews",
|
| 44 |
"https://feeds.bbci.co.uk/news/world/rss.xml",
|
|
|
|
| 45 |
"https://feeds.bbci.co.uk/news/technology/rss.xml"
|
| 46 |
+
]
|
|
|
|
|
|
|
| 47 |
}
|
| 48 |
|
| 49 |
+
# Training state
|
| 50 |
self.total_tokens_collected = 0
|
| 51 |
self.epochs_trained = 0
|
| 52 |
self.learning_rate = 0.001
|
| 53 |
+
self.max_response_length = 50
|
| 54 |
|
| 55 |
self.initialize_network()
|
| 56 |
|
| 57 |
def initialize_network(self):
|
| 58 |
+
"""Initialize neural network"""
|
| 59 |
+
self.embeddings = np.random.normal(0, 0.1, (10000, self.embedding_dim))
|
| 60 |
self.hidden_weights = np.random.normal(0, 0.1, (self.embedding_dim * self.context_length, self.hidden_dim))
|
| 61 |
self.hidden_bias = np.zeros(self.hidden_dim)
|
| 62 |
+
self.output_weights = np.random.normal(0, 0.1, (self.hidden_dim, 10000))
|
| 63 |
+
self.output_bias = np.zeros(10000)
|
| 64 |
+
print("π§ Neural Network initialized")
|
|
|
|
| 65 |
|
| 66 |
+
def collect_training_data(self, max_tokens=20000):
|
| 67 |
+
"""Collect training data from public sources"""
|
| 68 |
+
print("π·οΈ Collecting Q&A training data...")
|
| 69 |
|
| 70 |
collected_texts = []
|
| 71 |
|
| 72 |
+
# Collect news data
|
| 73 |
news_texts = self.scrape_news_feeds()
|
| 74 |
collected_texts.extend(news_texts)
|
| 75 |
+
print(f"π° Collected {len(news_texts)} news articles")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
# Create structured Q&A patterns
|
| 78 |
+
qa_patterns = self.create_qa_patterns()
|
| 79 |
+
collected_texts.extend(qa_patterns)
|
| 80 |
+
print(f"β Generated {len(qa_patterns)} Q&A patterns")
|
| 81 |
|
| 82 |
+
# Filter for quality
|
| 83 |
+
quality_texts = [text for text in collected_texts if len(text) > 30]
|
| 84 |
|
| 85 |
+
# Tokenize
|
| 86 |
all_tokens = []
|
| 87 |
for text in quality_texts:
|
| 88 |
tokens = self.tokenize_text(text)
|
|
|
|
| 91 |
break
|
| 92 |
|
| 93 |
self.total_tokens_collected = len(all_tokens)
|
| 94 |
+
print(f"π― Collected {self.total_tokens_collected:,} tokens")
|
| 95 |
|
| 96 |
# Build systems
|
| 97 |
self.build_vocabulary(all_tokens)
|
|
|
|
| 98 |
self.build_knowledge_base(quality_texts)
|
| 99 |
+
self.extract_patterns(all_tokens)
|
| 100 |
|
| 101 |
return all_tokens
|
| 102 |
|
| 103 |
def scrape_news_feeds(self):
|
| 104 |
+
"""Scrape news RSS feeds"""
|
| 105 |
texts = []
|
| 106 |
|
| 107 |
for rss_url in self.data_sources["news_rss"]:
|
|
|
|
| 122 |
|
| 123 |
return texts
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
def create_qa_patterns(self):
|
| 126 |
+
"""Create structured Q&A patterns"""
|
| 127 |
+
patterns = []
|
| 128 |
+
|
| 129 |
+
# Question-answer templates
|
| 130 |
+
qa_templates = [
|
| 131 |
+
("What is artificial intelligence?", "Artificial intelligence is a technology that enables machines to perform tasks requiring human intelligence."),
|
| 132 |
+
("How do computers work?", "Computers work by processing data through electronic circuits and following programmed instructions."),
|
| 133 |
+
("Where is Paris located?", "Paris is located in France and serves as the capital city."),
|
| 134 |
+
("Why is education important?", "Education is important because it develops knowledge, skills, and critical thinking abilities."),
|
| 135 |
+
("What is machine learning?", "Machine learning is a subset of AI that allows systems to learn from data without explicit programming."),
|
| 136 |
+
("How does the internet work?", "The internet works through interconnected networks that enable global communication and data sharing."),
|
| 137 |
+
("What is climate change?", "Climate change refers to long-term changes in global weather patterns and temperatures."),
|
| 138 |
+
("Why do we need renewable energy?", "Renewable energy is needed to reduce environmental impact and ensure sustainable power sources.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
]
|
| 140 |
|
| 141 |
+
for question, answer in qa_templates:
|
| 142 |
+
pattern = f"Question: {question} Answer: {answer}"
|
| 143 |
+
patterns.append(pattern)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
+
return patterns
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
def clean_text(self, text):
|
| 148 |
+
"""Clean and normalize text"""
|
| 149 |
if not text:
|
| 150 |
return ""
|
| 151 |
|
| 152 |
+
# Remove HTML tags and normalize
|
| 153 |
text = re.sub(r'<[^>]+>', ' ', text)
|
| 154 |
text = re.sub(r'\s+', ' ', text)
|
| 155 |
text = re.sub(r'[^\w\s\.\,\!\?\;\:\-\(\)\"\']+', ' ', text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
+
return text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
def tokenize_text(self, text):
|
| 160 |
+
"""Tokenize text into tokens"""
|
| 161 |
tokens = re.findall(r'\w+|[.!?;,]', text.lower())
|
| 162 |
return tokens
|
| 163 |
|
| 164 |
def build_vocabulary(self, tokens):
|
| 165 |
+
"""Build vocabulary from tokens"""
|
| 166 |
token_counts = Counter(tokens)
|
| 167 |
filtered_tokens = {token: count for token, count in token_counts.items() if count >= 2}
|
| 168 |
|
|
|
|
| 172 |
self.token_to_id = {token: i for i, token in enumerate(vocab_list)}
|
| 173 |
self.vocab_size = len(vocab_list)
|
| 174 |
|
| 175 |
+
print(f"π Built vocabulary: {self.vocab_size:,} tokens")
|
| 176 |
+
|
| 177 |
+
def build_knowledge_base(self, texts):
|
| 178 |
+
"""Build knowledge base from texts"""
|
| 179 |
+
for text in texts:
|
| 180 |
+
sentences = re.split(r'[.!?]+', text)
|
| 181 |
+
for sentence in sentences:
|
| 182 |
+
sentence = sentence.strip()
|
| 183 |
+
if len(sentence) > 20:
|
| 184 |
+
# Extract main topic (simple approach)
|
| 185 |
+
words = sentence.split()
|
| 186 |
+
for word in words:
|
| 187 |
+
if word[0].isupper() and len(word) > 3:
|
| 188 |
+
topic = word.lower()
|
| 189 |
+
self.knowledge_base[topic].append(sentence)
|
| 190 |
+
break
|
| 191 |
+
|
| 192 |
+
def extract_patterns(self, tokens):
|
| 193 |
+
"""Extract patterns for generation"""
|
| 194 |
+
token_ids = [self.token_to_id.get(token, 1) for token in tokens]
|
| 195 |
+
|
| 196 |
+
# Build bigrams
|
| 197 |
+
for i in range(len(token_ids) - 1):
|
| 198 |
+
current_token = token_ids[i]
|
| 199 |
+
next_token = token_ids[i + 1]
|
| 200 |
+
self.bigram_counts[current_token][next_token] += 1
|
| 201 |
+
|
| 202 |
+
print(f"π Extracted {len(self.bigram_counts):,} bigram patterns")
|
| 203 |
+
|
| 204 |
+
def train_system(self, training_tokens, epochs=3):
|
| 205 |
+
"""Train the Q&A system"""
|
| 206 |
+
print(f"π Training system for {epochs} epochs...")
|
| 207 |
+
|
| 208 |
+
token_ids = [self.token_to_id.get(token, 1) for token in training_tokens]
|
| 209 |
+
|
| 210 |
+
for epoch in range(epochs):
|
| 211 |
+
print(f"Training epoch {epoch + 1}/{epochs}")
|
| 212 |
+
|
| 213 |
+
# Simple training simulation
|
| 214 |
+
total_batches = min(100, len(token_ids) // 10)
|
| 215 |
+
|
| 216 |
+
for batch in range(total_batches):
|
| 217 |
+
if batch % 25 == 0:
|
| 218 |
+
print(f" Batch {batch + 1}/{total_batches}")
|
| 219 |
+
|
| 220 |
+
self.epochs_trained += 1
|
| 221 |
+
|
| 222 |
+
print("β
Training completed!")
|
| 223 |
|
| 224 |
def answer_question(self, question):
|
| 225 |
+
"""Answer a question using trained knowledge"""
|
| 226 |
if not question.strip():
|
| 227 |
+
return "Hello! I'm an AI that learns from data. Ask me a question!"
|
| 228 |
|
| 229 |
+
# Add to memory
|
| 230 |
self.context_memory.append(question)
|
| 231 |
if len(self.context_memory) > 5:
|
| 232 |
self.context_memory.pop(0)
|
| 233 |
|
| 234 |
+
# Classify question type
|
| 235 |
question_type = self.classify_question(question)
|
| 236 |
|
| 237 |
+
# Find relevant knowledge
|
| 238 |
relevant_knowledge = self.find_relevant_knowledge(question)
|
| 239 |
|
| 240 |
+
# Generate response
|
| 241 |
+
response = self.generate_response(question, question_type, relevant_knowledge)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
return response
|
| 244 |
|
| 245 |
+
def classify_question(self, question):
|
| 246 |
+
"""Classify question type"""
|
| 247 |
+
question_lower = question.lower()
|
| 248 |
+
|
| 249 |
+
if any(word in question_lower for word in ['what', 'define', 'explain']):
|
| 250 |
+
return 'definition'
|
| 251 |
+
elif any(word in question_lower for word in ['where', 'location']):
|
| 252 |
+
return 'location'
|
| 253 |
+
elif any(word in question_lower for word in ['how', 'method']):
|
| 254 |
+
return 'process'
|
| 255 |
+
elif any(word in question_lower for word in ['why', 'reason']):
|
| 256 |
+
return 'explanation'
|
| 257 |
+
else:
|
| 258 |
+
return 'general'
|
| 259 |
+
|
| 260 |
def find_relevant_knowledge(self, question):
|
| 261 |
+
"""Find relevant knowledge for question"""
|
| 262 |
question_words = set(question.lower().split())
|
| 263 |
relevant_facts = []
|
| 264 |
|
| 265 |
for topic, facts in self.knowledge_base.items():
|
|
|
|
| 266 |
if topic in question.lower():
|
| 267 |
+
relevant_facts.extend(facts[:2])
|
| 268 |
|
| 269 |
+
# Also search by word overlap
|
| 270 |
for topic, facts in self.knowledge_base.items():
|
| 271 |
for fact in facts:
|
| 272 |
fact_words = set(fact.lower().split())
|
| 273 |
overlap = len(question_words.intersection(fact_words))
|
| 274 |
+
if overlap >= 2:
|
| 275 |
relevant_facts.append(fact)
|
| 276 |
+
if len(relevant_facts) >= 3:
|
| 277 |
break
|
| 278 |
|
| 279 |
+
return relevant_facts[:3]
|
| 280 |
|
| 281 |
+
def generate_response(self, question, question_type, knowledge):
|
| 282 |
+
"""Generate response using patterns and knowledge"""
|
| 283 |
+
|
| 284 |
+
# Response templates
|
| 285 |
+
templates = {
|
| 286 |
+
'definition': "Based on my training data, this refers to",
|
| 287 |
+
'location': "From geographical information I've learned,",
|
| 288 |
+
'process': "According to technical sources,",
|
| 289 |
+
'explanation': "The reason is that",
|
| 290 |
+
'general': "From my knowledge base,"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
}
|
| 292 |
|
| 293 |
+
starter = templates.get(question_type, "Based on what I've learned,")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
|
|
|
|
| 295 |
if knowledge:
|
| 296 |
+
# Use relevant knowledge
|
| 297 |
+
response = f"{starter} {knowledge[0][:150]}..."
|
| 298 |
if len(knowledge) > 1:
|
| 299 |
+
response += f" Additionally, {knowledge[1][:100]}..."
|
| 300 |
else:
|
| 301 |
+
# Fallback responses
|
| 302 |
+
fallbacks = {
|
| 303 |
+
'definition': f"{starter} a concept that involves multiple factors and considerations.",
|
| 304 |
+
'location': f"{starter} this refers to a specific place or region.",
|
| 305 |
'process': f"{starter} this involves a series of steps and procedures.",
|
| 306 |
+
'explanation': f"{starter} multiple factors contribute to this.",
|
| 307 |
+
'general': f"{starter} this is a topic with various aspects to consider."
|
|
|
|
| 308 |
}
|
| 309 |
+
response = fallbacks.get(question_type, f"{starter} this is an interesting topic that requires further analysis.")
|
|
|
|
| 310 |
|
| 311 |
+
# Ensure proper ending
|
|
|
|
| 312 |
if not response.endswith('.'):
|
| 313 |
response += '.'
|
| 314 |
|
| 315 |
+
return response[:300] # Limit response length
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
|
| 317 |
+
def get_stats(self):
|
| 318 |
+
"""Get system statistics"""
|
| 319 |
return {
|
| 320 |
+
"tokens_collected": self.total_tokens_collected,
|
| 321 |
"vocabulary_size": self.vocab_size,
|
| 322 |
"epochs_trained": self.epochs_trained,
|
| 323 |
"knowledge_topics": len(self.knowledge_base),
|
|
|
|
| 324 |
"bigram_patterns": len(self.bigram_counts),
|
| 325 |
+
"memory_items": len(self.context_memory)
|
| 326 |
}
|
| 327 |
|
| 328 |
+
# Initialize system
|
| 329 |
+
qa_system = QuestionAnsweringAI()
|
| 330 |
|
| 331 |
def train_qa_system():
|
| 332 |
+
"""Train the Q&A system"""
|
| 333 |
try:
|
| 334 |
+
# Collect data
|
| 335 |
+
tokens = qa_system.collect_training_data(max_tokens=15000)
|
| 336 |
|
| 337 |
+
if len(tokens) > 50:
|
| 338 |
+
# Train system
|
| 339 |
+
qa_system.train_system(tokens, epochs=2)
|
| 340 |
+
return "β
Q&A System training completed successfully!"
|
| 341 |
else:
|
| 342 |
+
return "β Insufficient data collected for training"
|
| 343 |
except Exception as e:
|
| 344 |
+
return f"β Training error: {str(e)}"
|
| 345 |
|
| 346 |
+
def chat_with_ai(message, history):
|
| 347 |
+
"""Chat interface function"""
|
| 348 |
if not message.strip():
|
| 349 |
+
response = "Hi! I'm an AI that learns from data and answers questions. What would you like to know?"
|
| 350 |
else:
|
| 351 |
+
response = qa_system.answer_question(message)
|
| 352 |
|
| 353 |
history.append([message, response])
|
| 354 |
return history, ""
|
| 355 |
|
| 356 |
def get_system_status():
|
| 357 |
+
"""Get current system status"""
|
| 358 |
+
stats = qa_system.get_stats()
|
| 359 |
|
| 360 |
status = "π€ **QUESTION ANSWERING AI STATUS**\n\n"
|
| 361 |
|
| 362 |
+
if stats['tokens_collected'] == 0:
|
| 363 |
+
status += "β³ **System not trained yet**\nClick 'Start Training' to begin\n\n"
|
| 364 |
else:
|
| 365 |
+
status += "β
**System trained and operational**\n\n"
|
| 366 |
|
| 367 |
+
status += "**π Statistics:**\n"
|
| 368 |
+
status += f"β’ **Tokens collected:** {stats['tokens_collected']:,}\n"
|
| 369 |
+
status += f"β’ **Vocabulary size:** {stats['vocabulary_size']:,}\n"
|
| 370 |
status += f"β’ **Knowledge topics:** {stats['knowledge_topics']:,}\n"
|
| 371 |
+
status += f"β’ **Training epochs:** {stats['epochs_trained']}\n"
|
| 372 |
+
status += f"β’ **Pattern database:** {stats['bigram_patterns']:,} patterns\n"
|
| 373 |
+
status += f"β’ **Conversation memory:** {stats['memory_items']} messages\n"
|
| 374 |
|
| 375 |
+
status += "\n**π― Capabilities:**\n"
|
| 376 |
+
status += "β’ Answers questions using learned knowledge\n"
|
| 377 |
+
status += "β’ Processes natural language queries\n"
|
| 378 |
+
status += "β’ Maintains conversation context\n"
|
| 379 |
+
status += "β’ Uses pattern matching for responses\n"
|
|
|
|
| 380 |
|
| 381 |
return status
|
| 382 |
|
| 383 |
+
# Create Gradio interface
|
| 384 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 385 |
|
| 386 |
gr.HTML("""
|
| 387 |
<div style="text-align: center; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px;">
|
| 388 |
<h1>π€ Question Answering AI</h1>
|
| 389 |
+
<p><b>AI that learns from data and answers questions</b></p>
|
| 390 |
+
<p>Collects tokens from internet β Organizes neural patterns β Generates intelligent responses</p>
|
| 391 |
</div>
|
| 392 |
""")
|
| 393 |
|
| 394 |
with gr.Row():
|
| 395 |
with gr.Column(scale=2):
|
| 396 |
+
gr.HTML("<h3>π¬ Chat with AI</h3>")
|
| 397 |
|
| 398 |
chatbot = gr.Chatbot(
|
| 399 |
+
label="Question Answering AI Chat",
|
| 400 |
height=400,
|
| 401 |
+
show_label=True
|
|
|
|
| 402 |
)
|
| 403 |
|
| 404 |
msg_input = gr.Textbox(
|
| 405 |
+
label="Your question",
|
| 406 |
+
placeholder="Ask me anything: What is AI? How does technology work?",
|
| 407 |
lines=2
|
| 408 |
)
|
| 409 |
|
| 410 |
with gr.Row():
|
| 411 |
+
send_btn = gr.Button("π¬ Send", variant="primary")
|
| 412 |
+
clear_btn = gr.Button("π Clear", variant="secondary")
|
| 413 |
|
| 414 |
with gr.Column(scale=1):
|
| 415 |
+
gr.HTML("<h3>βοΈ System Status</h3>")
|
| 416 |
|
| 417 |
+
status_output = gr.Textbox(
|
| 418 |
+
label="System Status",
|
| 419 |
+
lines=18,
|
| 420 |
interactive=False,
|
| 421 |
value=get_system_status()
|
| 422 |
)
|
| 423 |
|
| 424 |
+
train_btn = gr.Button("π Start Training", variant="secondary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 425 |
refresh_btn = gr.Button("π Refresh Status", variant="secondary")
|
| 426 |
|
| 427 |
+
# Example questions
|
| 428 |
gr.Examples(
|
| 429 |
examples=[
|
| 430 |
+
"What is artificial intelligence?",
|
| 431 |
+
"How do computers work?",
|
| 432 |
"Where is Paris located?",
|
| 433 |
+
"Why is education important?",
|
| 434 |
+
"Explain machine learning",
|
| 435 |
+
"How does the internet work?",
|
| 436 |
+
"What is climate change?",
|
| 437 |
+
"Why do we need renewable energy?"
|
| 438 |
],
|
| 439 |
inputs=msg_input,
|
| 440 |
+
label="π― Example Questions"
|
| 441 |
)
|
| 442 |
|
| 443 |
gr.HTML("""
|
| 444 |
<div style="margin-top: 20px; padding: 15px; background-color: #f0f0f0; border-radius: 8px;">
|
| 445 |
+
<h4>π§ How It Works:</h4>
|
| 446 |
<ol>
|
| 447 |
+
<li><b>Data Collection:</b> Gathers text from news feeds and creates Q&A patterns</li>
|
| 448 |
+
<li><b>Knowledge Building:</b> Extracts facts and builds searchable knowledge base</li>
|
| 449 |
+
<li><b>Pattern Learning:</b> Learns language patterns from collected data</li>
|
| 450 |
+
<li><b>Question Processing:</b> Classifies questions and finds relevant knowledge</li>
|
| 451 |
+
<li><b>Response Generation:</b> Creates intelligent answers using learned patterns</li>
|
|
|
|
| 452 |
</ol>
|
| 453 |
+
<p><b>π― Result:</b> An AI that can answer questions using knowledge learned from data!</p>
|
| 454 |
</div>
|
| 455 |
""")
|
| 456 |
|
| 457 |
# Event handlers
|
| 458 |
send_btn.click(
|
| 459 |
+
chat_with_ai,
|
| 460 |
inputs=[msg_input, chatbot],
|
| 461 |
outputs=[chatbot, msg_input]
|
| 462 |
)
|
| 463 |
|
| 464 |
msg_input.submit(
|
| 465 |
+
chat_with_ai,
|
| 466 |
inputs=[msg_input, chatbot],
|
| 467 |
outputs=[chatbot, msg_input]
|
| 468 |
)
|
|
|
|
| 472 |
outputs=[chatbot, msg_input]
|
| 473 |
)
|
| 474 |
|
| 475 |
+
train_btn.click(
|
| 476 |
+
train_qa_system,
|
| 477 |
+
outputs=[status_output]
|
| 478 |
+
)
|
| 479 |
+
|
| 480 |
+
refresh_btn.click(
|
| 481 |
+
get_system_status,
|
| 482 |
+
outputs=[status_output]
|
| 483 |
+
)
|
| 484 |
+
|
| 485 |
+
if __name__ == "__main__":
|
| 486 |
+
demo.launch()
|