Update app.py
Browse files
app.py
CHANGED
|
@@ -13,100 +13,87 @@ import os
|
|
| 13 |
import threading
|
| 14 |
import time
|
| 15 |
|
| 16 |
-
class
|
| 17 |
def __init__(self):
|
| 18 |
# Token database e vocabulary
|
| 19 |
self.vocabulary = {} # token_id -> token_string
|
| 20 |
self.token_to_id = {} # token_string -> token_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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
self.embeddings = None
|
| 30 |
self.hidden_weights = None
|
| 31 |
self.output_weights = None
|
| 32 |
|
| 33 |
-
# Pattern database per
|
| 34 |
-
self.token_patterns = defaultdict(list)
|
| 35 |
-
self.bigram_counts = defaultdict(Counter)
|
| 36 |
-
self.trigram_counts = defaultdict(Counter)
|
|
|
|
| 37 |
|
| 38 |
-
# Dataset sources
|
| 39 |
self.data_sources = {
|
| 40 |
-
"gutenberg": "https://www.gutenberg.org/files/",
|
| 41 |
-
"wikipedia_dumps": "https://dumps.wikimedia.org/enwiki/latest/",
|
| 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/science_and_environment/rss.xml",
|
| 46 |
"https://feeds.bbci.co.uk/news/technology/rss.xml"
|
| 47 |
],
|
| 48 |
-
"
|
| 49 |
-
"
|
| 50 |
-
"opensubtitles": "https://opus.nlpl.eu/OpenSubtitles.php",
|
| 51 |
-
"common_crawl": "https://data.commoncrawl.org/crawl-data/"
|
| 52 |
}
|
| 53 |
|
| 54 |
-
#
|
| 55 |
self.total_tokens_collected = 0
|
| 56 |
-
self.quality_score_threshold = 0.7
|
| 57 |
-
self.collection_active = False
|
| 58 |
-
|
| 59 |
-
# Training state
|
| 60 |
-
self.training_loss = []
|
| 61 |
self.epochs_trained = 0
|
| 62 |
self.learning_rate = 0.001
|
|
|
|
| 63 |
|
| 64 |
self.initialize_network()
|
| 65 |
|
| 66 |
def initialize_network(self):
|
| 67 |
-
"""Inizializza rete neurale
|
| 68 |
-
# Embedding layer: converte token_id in vettori densi
|
| 69 |
self.embeddings = np.random.normal(0, 0.1, (50000, self.embedding_dim))
|
| 70 |
-
|
| 71 |
-
# Hidden layer weights
|
| 72 |
self.hidden_weights = np.random.normal(0, 0.1, (self.embedding_dim * self.context_length, self.hidden_dim))
|
| 73 |
self.hidden_bias = np.zeros(self.hidden_dim)
|
| 74 |
-
|
| 75 |
-
# Output layer weights
|
| 76 |
self.output_weights = np.random.normal(0, 0.1, (self.hidden_dim, 50000))
|
| 77 |
self.output_bias = np.zeros(50000)
|
| 78 |
|
| 79 |
-
print("🧠 Neural Network
|
| 80 |
|
| 81 |
-
def
|
| 82 |
-
"""Raccoglie dati
|
| 83 |
-
print("🕷️
|
| 84 |
-
|
| 85 |
collected_texts = []
|
| 86 |
|
| 87 |
-
# 1. News
|
| 88 |
news_texts = self.scrape_news_feeds()
|
| 89 |
collected_texts.extend(news_texts)
|
| 90 |
print(f"📰 Raccolti {len(news_texts)} articoli news")
|
| 91 |
|
| 92 |
-
# 2. Wikipedia
|
| 93 |
-
wiki_texts = self.
|
| 94 |
collected_texts.extend(wiki_texts)
|
| 95 |
-
print(f"📚 Raccolti {len(wiki_texts)}
|
| 96 |
|
| 97 |
-
# 3.
|
| 98 |
-
|
| 99 |
-
collected_texts.extend(
|
| 100 |
-
print(f"
|
| 101 |
-
|
| 102 |
-
# 4. Project Gutenberg (libri pubblici)
|
| 103 |
-
gutenberg_texts = self.scrape_gutenberg_samples()
|
| 104 |
-
collected_texts.extend(gutenberg_texts)
|
| 105 |
-
print(f"📖 Raccolti {len(gutenberg_texts)} testi Gutenberg")
|
| 106 |
|
| 107 |
# Quality filtering
|
| 108 |
quality_texts = self.filter_quality_texts(collected_texts)
|
| 109 |
-
print(f"✅ Filtrati {len(quality_texts)} testi di qualità")
|
| 110 |
|
| 111 |
# Tokenization
|
| 112 |
all_tokens = []
|
|
@@ -117,473 +104,556 @@ class TokenPredictor:
|
|
| 117 |
break
|
| 118 |
|
| 119 |
self.total_tokens_collected = len(all_tokens)
|
| 120 |
-
print(f"🎯 Raccolti {self.total_tokens_collected:,} token
|
| 121 |
|
| 122 |
-
# Build
|
| 123 |
self.build_vocabulary(all_tokens)
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
# Extract patterns per training
|
| 126 |
-
self.extract_training_patterns(all_tokens)
|
| 127 |
-
|
| 128 |
-
self.collection_active = False
|
| 129 |
return all_tokens
|
| 130 |
|
| 131 |
def scrape_news_feeds(self):
|
| 132 |
-
"""Scrape
|
| 133 |
texts = []
|
| 134 |
|
| 135 |
-
for rss_url in self.data_sources["news_rss"]
|
| 136 |
try:
|
| 137 |
response = requests.get(rss_url, timeout=5)
|
| 138 |
if response.status_code == 200:
|
| 139 |
root = ET.fromstring(response.content)
|
| 140 |
-
for item in root.findall(".//item")[:
|
| 141 |
title = item.find("title")
|
| 142 |
description = item.find("description")
|
| 143 |
if title is not None:
|
| 144 |
text = title.text
|
| 145 |
if description is not None:
|
| 146 |
-
text += " " + description.text
|
| 147 |
texts.append(self.clean_text(text))
|
| 148 |
except:
|
| 149 |
continue
|
| 150 |
|
| 151 |
return texts
|
| 152 |
|
| 153 |
-
def
|
| 154 |
-
"""Scrape Wikipedia
|
| 155 |
texts = []
|
| 156 |
|
| 157 |
-
# Wikipedia API per articoli casuali
|
| 158 |
-
wiki_api_urls = [
|
| 159 |
-
"https://en.wikipedia.org/api/rest_v1/page/random/summary",
|
| 160 |
-
"https://en.wikipedia.org/w/api.php?action=query&format=json&list=random&rnnamespace=0&rnlimit=5"
|
| 161 |
-
]
|
| 162 |
-
|
| 163 |
try:
|
| 164 |
-
for i in range(
|
| 165 |
-
response = requests.get(
|
| 166 |
if response.status_code == 200:
|
| 167 |
data = response.json()
|
|
|
|
|
|
|
|
|
|
| 168 |
if 'extract' in data:
|
| 169 |
-
|
|
|
|
|
|
|
| 170 |
except:
|
| 171 |
pass
|
| 172 |
|
| 173 |
return texts
|
| 174 |
|
| 175 |
-
def
|
| 176 |
-
"""
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
#
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
# Extract abstract from description
|
| 190 |
-
desc_text = description.text
|
| 191 |
-
if "Abstract:" in desc_text:
|
| 192 |
-
abstract = desc_text.split("Abstract:")[1].strip()
|
| 193 |
-
texts.append(self.clean_text(abstract))
|
| 194 |
-
except:
|
| 195 |
-
pass
|
| 196 |
|
| 197 |
-
return
|
| 198 |
|
| 199 |
-
def
|
| 200 |
-
"""
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
-
for
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
text = response.text
|
| 215 |
-
# Extract portion of text (primi 5000 chars)
|
| 216 |
-
if len(text) > 1000:
|
| 217 |
-
sample = text[1000:6000] # Skip header
|
| 218 |
-
texts.append(self.clean_text(sample))
|
| 219 |
-
except:
|
| 220 |
-
continue
|
| 221 |
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
def clean_text(self, text):
|
| 225 |
-
"""Pulisce
|
| 226 |
if not text:
|
| 227 |
return ""
|
| 228 |
|
| 229 |
-
# Remove HTML tags
|
| 230 |
text = re.sub(r'<[^>]+>', ' ', text)
|
| 231 |
-
|
| 232 |
-
# Normalize whitespace
|
| 233 |
text = re.sub(r'\s+', ' ', text)
|
| 234 |
-
|
| 235 |
-
# Remove special characters (keep basic punctuation)
|
| 236 |
text = re.sub(r'[^\w\s\.\,\!\?\;\:\-\(\)\"\']+', ' ', text)
|
| 237 |
-
|
| 238 |
-
# Remove extra spaces
|
| 239 |
text = text.strip()
|
| 240 |
|
| 241 |
return text
|
| 242 |
|
| 243 |
def filter_quality_texts(self, texts):
|
| 244 |
-
"""Filtra
|
| 245 |
quality_texts = []
|
| 246 |
|
| 247 |
for text in texts:
|
| 248 |
-
|
| 249 |
-
if score >= self.quality_score_threshold:
|
| 250 |
quality_texts.append(text)
|
| 251 |
|
| 252 |
return quality_texts
|
| 253 |
|
| 254 |
def calculate_quality_score(self, text):
|
| 255 |
-
"""Calcola score
|
| 256 |
-
if not text or len(text) <
|
| 257 |
return 0.0
|
| 258 |
|
| 259 |
score = 0.0
|
| 260 |
|
| 261 |
-
# Length score
|
| 262 |
length = len(text)
|
| 263 |
-
if
|
| 264 |
score += 0.3
|
| 265 |
-
elif length > 50:
|
| 266 |
-
score += 0.1
|
| 267 |
|
| 268 |
-
#
|
| 269 |
words = text.lower().split()
|
| 270 |
if words:
|
| 271 |
-
|
| 272 |
-
english_words = sum(1 for word in words if self.is_likely_english_word(word))
|
| 273 |
word_ratio = english_words / len(words)
|
| 274 |
score += word_ratio * 0.4
|
| 275 |
|
| 276 |
-
# Sentence structure
|
| 277 |
sentences = re.split(r'[.!?]+', text)
|
| 278 |
if len(sentences) > 1:
|
| 279 |
score += 0.2
|
| 280 |
|
| 281 |
-
#
|
| 282 |
word_set = set(words) if words else set()
|
| 283 |
-
if words and len(word_set) / len(words) > 0.
|
| 284 |
score += 0.1
|
| 285 |
|
| 286 |
return score
|
| 287 |
|
| 288 |
-
def
|
| 289 |
-
"""
|
| 290 |
word = re.sub(r'[^\w]', '', word.lower())
|
| 291 |
if len(word) < 2:
|
| 292 |
return False
|
| 293 |
|
| 294 |
-
|
| 295 |
-
common_patterns = [
|
| 296 |
-
r'^[a-z]+$', # Only letters
|
| 297 |
-
r'.*[aeiou].*', # Contains vowels
|
| 298 |
-
]
|
| 299 |
-
|
| 300 |
-
return any(re.match(pattern, word) for pattern in common_patterns)
|
| 301 |
|
| 302 |
def tokenize_text(self, text):
|
| 303 |
-
"""Tokenizza
|
| 304 |
-
# Simple word-based tokenization con punctuation
|
| 305 |
-
# In produzione: usare BPE (Byte Pair Encoding)
|
| 306 |
-
|
| 307 |
-
# Split on whitespace e punctuation
|
| 308 |
tokens = re.findall(r'\w+|[.!?;,]', text.lower())
|
| 309 |
-
|
| 310 |
return tokens
|
| 311 |
|
| 312 |
def build_vocabulary(self, tokens):
|
| 313 |
-
"""Costruisce vocabulary
|
| 314 |
token_counts = Counter(tokens)
|
| 315 |
-
|
| 316 |
-
# Keep only tokens con frequency >= 2
|
| 317 |
filtered_tokens = {token: count for token, count in token_counts.items() if count >= 2}
|
| 318 |
|
| 319 |
-
# Add special tokens
|
| 320 |
vocab_list = ['<PAD>', '<UNK>', '<START>', '<END>'] + list(filtered_tokens.keys())
|
| 321 |
|
| 322 |
self.vocabulary = {i: token for i, token in enumerate(vocab_list)}
|
| 323 |
self.token_to_id = {token: i for i, token in enumerate(vocab_list)}
|
| 324 |
self.vocab_size = len(vocab_list)
|
| 325 |
|
| 326 |
-
print(f"📚 Vocabulary
|
| 327 |
|
| 328 |
-
def
|
| 329 |
-
"""
|
| 330 |
-
|
|
|
|
| 331 |
|
| 332 |
-
#
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
for i in range(len(token_ids) - 1):
|
| 337 |
-
current_token = token_ids[i]
|
| 338 |
-
next_token = token_ids[i + 1]
|
| 339 |
-
self.bigram_counts[current_token][next_token] += 1
|
| 340 |
|
| 341 |
-
#
|
| 342 |
-
|
| 343 |
-
context = (token_ids[i], token_ids[i + 1])
|
| 344 |
-
next_token = token_ids[i + 2]
|
| 345 |
-
self.trigram_counts[context][next_token] += 1
|
| 346 |
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
print(f" Trigrams: {len(self.trigram_counts):,}")
|
| 350 |
-
|
| 351 |
-
def train_neural_network(self, training_sequences, epochs=5):
|
| 352 |
-
"""Training della rete neurale"""
|
| 353 |
-
print(f"🏋️ Iniziando training per {epochs} epochs...")
|
| 354 |
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
|
|
|
| 364 |
|
| 365 |
-
#
|
| 366 |
-
|
| 367 |
|
| 368 |
-
#
|
| 369 |
-
|
| 370 |
-
|
|
|
|
| 371 |
|
| 372 |
-
#
|
| 373 |
-
|
|
|
|
|
|
|
|
|
|
| 374 |
|
| 375 |
-
|
| 376 |
|
| 377 |
-
|
| 378 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
|
| 384 |
-
|
| 385 |
|
| 386 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
|
| 388 |
def forward_pass(self, input_sequence):
|
| 389 |
-
"""
|
| 390 |
-
# Embedding lookup
|
| 391 |
embeddings = np.array([self.embeddings[token_id] for token_id in input_sequence])
|
| 392 |
-
|
| 393 |
-
# Flatten embeddings
|
| 394 |
flattened = embeddings.flatten()
|
| 395 |
|
| 396 |
-
# Ensure correct size
|
| 397 |
if len(flattened) < self.embedding_dim * self.context_length:
|
| 398 |
-
# Pad with zeros
|
| 399 |
padding = np.zeros(self.embedding_dim * self.context_length - len(flattened))
|
| 400 |
flattened = np.concatenate([flattened, padding])
|
| 401 |
else:
|
| 402 |
flattened = flattened[:self.embedding_dim * self.context_length]
|
| 403 |
|
| 404 |
-
# Hidden layer
|
| 405 |
hidden = np.tanh(np.dot(flattened, self.hidden_weights) + self.hidden_bias)
|
|
|
|
| 406 |
|
| 407 |
-
# Output layer
|
| 408 |
logits = np.dot(hidden, self.output_weights) + self.output_bias
|
| 409 |
|
| 410 |
# Softmax
|
| 411 |
-
exp_logits = np.exp(logits - np.max(logits))
|
| 412 |
probabilities = exp_logits / np.sum(exp_logits)
|
| 413 |
|
| 414 |
return probabilities
|
| 415 |
|
| 416 |
-
def
|
| 417 |
-
"""
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
def backward_pass(self, input_sequence, target_token, predictions):
|
| 426 |
-
"""Simplified backward pass"""
|
| 427 |
-
# Questo è un backward pass molto semplificato
|
| 428 |
-
# In produzione: usare autograd frameworks come PyTorch
|
| 429 |
-
|
| 430 |
-
# Calculate gradient per output layer
|
| 431 |
-
grad_output = predictions.copy()
|
| 432 |
-
if target_token < len(grad_output):
|
| 433 |
-
grad_output[target_token] -= 1 # Cross-entropy gradient
|
| 434 |
-
|
| 435 |
-
# Update output weights (simplified)
|
| 436 |
-
learning_rate = self.learning_rate
|
| 437 |
-
|
| 438 |
-
# Gradient clipping
|
| 439 |
-
grad_output = np.clip(grad_output, -1.0, 1.0)
|
| 440 |
-
|
| 441 |
-
# Simple weight update (only output layer for demo)
|
| 442 |
-
if hasattr(self, 'hidden_output'):
|
| 443 |
-
weight_update = np.outer(self.hidden_output, grad_output)
|
| 444 |
-
self.output_weights -= learning_rate * weight_update
|
| 445 |
-
|
| 446 |
-
def predict_next_token(self, context_text, num_predictions=5):
|
| 447 |
-
"""Predice i prossimi token dato un contesto"""
|
| 448 |
-
if not context_text.strip():
|
| 449 |
-
return ["the", "a", "an", "to", "of"]
|
| 450 |
-
|
| 451 |
-
# Tokenize context
|
| 452 |
-
context_tokens = self.tokenize_text(context_text)
|
| 453 |
-
context_ids = [self.token_to_id.get(token, 1) for token in context_tokens]
|
| 454 |
-
|
| 455 |
-
# Use neural network se addestrato
|
| 456 |
-
if self.epochs_trained > 0 and len(context_ids) > 0:
|
| 457 |
-
# Take last context_length tokens
|
| 458 |
-
input_sequence = context_ids[-self.context_length:]
|
| 459 |
-
if len(input_sequence) < self.context_length:
|
| 460 |
-
# Pad with <PAD> tokens
|
| 461 |
-
input_sequence = [0] * (self.context_length - len(input_sequence)) + input_sequence
|
| 462 |
|
| 463 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
prediction_probs = self.forward_pass(input_sequence)
|
| 465 |
|
| 466 |
-
#
|
| 467 |
-
|
| 468 |
-
|
|
|
|
| 469 |
|
| 470 |
-
|
| 471 |
-
if idx < len(self.vocabulary):
|
| 472 |
-
token = self.vocabulary[idx]
|
| 473 |
-
prob = prediction_probs[idx]
|
| 474 |
-
predictions.append(f"{token} ({prob:.3f})")
|
| 475 |
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
if len(context_ids) >= 1:
|
| 490 |
-
# Try bigram
|
| 491 |
-
last_token = context_ids[-1]
|
| 492 |
-
if last_token in self.bigram_counts:
|
| 493 |
-
most_common = self.bigram_counts[last_token].most_common(num_predictions)
|
| 494 |
-
return [f"{self.vocabulary.get(token_id, '<UNK>')} ({count})"
|
| 495 |
-
for token_id, count in most_common]
|
| 496 |
-
|
| 497 |
-
# Ultimate fallback
|
| 498 |
-
return ["the", "a", "and", "to", "of"]
|
| 499 |
-
|
| 500 |
-
def get_training_stats(self):
|
| 501 |
-
"""Ritorna statistiche del training"""
|
| 502 |
-
stats = {
|
| 503 |
"total_tokens": self.total_tokens_collected,
|
| 504 |
"vocabulary_size": self.vocab_size,
|
| 505 |
"epochs_trained": self.epochs_trained,
|
|
|
|
|
|
|
| 506 |
"bigram_patterns": len(self.bigram_counts),
|
| 507 |
-
"
|
| 508 |
-
"current_loss": self.training_loss[-1] if self.training_loss else None,
|
| 509 |
-
"collection_active": self.collection_active
|
| 510 |
}
|
| 511 |
-
return stats
|
| 512 |
|
| 513 |
-
# Initialize
|
| 514 |
-
|
| 515 |
|
| 516 |
-
def
|
| 517 |
-
"""
|
| 518 |
try:
|
| 519 |
-
#
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
if len(
|
| 523 |
-
#
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
epochs=3
|
| 527 |
-
)
|
| 528 |
-
return "✅ Raccolta dati e training completati!"
|
| 529 |
else:
|
| 530 |
-
return "❌ Dati insufficienti
|
| 531 |
except Exception as e:
|
| 532 |
-
return f"❌ Errore: {str(e)}"
|
| 533 |
|
| 534 |
-
def
|
| 535 |
-
"""Interface per
|
| 536 |
-
if not
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
for i, pred in enumerate(predictions, 1):
|
| 544 |
-
result += f"{i}. {pred}\n"
|
| 545 |
-
|
| 546 |
-
# Add stats
|
| 547 |
-
stats = predictor.get_training_stats()
|
| 548 |
-
result += f"\n**📈 Stats del modello:**\n"
|
| 549 |
-
result += f"• Token raccolti: {stats['total_tokens']:,}\n"
|
| 550 |
-
result += f"• Vocabulary size: {stats['vocabulary_size']:,}\n"
|
| 551 |
-
result += f"• Epochs addestrati: {stats['epochs_trained']}\n"
|
| 552 |
-
result += f"• Pattern bigram: {stats['bigram_patterns']:,}\n"
|
| 553 |
-
result += f"• Pattern trigram: {stats['trigram_patterns']:,}\n"
|
| 554 |
-
|
| 555 |
-
if stats['current_loss']:
|
| 556 |
-
result += f"• Loss attuale: {stats['current_loss']:.4f}\n"
|
| 557 |
-
|
| 558 |
-
return result
|
| 559 |
|
| 560 |
-
def
|
| 561 |
-
"""
|
| 562 |
-
stats =
|
| 563 |
|
| 564 |
-
status = "🤖 **
|
| 565 |
|
| 566 |
-
if stats['
|
| 567 |
-
status += "
|
| 568 |
-
elif stats['total_tokens'] == 0:
|
| 569 |
-
status += "⏳ **Modello non addestrato**\nClicca 'Avvia Training' per iniziare\n\n"
|
| 570 |
else:
|
| 571 |
-
status += "✅ **
|
| 572 |
|
| 573 |
status += "**📊 Statistiche:**\n"
|
| 574 |
status += f"• **Token raccolti:** {stats['total_tokens']:,}\n"
|
| 575 |
-
status += f"• **Vocabulary:** {stats['vocabulary_size']:,} token
|
| 576 |
-
status += f"• **
|
|
|
|
| 577 |
status += f"• **Epochs training:** {stats['epochs_trained']}\n"
|
| 578 |
-
|
| 579 |
-
if stats['current_loss']:
|
| 580 |
-
status += f"• **Loss attuale:** {stats['current_loss']:.4f}\n"
|
| 581 |
|
| 582 |
status += "\n**🎯 Capacità:**\n"
|
| 583 |
-
status += "•
|
| 584 |
-
status += "•
|
| 585 |
-
status += "•
|
| 586 |
-
status += "•
|
|
|
|
| 587 |
|
| 588 |
return status
|
| 589 |
|
|
@@ -592,87 +662,104 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 592 |
|
| 593 |
gr.HTML("""
|
| 594 |
<div style="text-align: center; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px;">
|
| 595 |
-
<h1
|
| 596 |
-
<p><b>
|
| 597 |
-
<p>
|
| 598 |
</div>
|
| 599 |
""")
|
| 600 |
|
| 601 |
with gr.Row():
|
| 602 |
with gr.Column(scale=2):
|
| 603 |
-
gr.HTML("<h3
|
| 604 |
|
| 605 |
-
|
| 606 |
-
label="
|
| 607 |
-
|
| 608 |
-
|
|
|
|
| 609 |
)
|
| 610 |
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
lines=10,
|
| 616 |
-
interactive=False
|
| 617 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 618 |
|
| 619 |
with gr.Column(scale=1):
|
| 620 |
-
gr.HTML("<h3>⚙️
|
| 621 |
|
| 622 |
-
|
| 623 |
-
label="Status
|
| 624 |
-
lines=
|
| 625 |
interactive=False,
|
| 626 |
-
value=
|
| 627 |
)
|
| 628 |
|
| 629 |
-
train_btn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 630 |
refresh_btn = gr.Button("🔄 Refresh Status", variant="secondary")
|
| 631 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 632 |
gr.HTML("""
|
| 633 |
<div style="margin-top: 20px; padding: 15px; background-color: #f0f0f0; border-radius: 8px;">
|
| 634 |
-
<h4
|
| 635 |
<ol>
|
| 636 |
-
<li><b>Data Collection:</b>
|
| 637 |
-
<li><b>
|
| 638 |
-
<li><b>
|
| 639 |
-
<li><b>
|
| 640 |
-
<li><b>
|
| 641 |
-
<li><b>
|
| 642 |
</ol>
|
| 643 |
-
<p><b>🎯
|
| 644 |
</div>
|
| 645 |
""")
|
| 646 |
|
| 647 |
-
# Examples
|
| 648 |
-
gr.Examples(
|
| 649 |
-
examples=[
|
| 650 |
-
"The weather today is",
|
| 651 |
-
"Artificial intelligence will",
|
| 652 |
-
"The capital of Italy is",
|
| 653 |
-
"Machine learning algorithms",
|
| 654 |
-
"In the year 2030",
|
| 655 |
-
"The most important thing"
|
| 656 |
-
],
|
| 657 |
-
inputs=context_input
|
| 658 |
-
)
|
| 659 |
-
|
| 660 |
# Event handlers
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
inputs=[
|
| 664 |
-
outputs=[
|
| 665 |
)
|
| 666 |
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
|
|
|
| 670 |
)
|
| 671 |
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
outputs=[
|
| 675 |
)
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
demo.launch()
|
|
|
|
| 13 |
import threading
|
| 14 |
import time
|
| 15 |
|
| 16 |
+
class QuestionAnsweringAI:
|
| 17 |
def __init__(self):
|
| 18 |
# Token database e vocabulary
|
| 19 |
self.vocabulary = {} # token_id -> token_string
|
| 20 |
self.token_to_id = {} # token_string -> token_id
|
| 21 |
self.vocab_size = 0
|
| 22 |
|
| 23 |
+
# Neural Network per text generation
|
| 24 |
self.embedding_dim = 256
|
| 25 |
self.hidden_dim = 512
|
| 26 |
self.context_length = 32
|
| 27 |
|
| 28 |
+
# Knowledge base costruita dai dati
|
| 29 |
+
self.knowledge_base = defaultdict(list) # topic -> [facts]
|
| 30 |
+
self.qa_patterns = defaultdict(list) # question_type -> [answer_patterns]
|
| 31 |
+
self.context_memory = [] # Conversational memory
|
| 32 |
+
|
| 33 |
+
# Parametri del network
|
| 34 |
self.embeddings = None
|
| 35 |
self.hidden_weights = None
|
| 36 |
self.output_weights = None
|
| 37 |
|
| 38 |
+
# Pattern database per generation
|
| 39 |
+
self.token_patterns = defaultdict(list)
|
| 40 |
+
self.bigram_counts = defaultdict(Counter)
|
| 41 |
+
self.trigram_counts = defaultdict(Counter)
|
| 42 |
+
self.sentence_starts = [] # Per iniziare risposte
|
| 43 |
|
| 44 |
+
# Dataset sources
|
| 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 & generation state
|
| 57 |
self.total_tokens_collected = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
self.epochs_trained = 0
|
| 59 |
self.learning_rate = 0.001
|
| 60 |
+
self.max_response_length = 100
|
| 61 |
|
| 62 |
self.initialize_network()
|
| 63 |
|
| 64 |
def initialize_network(self):
|
| 65 |
+
"""Inizializza rete neurale"""
|
|
|
|
| 66 |
self.embeddings = np.random.normal(0, 0.1, (50000, self.embedding_dim))
|
|
|
|
|
|
|
| 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, 50000))
|
| 70 |
self.output_bias = np.zeros(50000)
|
| 71 |
|
| 72 |
+
print("🧠 Neural Network per Q&A inizializzato")
|
| 73 |
|
| 74 |
+
def collect_qa_training_data(self, max_tokens=100000):
|
| 75 |
+
"""Raccoglie dati focalizzati su Q&A patterns"""
|
| 76 |
+
print("🕷️ Raccogliendo dati per Question Answering...")
|
| 77 |
+
|
| 78 |
collected_texts = []
|
| 79 |
|
| 80 |
+
# 1. News articles (per current events Q&A)
|
| 81 |
news_texts = self.scrape_news_feeds()
|
| 82 |
collected_texts.extend(news_texts)
|
| 83 |
print(f"📰 Raccolti {len(news_texts)} articoli news")
|
| 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 |
+
# 3. Q&A structured data
|
| 91 |
+
qa_texts = self.create_qa_patterns()
|
| 92 |
+
collected_texts.extend(qa_texts)
|
| 93 |
+
print(f"❓ Generati {len(qa_texts)} pattern Q&A")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
# Quality filtering
|
| 96 |
quality_texts = self.filter_quality_texts(collected_texts)
|
|
|
|
| 97 |
|
| 98 |
# Tokenization
|
| 99 |
all_tokens = []
|
|
|
|
| 104 |
break
|
| 105 |
|
| 106 |
self.total_tokens_collected = len(all_tokens)
|
| 107 |
+
print(f"🎯 Raccolti {self.total_tokens_collected:,} token per Q&A")
|
| 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.extract_generation_patterns(all_tokens)
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
return all_tokens
|
| 116 |
|
| 117 |
def scrape_news_feeds(self):
|
| 118 |
+
"""Scrape news per current events"""
|
| 119 |
texts = []
|
| 120 |
|
| 121 |
+
for rss_url in self.data_sources["news_rss"]:
|
| 122 |
try:
|
| 123 |
response = requests.get(rss_url, timeout=5)
|
| 124 |
if response.status_code == 200:
|
| 125 |
root = ET.fromstring(response.content)
|
| 126 |
+
for item in root.findall(".//item")[:3]:
|
| 127 |
title = item.find("title")
|
| 128 |
description = item.find("description")
|
| 129 |
if title is not None:
|
| 130 |
text = title.text
|
| 131 |
if description is not None:
|
| 132 |
+
text += ". " + description.text
|
| 133 |
texts.append(self.clean_text(text))
|
| 134 |
except:
|
| 135 |
continue
|
| 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 |
+
"""Crea pattern Q&A strutturati per training"""
|
| 162 |
+
qa_patterns = []
|
| 163 |
+
|
| 164 |
+
# Question templates con risposte
|
| 165 |
+
templates = [
|
| 166 |
+
{
|
| 167 |
+
"questions": ["What is", "Define", "Explain"],
|
| 168 |
+
"topics": ["artificial intelligence", "machine learning", "climate change", "economics"],
|
| 169 |
+
"answers": ["is a technology that", "refers to the", "involves the process of"]
|
| 170 |
+
},
|
| 171 |
+
{
|
| 172 |
+
"questions": ["Where is", "What is the capital of"],
|
| 173 |
+
"topics": ["France", "Italy", "Germany", "Japan"],
|
| 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 |
+
# Genera esempi Q&A
|
| 189 |
+
for template in templates:
|
| 190 |
+
for question in template["questions"]:
|
| 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 |
+
if any(word in text_lower for word in ['what', 'define', 'explain']):
|
| 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 |
+
"""Pulisce testo"""
|
| 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 quality_texts
|
| 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 |
+
"""Tokenizza testo"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
tokens = re.findall(r'\w+|[.!?;,]', text.lower())
|
|
|
|
| 343 |
return tokens
|
| 344 |
|
| 345 |
def build_vocabulary(self, tokens):
|
| 346 |
+
"""Costruisce vocabulary"""
|
| 347 |
token_counts = Counter(tokens)
|
|
|
|
|
|
|
| 348 |
filtered_tokens = {token: count for token, count in token_counts.items() if count >= 2}
|
| 349 |
|
|
|
|
| 350 |
vocab_list = ['<PAD>', '<UNK>', '<START>', '<END>'] + list(filtered_tokens.keys())
|
| 351 |
|
| 352 |
self.vocabulary = {i: token for i, token in enumerate(vocab_list)}
|
| 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"📚 Vocabulary: {self.vocab_size:,} token")
|
| 357 |
|
| 358 |
+
def answer_question(self, question):
|
| 359 |
+
"""Risponde a una domanda usando AI trained"""
|
| 360 |
+
if not question.strip():
|
| 361 |
+
return "Ciao! Sono un AI che impara dai dati. Fai una domanda e userò la mia conoscenza per rispondere!"
|
| 362 |
|
| 363 |
+
# Add to conversation memory
|
| 364 |
+
self.context_memory.append(question)
|
| 365 |
+
if len(self.context_memory) > 5:
|
| 366 |
+
self.context_memory.pop(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
|
| 368 |
+
# Classifica la domanda
|
| 369 |
+
question_type = self.classify_question(question)
|
|
|
|
|
|
|
|
|
|
| 370 |
|
| 371 |
+
# Trova knowledge rilevante
|
| 372 |
+
relevant_knowledge = self.find_relevant_knowledge(question)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
|
| 374 |
+
# Genera risposta
|
| 375 |
+
if self.epochs_trained > 0:
|
| 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 |
+
"""Trova knowledge rilevante per la domanda"""
|
| 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[:3]) # Top 3 facts per topic
|
| 393 |
+
|
| 394 |
+
# Cerca anche per keyword matching
|
| 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: # Almeno 2 parole in comune
|
| 400 |
+
relevant_facts.append(fact)
|
| 401 |
+
if len(relevant_facts) >= 5:
|
| 402 |
+
break
|
| 403 |
+
|
| 404 |
+
return relevant_facts[:5] # Limit to top 5
|
| 405 |
+
|
| 406 |
+
def generate_neural_response(self, question, knowledge):
|
| 407 |
+
"""Genera risposta usando neural network"""
|
| 408 |
+
try:
|
| 409 |
+
# Tokenizza la domanda
|
| 410 |
+
question_tokens = self.tokenize_text(question)
|
| 411 |
+
question_ids = [self.token_to_id.get(token, 1) for token in question_tokens]
|
| 412 |
+
|
| 413 |
+
# Genera risposta token by token
|
| 414 |
+
response_tokens = []
|
| 415 |
+
current_context = question_ids[-self.context_length:]
|
| 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 |
+
# Inizia risposta
|
| 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 |
+
response = f"{starter} {knowledge[0]}"
|
| 513 |
+
# Aggiungi più context se disponibile
|
| 514 |
+
if len(knowledge) > 1:
|
| 515 |
+
response += f" Additionally, {knowledge[1]}"
|
| 516 |
+
else:
|
| 517 |
+
# Fallback response
|
| 518 |
+
fallback_responses = {
|
| 519 |
+
'definition': f"{starter} this concept involves multiple factors and considerations.",
|
| 520 |
+
'location': f"{starter} this refers to a specific geographical location.",
|
| 521 |
+
'process': f"{starter} this involves a series of steps and procedures.",
|
| 522 |
+
'explanation': f"{starter} multiple factors contribute to this phenomenon.",
|
| 523 |
+
'temporal': f"{starter} this relates to specific time periods or sequences.",
|
| 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 |
+
# Clean up response
|
| 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 get_system_stats(self):
|
| 593 |
+
"""Statistiche del sistema"""
|
| 594 |
+
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 595 |
"total_tokens": self.total_tokens_collected,
|
| 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 |
+
"conversation_memory": len(self.context_memory)
|
|
|
|
|
|
|
| 602 |
}
|
|
|
|
| 603 |
|
| 604 |
+
# Initialize Q&A AI
|
| 605 |
+
qa_ai = QuestionAnsweringAI()
|
| 606 |
|
| 607 |
+
def train_qa_system():
|
| 608 |
+
"""Training del sistema Q&A"""
|
| 609 |
try:
|
| 610 |
+
# Raccolta dati
|
| 611 |
+
training_tokens = qa_ai.collect_qa_training_data(max_tokens=30000)
|
| 612 |
+
|
| 613 |
+
if len(training_tokens) > 100:
|
| 614 |
+
# Training
|
| 615 |
+
qa_ai.train_qa_system(training_tokens, epochs=3)
|
| 616 |
+
return "✅ Sistema Q&A addestrato con successo!"
|
|
|
|
|
|
|
|
|
|
| 617 |
else:
|
| 618 |
+
return "❌ Dati insufficienti per training"
|
| 619 |
except Exception as e:
|
| 620 |
+
return f"❌ Errore durante training: {str(e)}"
|
| 621 |
|
| 622 |
+
def chat_interface(message, history):
|
| 623 |
+
"""Interface per Q&A"""
|
| 624 |
+
if not message.strip():
|
| 625 |
+
response = "Ciao! Sono un AI che impara dai dati e risponde alle tue domande. Prova a chiedermi qualcosa!"
|
| 626 |
+
else:
|
| 627 |
+
response = qa_ai.answer_question(message)
|
| 628 |
+
|
| 629 |
+
history.append([message, response])
|
| 630 |
+
return history, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 631 |
|
| 632 |
+
def get_system_status():
|
| 633 |
+
"""Status del sistema"""
|
| 634 |
+
stats = qa_ai.get_system_stats()
|
| 635 |
|
| 636 |
+
status = "🤖 **QUESTION ANSWERING AI STATUS**\n\n"
|
| 637 |
|
| 638 |
+
if stats['total_tokens'] == 0:
|
| 639 |
+
status += "⏳ **Sistema non addestrato**\nClicca 'Avvia Training' per iniziare\n\n"
|
|
|
|
|
|
|
| 640 |
else:
|
| 641 |
+
status += "✅ **Sistema addestrato e operativo**\n\n"
|
| 642 |
|
| 643 |
status += "**📊 Statistiche:**\n"
|
| 644 |
status += f"• **Token raccolti:** {stats['total_tokens']:,}\n"
|
| 645 |
+
status += f"• **Vocabulary:** {stats['vocabulary_size']:,} token\n"
|
| 646 |
+
status += f"• **Knowledge topics:** {stats['knowledge_topics']:,}\n"
|
| 647 |
+
status += f"• **Q&A patterns:** {stats['qa_patterns']:,}\n"
|
| 648 |
status += f"• **Epochs training:** {stats['epochs_trained']}\n"
|
| 649 |
+
status += f"• **Conversation memory:** {stats['conversation_memory']} messaggi\n"
|
|
|
|
|
|
|
| 650 |
|
| 651 |
status += "\n**🎯 Capacità:**\n"
|
| 652 |
+
status += "• Risponde a domande usando conoscenza appresa\n"
|
| 653 |
+
status += "• Genera testo con neural network\n"
|
| 654 |
+
status += "• Usa knowledge base costruita dai dati\n"
|
| 655 |
+
status += "• Memoria conversazionale\n"
|
| 656 |
+
status += "• Pattern matching per fallback\n"
|
| 657 |
|
| 658 |
return status
|
| 659 |
|
|
|
|
| 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 che impara dai dati e risponde alle domande</b></p>
|
| 667 |
+
<p>Acquisisce token da internet → Auto-organizza neuroni → Genera risposte intelligenti</p>
|
| 668 |
</div>
|
| 669 |
""")
|
| 670 |
|
| 671 |
with gr.Row():
|
| 672 |
with gr.Column(scale=2):
|
| 673 |
+
gr.HTML("<h3>💬 Conversazione con AI</h3>")
|
| 674 |
|
| 675 |
+
chatbot = gr.Chatbot(
|
| 676 |
+
label="Chat con Question Answering AI",
|
| 677 |
+
height=400,
|
| 678 |
+
show_label=True,
|
| 679 |
+
bubble_full_width=False
|
| 680 |
)
|
| 681 |
|
| 682 |
+
msg_input = gr.Textbox(
|
| 683 |
+
label="La tua domanda",
|
| 684 |
+
placeholder="Es: What is artificial intelligence? Where is the capital of France?",
|
| 685 |
+
lines=2
|
|
|
|
|
|
|
| 686 |
)
|
| 687 |
+
|
| 688 |
+
with gr.Row():
|
| 689 |
+
send_btn = gr.Button("💬 Invia", variant="primary")
|
| 690 |
+
clear_btn = gr.Button("🔄 Clear Chat", variant="secondary")
|
| 691 |
|
| 692 |
with gr.Column(scale=1):
|
| 693 |
+
gr.HTML("<h3>⚙️ Sistema & Training</h3>")
|
| 694 |
|
| 695 |
+
status_display = gr.Textbox(
|
| 696 |
+
label="Status Sistema",
|
| 697 |
+
lines=20,
|
| 698 |
interactive=False,
|
| 699 |
+
value=get_system_status()
|
| 700 |
)
|
| 701 |
|
| 702 |
+
train_btn.click(
|
| 703 |
+
train_qa_system,
|
| 704 |
+
outputs=[status_display]
|
| 705 |
+
)
|
| 706 |
+
|
| 707 |
+
refresh_btn.click(
|
| 708 |
+
get_system_status,
|
| 709 |
+
outputs=[status_display]
|
| 710 |
+
)
|
| 711 |
+
|
| 712 |
+
if __name__ == "__main__":
|
| 713 |
+
demo.launch()btn = gr.Button("🚀 Avvia Training Q&A", variant="secondary")
|
| 714 |
refresh_btn = gr.Button("🔄 Refresh Status", variant="secondary")
|
| 715 |
|
| 716 |
+
# Examples
|
| 717 |
+
gr.Examples(
|
| 718 |
+
examples=[
|
| 719 |
+
"What is machine learning?",
|
| 720 |
+
"How does artificial intelligence work?",
|
| 721 |
+
"Where is Paris located?",
|
| 722 |
+
"Why is climate change important?",
|
| 723 |
+
"Explain neural networks",
|
| 724 |
+
"What are the benefits of technology?",
|
| 725 |
+
"How do computers process information?",
|
| 726 |
+
"What is the purpose of education?"
|
| 727 |
+
],
|
| 728 |
+
inputs=msg_input,
|
| 729 |
+
label="🎯 Esempi di Domande"
|
| 730 |
+
)
|
| 731 |
+
|
| 732 |
gr.HTML("""
|
| 733 |
<div style="margin-top: 20px; padding: 15px; background-color: #f0f0f0; border-radius: 8px;">
|
| 734 |
+
<h4>🧠 Question Answering Pipeline:</h4>
|
| 735 |
<ol>
|
| 736 |
+
<li><b>Data Collection:</b> RSS news, Wikipedia, Q&A patterns strutturati</li>
|
| 737 |
+
<li><b>Knowledge Extraction:</b> Facts, entities, Q&A patterns dai testi</li>
|
| 738 |
+
<li><b>Neural Training:</b> Rete neurale per text generation</li>
|
| 739 |
+
<li><b>Question Classification:</b> Tipo di domanda (definition, location, etc.)</li>
|
| 740 |
+
<li><b>Knowledge Retrieval:</b> Trova informazioni rilevanti</li>
|
| 741 |
+
<li><b>Response Generation:</b> Neural network + pattern matching</li>
|
| 742 |
</ol>
|
| 743 |
+
<p><b>🎯 Risultato:</b> AI che risponde intelligentemente usando conoscenza appresa dai dati!</p>
|
| 744 |
</div>
|
| 745 |
""")
|
| 746 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 747 |
# Event handlers
|
| 748 |
+
send_btn.click(
|
| 749 |
+
chat_interface,
|
| 750 |
+
inputs=[msg_input, chatbot],
|
| 751 |
+
outputs=[chatbot, msg_input]
|
| 752 |
)
|
| 753 |
|
| 754 |
+
msg_input.submit(
|
| 755 |
+
chat_interface,
|
| 756 |
+
inputs=[msg_input, chatbot],
|
| 757 |
+
outputs=[chatbot, msg_input]
|
| 758 |
)
|
| 759 |
|
| 760 |
+
clear_btn.click(
|
| 761 |
+
lambda: ([], ""),
|
| 762 |
+
outputs=[chatbot, msg_input]
|
| 763 |
)
|
| 764 |
+
|
| 765 |
+
train_
|
|
|