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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
from bs4 import BeautifulSoup
|
| 6 |
+
import spacy
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
|
| 10 |
+
# Inizializza modelli NLP e di summarization
|
| 11 |
+
nlp = spacy.load("en_core_web_sm")
|
| 12 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 13 |
+
|
| 14 |
+
class ConversationalUBRA:
|
| 15 |
+
def __init__(self):
|
| 16 |
+
self.conversation_history = []
|
| 17 |
+
self.sources = {
|
| 18 |
+
'duckduckgo': True,
|
| 19 |
+
'wikipedia': True,
|
| 20 |
+
'newsapi': False,
|
| 21 |
+
'google_scholar': False
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
def analyze_intent(self, query):
|
| 25 |
+
"""Analizza l'intento della query"""
|
| 26 |
+
doc = nlp(query)
|
| 27 |
+
|
| 28 |
+
intents = {
|
| 29 |
+
'information_request': any(token.pos_ in ['NOUN', 'PROPN'] for token in doc),
|
| 30 |
+
'comparison': any(word in query for word in ['vs', 'comparare', 'confrontare']),
|
| 31 |
+
'definition': any(word in query for word in ['cos\'è', 'significa', 'definizione']),
|
| 32 |
+
'how_to': any(word in query for word in ['come', 'funziona', 'procedura']),
|
| 33 |
+
'opinion': any(word in query for word in ['opinione', 'credi', 'pensiero'])
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
primary_intent = max(intents, key=intents.get) if any(intents.values()) else 'general'
|
| 37 |
+
|
| 38 |
+
return {
|
| 39 |
+
'primary': primary_intent,
|
| 40 |
+
'keywords': [token.lemma_.lower() for token in doc if not token.is_stop]
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
def collect_information(self, query, intent):
|
| 44 |
+
"""Raccolta dati da fonti attive"""
|
| 45 |
+
data_sources = []
|
| 46 |
+
|
| 47 |
+
if self.sources['duckduckgo']:
|
| 48 |
+
data_sources.extend(self.search_duckduckgo(query))
|
| 49 |
+
|
| 50 |
+
if self.sources['wikipedia']:
|
| 51 |
+
data_sources.extend(self.search_wikipedia(query))
|
| 52 |
+
|
| 53 |
+
if self.sources['newsapi']:
|
| 54 |
+
data_sources.extend(self.search_newsapi(query))
|
| 55 |
+
|
| 56 |
+
if self.sources['google_scholar']:
|
| 57 |
+
data_sources.extend(self.search_google_scholar(query))
|
| 58 |
+
|
| 59 |
+
return data_sources
|
| 60 |
+
|
| 61 |
+
def search_duckduckgo(self, query):
|
| 62 |
+
"""Ricerca su DuckDuckGo"""
|
| 63 |
+
try:
|
| 64 |
+
url = f"https://duckduckgo.com/html?q={query}"
|
| 65 |
+
headers = {'User-Agent': 'Mozilla/5.0'}
|
| 66 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 67 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 68 |
+
|
| 69 |
+
results = []
|
| 70 |
+
for item in soup.select('.result__body')[:3]:
|
| 71 |
+
title = item.select_one('.result__title').get_text(strip=True)
|
| 72 |
+
snippet = item.select_one('.result__snippet').get_text(strip=True)
|
| 73 |
+
link = item.select_one('.result__url').get_text(strip=True)
|
| 74 |
+
results.append(f"🌐 DuckDuckGo:\n{title}\n{snippet}\nLink: {link}\n")
|
| 75 |
+
|
| 76 |
+
return results
|
| 77 |
+
except Exception as e:
|
| 78 |
+
return [f"⚠️ Errore DuckDuckGo: {str(e)}"]
|
| 79 |
+
|
| 80 |
+
def search_wikipedia(self, query):
|
| 81 |
+
"""Ricerca su Wikipedia"""
|
| 82 |
+
try:
|
| 83 |
+
url = f"https://it.wikipedia.org/w/api.php?action=query&list=search&srsearch={query}&format=json&srlimit=3"
|
| 84 |
+
response = requests.get(url, timeout=10)
|
| 85 |
+
data = response.json()
|
| 86 |
+
|
| 87 |
+
results = []
|
| 88 |
+
if 'query' in data and 'search' in data['query']:
|
| 89 |
+
for item in data['query']['search'][:3]:
|
| 90 |
+
title = item['title']
|
| 91 |
+
snippet = item['snippet'].replace('<span class="searchmatch">', '').replace('</span>', '')
|
| 92 |
+
page_url = f"https://it.wikipedia.org/wiki/{title.replace(' ', '_')}"
|
| 93 |
+
results.append(f"📚 Wikipedia:\n{title}\n{snippet}\nLink: {page_url}\n")
|
| 94 |
+
|
| 95 |
+
return results
|
| 96 |
+
except Exception as e:
|
| 97 |
+
return [f"⚠️ Errore Wikipedia: {str(e)}"]
|
| 98 |
+
|
| 99 |
+
def search_newsapi(self, query):
|
| 100 |
+
"""Ricerca su NewsAPI (richiede API key)"""
|
| 101 |
+
try:
|
| 102 |
+
if not hasattr(self, 'newsapi_key'):
|
| 103 |
+
return ["⚠️ NewsAPI non configurato. Imposta la chiave API."]
|
| 104 |
+
|
| 105 |
+
url = f"https://newsapi.org/v2/everything?q={query}&apiKey={self.newsapi_key}"
|
| 106 |
+
response = requests.get(url, timeout=10)
|
| 107 |
+
data = response.json()
|
| 108 |
+
|
| 109 |
+
results = []
|
| 110 |
+
if 'articles' in data:
|
| 111 |
+
for article in data['articles'][:3]:
|
| 112 |
+
title = article['title']
|
| 113 |
+
description = article['description']
|
| 114 |
+
url = article['url']
|
| 115 |
+
results.append(f"📰 NewsAPI:\n{title}\n{description}\nLink: {url}\n")
|
| 116 |
+
|
| 117 |
+
return results
|
| 118 |
+
except Exception as e:
|
| 119 |
+
return [f"⚠️ Errore NewsAPI: {str(e)}"]
|
| 120 |
+
|
| 121 |
+
def search_google_scholar(self, query):
|
| 122 |
+
"""Ricerca su Google Scholar (richiede API)"""
|
| 123 |
+
try:
|
| 124 |
+
if not hasattr(self, 'scholar_cx') or not hasattr(self, 'scholar_key'):
|
| 125 |
+
return ["⚠️ Google Scholar non configurato. Imposta cx e chiave API."]
|
| 126 |
+
|
| 127 |
+
url = f"https://www.googleapis.com/customsearch/v1?key={self.scholar_key}&cx={self.scholar_cx}&q={query}"
|
| 128 |
+
response = requests.get(url, timeout=10)
|
| 129 |
+
data = response.json()
|
| 130 |
+
|
| 131 |
+
results = []
|
| 132 |
+
if 'items' in data:
|
| 133 |
+
for item in data['items'][:3]:
|
| 134 |
+
title = item['title']
|
| 135 |
+
snippet = item['snippet']
|
| 136 |
+
link = item['link']
|
| 137 |
+
results.append(f"📚 Google Scholar:\n{title}\n{snippet}\nLink: {link}\n")
|
| 138 |
+
|
| 139 |
+
return results
|
| 140 |
+
except Exception as e:
|
| 141 |
+
return [f"⚠️ Errore Google Scholar: {str(e)}"]
|
| 142 |
+
|
| 143 |
+
def generate_response(self, query):
|
| 144 |
+
"""Genera una risposta basata sull'intento"""
|
| 145 |
+
intent = self.analyze_intent(query)
|
| 146 |
+
data = self.collect_information(query, intent)
|
| 147 |
+
|
| 148 |
+
if not data:
|
| 149 |
+
return "Non sono riuscito a trovare informazioni rilevanti."
|
| 150 |
+
|
| 151 |
+
if intent['primary'] == 'comparison':
|
| 152 |
+
return self.process_comparison(data)
|
| 153 |
+
elif intent['primary'] == 'how_to':
|
| 154 |
+
return self.process_how_to(data)
|
| 155 |
+
elif intent['primary'] == 'opinion':
|
| 156 |
+
return self.process_opinion(data)
|
| 157 |
+
else:
|
| 158 |
+
return self.summarize_data(data)
|
| 159 |
+
|
| 160 |
+
def process_comparison(self, data):
|
| 161 |
+
"""Processa dati per confronti"""
|
| 162 |
+
comparisons = []
|
| 163 |
+
for item in data:
|
| 164 |
+
if 'vs' in item or 'confronto' in item.lower():
|
| 165 |
+
comparisons.append(item)
|
| 166 |
+
|
| 167 |
+
if not comparisons:
|
| 168 |
+
return "Non ho trovato informazioni dirette per confrontare questi elementi."
|
| 169 |
+
|
| 170 |
+
return "Ecco i principali punti di confronto:\n\n" + "\n\n".join(comparisons[:3])
|
| 171 |
+
|
| 172 |
+
def process_how_to(self, data):
|
| 173 |
+
"""Processa dati per procedure"""
|
| 174 |
+
procedures = []
|
| 175 |
+
for item in data:
|
| 176 |
+
if any(step_word in item.lower() for step_word in ['passo', 'step', 'procedura']):
|
| 177 |
+
procedures.append(item)
|
| 178 |
+
|
| 179 |
+
if not procedures:
|
| 180 |
+
return "Non ho trovato istruzioni dettagliate. Prova a cercare con parole chiave come 'guida', 'tutorial' o 'istruzioni'."
|
| 181 |
+
|
| 182 |
+
return "Ecco i passaggi principali:\n\n" + "\n\n".join(procedures[:3])
|
| 183 |
+
|
| 184 |
+
def process_opinion(self, data):
|
| 185 |
+
"""Sintetizza opinioni da diverse fonti"""
|
| 186 |
+
opinions = []
|
| 187 |
+
for item in data:
|
| 188 |
+
if any(opinion_word in item.lower() for opinion_word in ['opinione', 'pensiero', 'considerazione']):
|
| 189 |
+
opinions.append(item)
|
| 190 |
+
|
| 191 |
+
if not opinions:
|
| 192 |
+
return "Non ho trovato opinioni esplicite. Posso fornirti informazioni oggettive sulle fonti consultate."
|
| 193 |
+
|
| 194 |
+
return "Ecco alcune opinioni rilevate:\n\n" + "\n\n".join(opinions[:3])
|
| 195 |
+
|
| 196 |
+
def summarize_data(self, data):
|
| 197 |
+
"""Sommazzina i dati raccolti"""
|
| 198 |
+
if not data:
|
| 199 |
+
return "Non sono riuscito a trovare informazioni rilevanti per la tua query."
|
| 200 |
+
|
| 201 |
+
combined_text = "\n\n---\n\n".join(data)
|
| 202 |
+
|
| 203 |
+
if len(combined_text) > 300:
|
| 204 |
+
summary = summarizer(combined_text, max_length=500, min_length=100)[0]['summary_text']
|
| 205 |
+
return summary
|
| 206 |
+
else:
|
| 207 |
+
return combined_text
|
| 208 |
+
|
| 209 |
+
# Interfaccia Gradio
|
| 210 |
+
def create_app():
|
| 211 |
+
app = ConversationalUBRA()
|
| 212 |
+
|
| 213 |
+
def respond(message, history):
|
| 214 |
+
response = app.generate_response(message)
|
| 215 |
+
return "", history + [[message, response]]
|
| 216 |
+
|
| 217 |
+
iface = gr.ChatInterface(
|
| 218 |
+
fn=respond,
|
| 219 |
+
examples=[
|
| 220 |
+
"Spiega i benefici dell'intelligenza artificiale",
|
| 221 |
+
"Confronta le energie rinnovabili vs fossili",
|
| 222 |
+
"Come preparare un piano di business?",
|
| 223 |
+
"Definisci la sostenibilità aziendale"
|
| 224 |
+
],
|
| 225 |
+
title="UBRA - Assistente Conversazionale Intelligente",
|
| 226 |
+
description="Un AI che ricerca e sintetizza informazioni da fonti affidabili. Chiedi anything!"
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
return iface
|
| 230 |
+
|
| 231 |
+
if __name__ == "__main__":
|
| 232 |
+
app = create_app()
|
| 233 |
+
app.launch()
|