Update app.py
Browse files
app.py
CHANGED
@@ -1,625 +1,678 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import json
|
4 |
-
from datetime import datetime, timedelta
|
5 |
import re
|
6 |
import xml.etree.ElementTree as ET
|
|
|
7 |
import random
|
8 |
import hashlib
|
9 |
-
import
|
10 |
-
from collections import defaultdict
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
class
|
13 |
def __init__(self):
|
14 |
-
#
|
15 |
-
self.
|
16 |
-
self.
|
17 |
-
self.
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
self.data_sources = {
|
26 |
-
"
|
27 |
-
|
28 |
-
|
29 |
-
"
|
30 |
-
"
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
}
|
33 |
|
34 |
-
#
|
35 |
-
self.
|
36 |
-
|
37 |
-
|
38 |
-
"analytical": self.analytical_reasoning,
|
39 |
-
"synthetic": self.synthetic_reasoning,
|
40 |
-
"critical": self.critical_reasoning
|
41 |
-
}
|
42 |
|
43 |
-
#
|
44 |
-
self.
|
45 |
-
|
46 |
-
|
47 |
-
"history": 0.88,
|
48 |
-
"philosophy": 0.85,
|
49 |
-
"economics": 0.90,
|
50 |
-
"politics": 0.87,
|
51 |
-
"culture": 0.83,
|
52 |
-
"arts": 0.80,
|
53 |
-
"medicine": 0.85,
|
54 |
-
"engineering": 0.88,
|
55 |
-
"psychology": 0.82,
|
56 |
-
"education": 0.84,
|
57 |
-
"environment": 0.86,
|
58 |
-
"business": 0.89
|
59 |
-
}
|
60 |
|
61 |
-
|
62 |
-
"""Simulates massive pre-training on internet-scale data"""
|
63 |
-
return {
|
64 |
-
"science_and_technology": {
|
65 |
-
"keywords": ["AI", "machine learning", "quantum", "physics", "chemistry", "biology",
|
66 |
-
"computer science", "engineering", "mathematics", "astronomy", "genetics",
|
67 |
-
"nanotechnology", "robotics", "blockchain", "cybersecurity"],
|
68 |
-
"concepts": {
|
69 |
-
"artificial_intelligence": {
|
70 |
-
"definition": "Simulation of human intelligence in machines",
|
71 |
-
"applications": ["autonomous vehicles", "medical diagnosis", "natural language processing"],
|
72 |
-
"challenges": ["bias", "interpretability", "alignment"],
|
73 |
-
"future_trends": ["AGI", "quantum AI", "neuromorphic computing"]
|
74 |
-
},
|
75 |
-
"quantum_computing": {
|
76 |
-
"definition": "Computing using quantum mechanical phenomena",
|
77 |
-
"applications": ["cryptography", "drug discovery", "optimization"],
|
78 |
-
"challenges": ["decoherence", "error correction", "scalability"],
|
79 |
-
"future_trends": ["quantum supremacy", "quantum internet", "quantum AI"]
|
80 |
-
}
|
81 |
-
}
|
82 |
-
},
|
83 |
-
"humanities_and_culture": {
|
84 |
-
"keywords": ["history", "philosophy", "literature", "art", "music", "religion",
|
85 |
-
"anthropology", "sociology", "linguistics", "archaeology", "ethics"],
|
86 |
-
"concepts": {
|
87 |
-
"philosophy": {
|
88 |
-
"branches": ["metaphysics", "epistemology", "ethics", "logic", "aesthetics"],
|
89 |
-
"major_thinkers": ["Plato", "Aristotle", "Kant", "Nietzsche", "Wittgenstein"],
|
90 |
-
"contemporary_issues": ["consciousness", "free will", "meaning of life"]
|
91 |
-
},
|
92 |
-
"history": {
|
93 |
-
"periods": ["ancient", "medieval", "renaissance", "modern", "contemporary"],
|
94 |
-
"themes": ["civilizations", "wars", "revolutions", "cultural movements"],
|
95 |
-
"methodologies": ["primary sources", "historiography", "comparative analysis"]
|
96 |
-
}
|
97 |
-
}
|
98 |
-
},
|
99 |
-
"social_sciences": {
|
100 |
-
"keywords": ["psychology", "sociology", "economics", "political science", "anthropology",
|
101 |
-
"education", "communication", "criminology", "social work"],
|
102 |
-
"concepts": {
|
103 |
-
"psychology": {
|
104 |
-
"branches": ["cognitive", "behavioral", "developmental", "clinical", "social"],
|
105 |
-
"theories": ["cognitive theory", "behaviorism", "psychoanalysis", "humanistic"],
|
106 |
-
"applications": ["therapy", "education", "organizational behavior"]
|
107 |
-
},
|
108 |
-
"economics": {
|
109 |
-
"schools": ["classical", "keynesian", "chicago", "austrian", "behavioral"],
|
110 |
-
"concepts": ["supply and demand", "inflation", "GDP", "market efficiency"],
|
111 |
-
"current_issues": ["inequality", "automation", "cryptocurrency", "sustainability"]
|
112 |
-
}
|
113 |
-
}
|
114 |
-
},
|
115 |
-
"current_affairs": {
|
116 |
-
"keywords": ["politics", "international relations", "conflicts", "diplomacy", "elections",
|
117 |
-
"climate change", "pandemics", "migration", "trade", "terrorism"],
|
118 |
-
"concepts": {
|
119 |
-
"geopolitics": {
|
120 |
-
"theories": ["realism", "liberalism", "constructivism", "critical theory"],
|
121 |
-
"actors": ["states", "international organizations", "NGOs", "multinational corporations"],
|
122 |
-
"issues": ["security", "economic interdependence", "human rights", "sovereignty"]
|
123 |
-
}
|
124 |
-
}
|
125 |
-
},
|
126 |
-
"practical_skills": {
|
127 |
-
"keywords": ["programming", "project management", "communication", "leadership",
|
128 |
-
"problem solving", "creativity", "critical thinking", "research"],
|
129 |
-
"concepts": {
|
130 |
-
"programming": {
|
131 |
-
"languages": ["Python", "JavaScript", "Java", "C++", "Rust", "Go"],
|
132 |
-
"paradigms": ["object-oriented", "functional", "procedural", "declarative"],
|
133 |
-
"applications": ["web development", "data science", "AI/ML", "systems programming"]
|
134 |
-
}
|
135 |
-
}
|
136 |
-
}
|
137 |
-
}
|
138 |
-
|
139 |
-
def fetch_real_time_data(self, domain="general"):
|
140 |
-
"""Fetches real-time data from multiple sources"""
|
141 |
-
all_data = []
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
else:
|
148 |
-
sources_to_check.extend(["reuters", "bbc"])
|
149 |
-
|
150 |
-
for source in sources_to_check:
|
151 |
-
if source in self.data_sources["news"]:
|
152 |
-
try:
|
153 |
-
response = requests.get(self.data_sources["news"][source], timeout=5)
|
154 |
-
if response.status_code == 200:
|
155 |
-
root = ET.fromstring(response.content)
|
156 |
-
for item in root.findall(".//item")[:3]:
|
157 |
-
title = item.find("title")
|
158 |
-
description = item.find("description")
|
159 |
-
if title is not None:
|
160 |
-
all_data.append({
|
161 |
-
"source": source.upper(),
|
162 |
-
"title": title.text,
|
163 |
-
"description": description.text if description is not None else "",
|
164 |
-
"domain": self.classify_content_domain(title.text),
|
165 |
-
"timestamp": datetime.now()
|
166 |
-
})
|
167 |
-
except:
|
168 |
-
continue
|
169 |
-
|
170 |
-
return all_data[:10]
|
171 |
-
|
172 |
-
def classify_content_domain(self, text):
|
173 |
-
"""Classifies content into knowledge domains"""
|
174 |
-
text_lower = text.lower()
|
175 |
-
|
176 |
-
domain_indicators = {
|
177 |
-
"science_and_technology": ["AI", "technology", "science", "research", "innovation", "quantum", "space"],
|
178 |
-
"current_affairs": ["politics", "election", "government", "conflict", "diplomacy", "war", "crisis"],
|
179 |
-
"social_sciences": ["economy", "market", "society", "culture", "education", "health"],
|
180 |
-
"humanities_and_culture": ["art", "literature", "philosophy", "history", "culture", "religion"]
|
181 |
-
}
|
182 |
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
scores[domain] = score
|
187 |
-
|
188 |
-
return max(scores, key=scores.get) if any(scores.values()) else "general"
|
189 |
-
|
190 |
-
def detect_query_complexity(self, query):
|
191 |
-
"""Analyzes query complexity and required reasoning type"""
|
192 |
-
complexity_indicators = {
|
193 |
-
"simple": ["what is", "define", "quando", "dove", "chi è"],
|
194 |
-
"moderate": ["how does", "why", "explain", "compare", "difference"],
|
195 |
-
"complex": ["analyze", "evaluate", "synthesize", "predict", "implications"],
|
196 |
-
"creative": ["imagine", "create", "design", "invent", "brainstorm"],
|
197 |
-
"philosophical": ["meaning", "purpose", "consciousness", "existence", "truth", "reality"]
|
198 |
-
}
|
199 |
|
200 |
-
|
201 |
-
|
|
|
202 |
|
203 |
-
|
204 |
-
|
205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
break
|
207 |
|
208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
|
210 |
-
def
|
211 |
-
"""
|
212 |
-
|
213 |
-
domain = self.classify_content_domain(query)
|
214 |
|
215 |
-
#
|
216 |
-
|
217 |
-
"
|
218 |
-
"
|
219 |
-
|
220 |
-
"time_references": re.findall(r'\b(?:today|tomorrow|yesterday|next year|future|past|2024|2025)\b', query, re.IGNORECASE)
|
221 |
-
}
|
222 |
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
}
|
235 |
|
236 |
-
def
|
237 |
-
"""
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
|
244 |
-
def
|
245 |
-
"""
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
|
252 |
-
def
|
253 |
-
"""
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
259 |
|
260 |
-
def
|
261 |
-
"""
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
|
|
|
|
|
|
267 |
|
268 |
-
def
|
269 |
-
"""
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
275 |
|
276 |
-
def
|
277 |
-
"""
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
#
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
300 |
|
301 |
-
#
|
302 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
303 |
|
304 |
-
|
|
|
|
|
305 |
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
def select_reasoning_type(self, complexity, domain):
|
310 |
-
"""Selects appropriate reasoning framework"""
|
311 |
-
if complexity == "creative":
|
312 |
-
return "creative"
|
313 |
-
elif complexity == "philosophical":
|
314 |
-
return "critical"
|
315 |
-
elif domain == "science_and_technology":
|
316 |
-
return "analytical"
|
317 |
-
elif complexity == "complex":
|
318 |
-
return "synthetic"
|
319 |
-
else:
|
320 |
-
return "logical"
|
321 |
|
322 |
-
def
|
323 |
-
"""
|
324 |
-
|
325 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
326 |
|
327 |
-
|
|
|
328 |
|
329 |
-
#
|
330 |
-
|
331 |
-
"science_and_technology": "Based on current scientific understanding and technological developments,",
|
332 |
-
"current_affairs": "According to the latest information and real-time data,",
|
333 |
-
"social_sciences": "From a social science perspective, drawing on established research,",
|
334 |
-
"humanities_and_culture": "Considering historical and cultural context,"
|
335 |
-
}
|
336 |
|
337 |
-
|
338 |
-
|
|
|
339 |
|
340 |
-
|
341 |
-
|
342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
343 |
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
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 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
])
|
394 |
-
|
395 |
-
# Domain-specific creativity
|
396 |
-
domain = extraction["domain"]
|
397 |
-
if domain == "science_and_technology":
|
398 |
-
response.extend([
|
399 |
-
"**🚀 Future-Tech Scenarios:**",
|
400 |
-
"• Breakthrough technologies that could emerge",
|
401 |
-
"• Convergence of multiple scientific fields",
|
402 |
-
"• Transformative applications and societal impacts"
|
403 |
-
])
|
404 |
-
elif domain == "social_sciences":
|
405 |
-
response.extend([
|
406 |
-
"**🌍 Social Innovation:**",
|
407 |
-
"• Novel social structures and governance models",
|
408 |
-
"• Creative solutions to collective challenges",
|
409 |
-
"• Emerging cultural and behavioral patterns"
|
410 |
-
])
|
411 |
-
|
412 |
-
response.append("")
|
413 |
-
response.append("*This creative exploration opens new avenues for thinking about your question.*")
|
414 |
-
|
415 |
-
return "\n".join(response)
|
416 |
-
|
417 |
-
def generate_philosophical_response(self, query, extraction, reasoning_process):
|
418 |
-
"""Generates deep philosophical responses"""
|
419 |
-
response = []
|
420 |
-
|
421 |
-
response.append("🤔 **Philosophical Inquiry:**")
|
422 |
-
response.append(f"*{reasoning_process['evaluation']}*")
|
423 |
-
response.append("")
|
424 |
-
|
425 |
-
# Philosophical frameworks
|
426 |
-
response.extend([
|
427 |
-
"**📚 Multiple Philosophical Perspectives:**",
|
428 |
-
"",
|
429 |
-
"**• Epistemological View:**",
|
430 |
-
" How do we know what we know about this topic?",
|
431 |
-
" What are the sources and limits of our understanding?",
|
432 |
-
"",
|
433 |
-
"**• Ethical Considerations:**",
|
434 |
-
" What moral implications and responsibilities arise?",
|
435 |
-
" How do we balance competing values and interests?",
|
436 |
-
"",
|
437 |
-
"**• Metaphysical Questions:**",
|
438 |
-
" What does this reveal about the nature of reality?",
|
439 |
-
" How does this relate to fundamental questions of existence?",
|
440 |
-
""
|
441 |
-
])
|
442 |
-
|
443 |
-
# Connect to major philosophical traditions
|
444 |
-
response.extend([
|
445 |
-
"**🏛️ Historical Wisdom:**",
|
446 |
-
"• **Ancient Philosophy**: Socratic questioning and Aristotelian analysis",
|
447 |
-
"• **Modern Thought**: Enlightenment rationalism and empiricism",
|
448 |
-
"• **Contemporary Debates**: Current philosophical discourse and emerging paradigms",
|
449 |
-
""
|
450 |
-
])
|
451 |
-
|
452 |
-
response.append("*Philosophy helps us examine not just what we think, but how and why we think it.*")
|
453 |
-
|
454 |
-
return "\n".join(response)
|
455 |
-
|
456 |
-
def generate_analytical_response(self, query, extraction, real_time_data, reasoning_process):
|
457 |
-
"""Generates comprehensive analytical responses"""
|
458 |
-
domain = extraction["domain"]
|
459 |
-
topics = extraction["topics"]
|
460 |
-
|
461 |
-
response = []
|
462 |
-
|
463 |
-
# Analytical framework header
|
464 |
-
response.append("🔬 **Comprehensive Analysis:**")
|
465 |
-
response.append(f"*{reasoning_process['decomposition']}*")
|
466 |
-
response.append("")
|
467 |
-
|
468 |
-
# Multi-dimensional analysis
|
469 |
-
response.append("**📊 Multi-Dimensional Analysis:**")
|
470 |
-
response.append("")
|
471 |
-
|
472 |
-
# Domain-specific analysis dimensions
|
473 |
-
if domain == "current_affairs":
|
474 |
-
dimensions = [
|
475 |
-
("Political Dimension", "Power dynamics, governance structures, and policy implications"),
|
476 |
-
("Economic Dimension", "Market forces, resource allocation, and financial impacts"),
|
477 |
-
("Social Dimension", "Cultural factors, public opinion, and societal effects"),
|
478 |
-
("Historical Context", "Past patterns, precedents, and long-term trends")
|
479 |
-
]
|
480 |
-
elif domain == "science_and_technology":
|
481 |
-
dimensions = [
|
482 |
-
("Technical Aspects", "Core mechanisms, capabilities, and limitations"),
|
483 |
-
("Innovation Potential", "Breakthrough possibilities and future developments"),
|
484 |
-
("Ethical Implications", "Responsible development and potential risks"),
|
485 |
-
("Societal Impact", "Transformative effects on daily life and society")
|
486 |
-
]
|
487 |
-
else:
|
488 |
-
dimensions = [
|
489 |
-
("Core Components", "Fundamental elements and structures"),
|
490 |
-
("Interconnections", "Relationships and system dynamics"),
|
491 |
-
("Implications", "Consequences and broader significance"),
|
492 |
-
("Future Directions", "Emerging trends and possibilities")
|
493 |
-
]
|
494 |
-
|
495 |
-
for dim_name, dim_desc in dimensions:
|
496 |
-
response.append(f"**{dim_name}:**")
|
497 |
-
response.append(f" {dim_desc}")
|
498 |
-
response.append("")
|
499 |
-
|
500 |
-
# Evidence from real-time data
|
501 |
-
if real_time_data:
|
502 |
-
response.append("**📡 Current Evidence Base:**")
|
503 |
-
relevant_data = [item for item in real_time_data if item["domain"] == domain][:3]
|
504 |
-
for item in relevant_data:
|
505 |
-
response.append(f"• **[{item['source']}]** {item['title']}")
|
506 |
-
response.append("")
|
507 |
-
|
508 |
-
# Synthesis and insights
|
509 |
-
response.extend([
|
510 |
-
"**💡 Key Insights:**",
|
511 |
-
f"• **Complexity Level**: High - multiple interacting factors in {domain}",
|
512 |
-
f"• **Certainty Level**: Moderate - based on available evidence and analysis",
|
513 |
-
f"• **Significance**: Important implications for understanding {', '.join(topics[:2]) if topics else 'this topic'}",
|
514 |
-
""
|
515 |
-
])
|
516 |
-
|
517 |
-
# Expert-level considerations
|
518 |
-
if domain in self.expertise_levels:
|
519 |
-
expertise = self.expertise_levels[domain]
|
520 |
-
if expertise > 0.85:
|
521 |
-
response.extend([
|
522 |
-
"**🎓 Expert-Level Considerations:**",
|
523 |
-
"• Advanced theoretical frameworks and cutting-edge research",
|
524 |
-
"• Nuanced understanding of domain-specific methodologies",
|
525 |
-
"• Integration with interdisciplinary perspectives",
|
526 |
-
""
|
527 |
-
])
|
528 |
-
|
529 |
-
response.append("*This analysis draws from comprehensive knowledge across multiple disciplines and current data.*")
|
530 |
-
|
531 |
-
return "\n".join(response)
|
532 |
-
|
533 |
-
def generate_fallback_response(self, query):
|
534 |
-
"""Graceful fallback for complex or unclear queries"""
|
535 |
-
return f"""
|
536 |
-
I'm processing your question about "{query[:50]}..."
|
537 |
-
|
538 |
-
While I have extensive knowledge across many domains, I want to provide you with the most accurate and helpful response.
|
539 |
|
540 |
-
|
541 |
-
|
542 |
-
• Providing a bit more context about what you're looking for
|
543 |
-
• Letting me know if you prefer a technical or general explanation
|
544 |
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
|
563 |
-
|
564 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
565 |
|
566 |
-
def
|
567 |
-
"""
|
568 |
-
|
569 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
570 |
|
571 |
-
|
572 |
-
return response
|
573 |
|
574 |
-
#
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
|
602 |
-
|
603 |
-
|
604 |
-
|
605 |
-
|
606 |
-
|
607 |
-
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
-
|
612 |
-
|
613 |
-
|
614 |
-
|
615 |
-
|
616 |
-
|
617 |
-
|
618 |
-
|
619 |
-
|
620 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
621 |
)
|
622 |
-
)
|
623 |
|
624 |
if __name__ == "__main__":
|
625 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import json
|
|
|
4 |
import re
|
5 |
import xml.etree.ElementTree as ET
|
6 |
+
import numpy as np
|
7 |
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 TokenPredictor:
|
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 semplificato per predizione
|
24 |
+
self.embedding_dim = 256
|
25 |
+
self.hidden_dim = 512
|
26 |
+
self.context_length = 32
|
27 |
+
|
28 |
+
# Parametri del network (pesi)
|
29 |
+
self.embeddings = None
|
30 |
+
self.hidden_weights = None
|
31 |
+
self.output_weights = None
|
32 |
+
|
33 |
+
# Pattern database per apprendimento
|
34 |
+
self.token_patterns = defaultdict(list) # token -> [next_tokens]
|
35 |
+
self.bigram_counts = defaultdict(Counter) # token -> {next_token: count}
|
36 |
+
self.trigram_counts = defaultdict(Counter) # (tok1,tok2) -> {next_token: count}
|
37 |
+
|
38 |
+
# Dataset sources (pubblici, no API key)
|
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 |
+
"academic_arxiv": "https://arxiv.org/list/cs/recent",
|
49 |
+
"reddit_json": "https://files.pushshift.io/reddit/",
|
50 |
+
"opensubtitles": "https://opus.nlpl.eu/OpenSubtitles.php",
|
51 |
+
"common_crawl": "https://data.commoncrawl.org/crawl-data/"
|
52 |
}
|
53 |
|
54 |
+
# Data collection stats
|
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 con pesi casuali"""
|
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 inizializzato con pesi casuali")
|
80 |
+
|
81 |
+
def collect_quality_data(self, max_tokens=1000000):
|
82 |
+
"""Raccoglie dati di qualità da fonti pubbliche"""
|
83 |
+
print("🕷️ Iniziando raccolta dati da fonti pubbliche...")
|
84 |
+
self.collection_active = True
|
85 |
+
collected_texts = []
|
86 |
+
|
87 |
+
# 1. News RSS feeds (real-time, alta qualità)
|
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 abstracts (altissima qualità)
|
93 |
+
wiki_texts = self.scrape_wikipedia_samples()
|
94 |
+
collected_texts.extend(wiki_texts)
|
95 |
+
print(f"📚 Raccolti {len(wiki_texts)} abstract Wikipedia")
|
96 |
+
|
97 |
+
# 3. ArXiv papers abstracts (qualità accademica)
|
98 |
+
arxiv_texts = self.scrape_arxiv_abstracts()
|
99 |
+
collected_texts.extend(arxiv_texts)
|
100 |
+
print(f"🔬 Raccolti {len(arxiv_texts)} abstract ArXiv")
|
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 = []
|
113 |
+
for text in quality_texts:
|
114 |
+
tokens = self.tokenize_text(text)
|
115 |
+
all_tokens.extend(tokens)
|
116 |
+
if len(all_tokens) >= max_tokens:
|
117 |
break
|
118 |
|
119 |
+
self.total_tokens_collected = len(all_tokens)
|
120 |
+
print(f"🎯 Raccolti {self.total_tokens_collected:,} token di qualità")
|
121 |
+
|
122 |
+
# Build vocabulary
|
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 RSS news feeds per contenuto di qualità"""
|
133 |
+
texts = []
|
134 |
+
|
135 |
+
for rss_url in self.data_sources["news_rss"][:2]: # Limit per demo
|
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")[:5]:
|
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 scrape_wikipedia_samples(self):
|
154 |
+
"""Scrape Wikipedia content (sample)"""
|
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(3): # 3 articoli casuali
|
165 |
+
response = requests.get(wiki_api_urls[0], timeout=5)
|
166 |
+
if response.status_code == 200:
|
167 |
+
data = response.json()
|
168 |
+
if 'extract' in data:
|
169 |
+
texts.append(self.clean_text(data['extract']))
|
170 |
+
except:
|
171 |
+
pass
|
172 |
+
|
173 |
+
return texts
|
|
|
174 |
|
175 |
+
def scrape_arxiv_abstracts(self):
|
176 |
+
"""Scrape ArXiv abstracts (sample)"""
|
177 |
+
texts = []
|
178 |
+
|
179 |
+
# ArXiv RSS feed per CS papers
|
180 |
+
arxiv_rss = "http://export.arxiv.org/rss/cs"
|
181 |
+
|
182 |
+
try:
|
183 |
+
response = requests.get(arxiv_rss, timeout=5)
|
184 |
+
if response.status_code == 200:
|
185 |
+
root = ET.fromstring(response.content)
|
186 |
+
for item in root.findall(".//item")[:3]:
|
187 |
+
description = item.find("description")
|
188 |
+
if description is not None:
|
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 texts
|
198 |
|
199 |
+
def scrape_gutenberg_samples(self):
|
200 |
+
"""Scrape Project Gutenberg public domain texts (sample)"""
|
201 |
+
texts = []
|
202 |
+
|
203 |
+
# Sample di testi Gutenberg famosi (public domain)
|
204 |
+
gutenberg_samples = [
|
205 |
+
"https://www.gutenberg.org/files/11/11-0.txt", # Alice in Wonderland
|
206 |
+
"https://www.gutenberg.org/files/74/74-0.txt", # Tom Sawyer
|
207 |
+
"https://www.gutenberg.org/files/1342/1342-0.txt", # Pride and Prejudice
|
208 |
+
]
|
209 |
+
|
210 |
+
for url in gutenberg_samples[:1]: # Solo 1 per demo
|
211 |
+
try:
|
212 |
+
response = requests.get(url, timeout=10)
|
213 |
+
if response.status_code == 200:
|
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 |
+
return texts
|
223 |
|
224 |
+
def clean_text(self, text):
|
225 |
+
"""Pulisce e normalizza il testo"""
|
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 testi per qualità"""
|
245 |
+
quality_texts = []
|
246 |
+
|
247 |
+
for text in texts:
|
248 |
+
score = self.calculate_quality_score(text)
|
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 di qualità del testo"""
|
256 |
+
if not text or len(text) < 50:
|
257 |
+
return 0.0
|
258 |
+
|
259 |
+
score = 0.0
|
260 |
+
|
261 |
+
# Length score (optimal 100-5000 chars)
|
262 |
+
length = len(text)
|
263 |
+
if 100 <= length <= 5000:
|
264 |
+
score += 0.3
|
265 |
+
elif length > 50:
|
266 |
+
score += 0.1
|
267 |
+
|
268 |
+
# Language quality (proportion of dictionary words)
|
269 |
+
words = text.lower().split()
|
270 |
+
if words:
|
271 |
+
# Simple English word detection
|
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 (has proper punctuation)
|
277 |
+
sentences = re.split(r'[.!?]+', text)
|
278 |
+
if len(sentences) > 1:
|
279 |
+
score += 0.2
|
280 |
+
|
281 |
+
# Avoid repetitive text
|
282 |
+
word_set = set(words) if words else set()
|
283 |
+
if words and len(word_set) / len(words) > 0.5: # Vocabulary diversity
|
284 |
+
score += 0.1
|
285 |
+
|
286 |
+
return score
|
287 |
|
288 |
+
def is_likely_english_word(self, word):
|
289 |
+
"""Simple heuristic per English words"""
|
290 |
+
word = re.sub(r'[^\w]', '', word.lower())
|
291 |
+
if len(word) < 2:
|
292 |
+
return False
|
293 |
+
|
294 |
+
# Basic English patterns
|
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 il testo in token"""
|
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 da tokens"""
|
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 costruito: {self.vocab_size:,} token unici")
|
327 |
+
|
328 |
+
def extract_training_patterns(self, tokens):
|
329 |
+
"""Estrae pattern per training prediction"""
|
330 |
+
print("🔍 Estraendo pattern per training...")
|
331 |
+
|
332 |
+
# Convert tokens to IDs
|
333 |
+
token_ids = [self.token_to_id.get(token, 1) for token in tokens] # 1 = <UNK>
|
334 |
+
|
335 |
+
# Extract bigrams
|
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 |
+
# Extract trigrams
|
342 |
+
for i in range(len(token_ids) - 2):
|
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 |
+
print(f"📊 Pattern estratti:")
|
348 |
+
print(f" Bigrams: {len(self.bigram_counts):,}")
|
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 |
+
for epoch in range(epochs):
|
356 |
+
epoch_loss = 0.0
|
357 |
+
batch_count = 0
|
358 |
|
359 |
+
# Training su sequenze
|
360 |
+
for i in range(0, len(training_sequences) - self.context_length, 10):
|
361 |
+
# Create input/target pairs
|
362 |
+
input_sequence = training_sequences[i:i + self.context_length]
|
363 |
+
target_token = training_sequences[i + self.context_length]
|
364 |
+
|
365 |
+
# Forward pass
|
366 |
+
prediction_probs = self.forward_pass(input_sequence)
|
367 |
+
|
368 |
+
# Calculate loss
|
369 |
+
loss = self.calculate_loss(prediction_probs, target_token)
|
370 |
+
epoch_loss += loss
|
371 |
+
|
372 |
+
# Backward pass (simplified)
|
373 |
+
self.backward_pass(input_sequence, target_token, prediction_probs)
|
374 |
+
|
375 |
+
batch_count += 1
|
376 |
+
|
377 |
+
if batch_count % 100 == 0:
|
378 |
+
print(f" Epoch {epoch+1}, Batch {batch_count}, Loss: {loss:.4f}")
|
379 |
|
380 |
+
avg_loss = epoch_loss / batch_count if batch_count > 0 else 0
|
381 |
+
self.training_loss.append(avg_loss)
|
382 |
+
self.epochs_trained += 1
|
383 |
|
384 |
+
print(f"🎯 Epoch {epoch+1} completato, Loss medio: {avg_loss:.4f}")
|
385 |
+
|
386 |
+
print("✅ Training completato!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
387 |
|
388 |
+
def forward_pass(self, input_sequence):
|
389 |
+
"""Forward pass della rete neurale"""
|
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)) # Numerical stability
|
412 |
+
probabilities = exp_logits / np.sum(exp_logits)
|
413 |
|
414 |
+
return probabilities
|
415 |
+
|
416 |
+
def calculate_loss(self, predictions, target_token):
|
417 |
+
"""Calcola cross-entropy loss"""
|
418 |
+
# Ensure target_token is in valid range
|
419 |
+
if target_token >= len(predictions):
|
420 |
+
target_token = 1 # <UNK>
|
421 |
+
|
422 |
+
# Cross-entropy loss
|
423 |
+
return -np.log(predictions[target_token] + 1e-10) # Small epsilon per numerical stability
|
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 |
+
try:
|
464 |
+
prediction_probs = self.forward_pass(input_sequence)
|
465 |
+
|
466 |
+
# Get top predictions
|
467 |
+
top_indices = np.argsort(prediction_probs)[-num_predictions:][::-1]
|
468 |
+
predictions = []
|
469 |
+
|
470 |
+
for idx in top_indices:
|
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 |
+
return predictions
|
477 |
+
except:
|
478 |
+
pass
|
479 |
+
|
480 |
+
# Fallback: use pattern matching
|
481 |
+
if len(context_ids) >= 2:
|
482 |
+
# Try trigram
|
483 |
+
last_bigram = (context_ids[-2], context_ids[-1])
|
484 |
+
if last_bigram in self.trigram_counts:
|
485 |
+
most_common = self.trigram_counts[last_bigram].most_common(num_predictions)
|
486 |
+
return [f"{self.vocabulary.get(token_id, '<UNK>')} ({count})"
|
487 |
+
for token_id, count in most_common]
|
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 |
+
"trigram_patterns": len(self.trigram_counts),
|
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 Token Predictor
|
514 |
+
predictor = TokenPredictor()
|
|
|
|
|
515 |
|
516 |
+
def collect_and_train():
|
517 |
+
"""Funzione per raccolta dati e training"""
|
518 |
+
try:
|
519 |
+
# Phase 1: Data collection
|
520 |
+
tokens = predictor.collect_quality_data(max_tokens=50000) # Limit per demo
|
521 |
+
|
522 |
+
if len(tokens) > 100:
|
523 |
+
# Phase 2: Training
|
524 |
+
predictor.train_neural_network(
|
525 |
+
[predictor.token_to_id.get(token, 1) for token in tokens],
|
526 |
+
epochs=3
|
527 |
+
)
|
528 |
+
return "✅ Raccolta dati e training completati!"
|
529 |
+
else:
|
530 |
+
return "❌ Dati insufficienti raccolti"
|
531 |
+
except Exception as e:
|
532 |
+
return f"❌ Errore: {str(e)}"
|
533 |
|
534 |
+
def predict_interface(context_text):
|
535 |
+
"""Interface per predizione"""
|
536 |
+
if not context_text.strip():
|
537 |
+
return "Inserisci del testo per ottenere predizioni del prossimo token."
|
538 |
+
|
539 |
+
predictions = predictor.predict_next_token(context_text)
|
540 |
+
|
541 |
+
result = f"**🎯 Predizioni per:** '{context_text}'\n\n"
|
542 |
+
result += "**📊 Top token predetti:**\n"
|
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 get_model_status():
|
561 |
+
"""Ritorna status del modello"""
|
562 |
+
stats = predictor.get_training_stats()
|
563 |
+
|
564 |
+
status = "🤖 **STATUS DEL MODELLO TOKEN PREDICTOR**\n\n"
|
565 |
+
|
566 |
+
if stats['collection_active']:
|
567 |
+
status += "🔄 **Raccolta dati in corso...**\n\n"
|
568 |
+
elif stats['total_tokens'] == 0:
|
569 |
+
status += "⏳ **Modello non addestrato**\nClicca 'Avvia Training' per iniziare\n\n"
|
570 |
+
else:
|
571 |
+
status += "✅ **Modello addestrato e pronto**\n\n"
|
572 |
+
|
573 |
+
status += "**📊 Statistiche:**\n"
|
574 |
+
status += f"• **Token raccolti:** {stats['total_tokens']:,}\n"
|
575 |
+
status += f"• **Vocabulary:** {stats['vocabulary_size']:,} token unici\n"
|
576 |
+
status += f"• **Pattern appresi:** {stats['bigram_patterns']:,} bigram, {stats['trigram_patterns']:,} trigram\n"
|
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 += "• Predizione next token da contesto\n"
|
584 |
+
status += "• Pattern recognition da milioni di token\n"
|
585 |
+
status += "• Neural network con embeddings 256D\n"
|
586 |
+
status += "• Training su dati pubblici di qualità\n"
|
587 |
|
588 |
+
return status
|
|
|
589 |
|
590 |
+
# Gradio Interface
|
591 |
+
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>🧠 Token Predictor AI</h1>
|
596 |
+
<p><b>Neural Network che impara a predire il prossimo token</b></p>
|
597 |
+
<p>Input: Milioni di token da database pubblici → Process: Auto-organizzazione neurale → Output: Predizione intelligente</p>
|
598 |
+
</div>
|
599 |
+
""")
|
600 |
+
|
601 |
+
with gr.Row():
|
602 |
+
with gr.Column(scale=2):
|
603 |
+
gr.HTML("<h3>🎯 Token Prediction</h3>")
|
604 |
+
|
605 |
+
context_input = gr.Textbox(
|
606 |
+
label="Contesto",
|
607 |
+
placeholder="Es: The capital of France is",
|
608 |
+
lines=2
|
609 |
+
)
|
610 |
+
|
611 |
+
predict_btn = gr.Button("🔮 Predici Next Token", variant="primary")
|
612 |
+
|
613 |
+
prediction_output = gr.Textbox(
|
614 |
+
label="Predizioni",
|
615 |
+
lines=10,
|
616 |
+
interactive=False
|
617 |
+
)
|
618 |
+
|
619 |
+
with gr.Column(scale=1):
|
620 |
+
gr.HTML("<h3>⚙️ Training & Status</h3>")
|
621 |
+
|
622 |
+
status_output = gr.Textbox(
|
623 |
+
label="Status Modello",
|
624 |
+
lines=15,
|
625 |
+
interactive=False,
|
626 |
+
value=get_model_status()
|
627 |
+
)
|
628 |
+
|
629 |
+
train_btn = gr.Button("🚀 Avvia Data Collection & Training", variant="secondary")
|
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>🔬 Come Funziona:</h4>
|
635 |
+
<ol>
|
636 |
+
<li><b>Data Collection:</b> Raccoglie token da fonti pubbliche (RSS news, Wikipedia, ArXiv, Project Gutenberg)</li>
|
637 |
+
<li><b>Quality Filtering:</b> Filtra contenuti per qualità linguistica e strutturale</li>
|
638 |
+
<li><b>Tokenization:</b> Converte testo in token discreti</li>
|
639 |
+
<li><b>Pattern Extraction:</b> Estrae bigram e trigram per apprendimento</li>
|
640 |
+
<li><b>Neural Training:</b> Addestra rete neurale per predizione next token</li>
|
641 |
+
<li><b>Prediction:</b> Usa pattern appresi per predire token successivi</li>
|
642 |
+
</ol>
|
643 |
+
<p><b>🎯 Obiettivo:</b> AI che predice bene il prossimo token tramite auto-organizzazione neurale su milioni di esempi!</p>
|
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 |
+
predict_btn.click(
|
662 |
+
predict_interface,
|
663 |
+
inputs=[context_input],
|
664 |
+
outputs=[prediction_output]
|
665 |
+
)
|
666 |
+
|
667 |
+
train_btn.click(
|
668 |
+
collect_and_train,
|
669 |
+
outputs=[status_output]
|
670 |
+
)
|
671 |
+
|
672 |
+
refresh_btn.click(
|
673 |
+
get_model_status,
|
674 |
+
outputs=[status_output]
|
675 |
)
|
|
|
676 |
|
677 |
if __name__ == "__main__":
|
678 |
+
demo.launch()
|