Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,5 +7,79 @@ from sklearn.metrics.pairwise import cosine_similarity
|
|
7 |
import numpy as np
|
8 |
import zipfile
|
9 |
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
import numpy as np
|
8 |
import zipfile
|
9 |
|
10 |
+
# Percorso del file zip
|
11 |
+
zip_path = '/mnt/data/en_core_web_sm.zip' # o il percorso che hai trovato
|
12 |
+
|
13 |
+
# Directory in cui estrarre il modello
|
14 |
+
extraction_dir = '/mnt/data/en_core_web_sm' # o il percorso di estrazione scelto
|
15 |
+
|
16 |
+
# Estrai il file zip
|
17 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
18 |
+
zip_ref.extractall(extraction_dir)
|
19 |
+
|
20 |
+
# Carica il modello Spacy
|
21 |
+
nlp = spacy.load(extraction_dir)
|
22 |
+
|
23 |
+
|
24 |
+
# Carica il modello SentenceTransformer
|
25 |
+
model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2', device='cpu')
|
26 |
+
|
27 |
+
# Preprocessamento manuale (carica il manuale da un file o base di dati)
|
28 |
+
with open('testo.txt', 'r', encoding='utf-8') as file:
|
29 |
+
text = file.read()
|
30 |
+
|
31 |
+
# Tokenizza il testo in frasi usando SpaCy
|
32 |
+
doc = nlp(text)
|
33 |
+
sentences = [sent.text for sent in doc.sents] # Estrarre frasi dal testo
|
34 |
+
|
35 |
+
# Crea gli embedding per il manuale
|
36 |
+
embeddings = model.encode(sentences, batch_size=8, show_progress_bar=True)
|
37 |
+
|
38 |
+
# Funzione per ottenere le frasi più rilevanti
|
39 |
+
# Funzione per ottenere le frasi più rilevanti
|
40 |
+
def find_relevant_sentences(query):
|
41 |
+
query_embedding = model.encode([query])
|
42 |
+
similarities = cosine_similarity(query_embedding, embeddings).flatten()
|
43 |
+
|
44 |
+
# Filtra i risultati in base alla similitudine
|
45 |
+
threshold = 0.2
|
46 |
+
filtered_results = [(idx, sim) for idx, sim in enumerate(similarities) if sim >= threshold]
|
47 |
+
|
48 |
+
if not filtered_results: # Se nessun risultato supera la soglia
|
49 |
+
return ["No relevant sentences found."]
|
50 |
+
|
51 |
+
# Ordina i risultati per similitudine
|
52 |
+
filtered_results.sort(key=lambda x: x[1], reverse=True)
|
53 |
+
|
54 |
+
# Limita i risultati alle top_n frasi
|
55 |
+
top_n = 4
|
56 |
+
relevant_sentences = [sentences[idx] for idx, _ in filtered_results[:top_n]]
|
57 |
+
|
58 |
+
# Rimuove duplicati e segmenta in frasi
|
59 |
+
unique_sentences = list(dict.fromkeys(relevant_sentences)) # Mantiene l'ordine
|
60 |
+
doc = nlp(" ".join(unique_sentences))
|
61 |
+
grouped_results = [sent.text.strip() for sent in doc.sents]
|
62 |
+
|
63 |
+
return grouped_results
|
64 |
+
|
65 |
+
|
66 |
+
examples = [
|
67 |
+
["irresponsible use of the machine?"],
|
68 |
+
["If I have a problem how can I get help? "],
|
69 |
+
["precautions when using the cutting machine"],
|
70 |
+
["How do I change the knife of the cutting machine?"]
|
71 |
+
["Uso irresponsable de la máquina cortadora ?"]
|
72 |
+
]
|
73 |
+
|
74 |
+
# Interfaccia Gradio
|
75 |
+
iface = gr.Interface(
|
76 |
+
fn=find_relevant_sentences,
|
77 |
+
inputs=gr.Textbox(label="Insert your query"),
|
78 |
+
outputs=gr.Textbox(label="Relevant sentences"),
|
79 |
+
examples=examples,
|
80 |
+
title="Manual Querying System",
|
81 |
+
description="Enter a question about the machine, and this tool will find the most relevant sentences from the manual."
|
82 |
+
)
|
83 |
+
|
84 |
+
# Avvia l'app Gradio
|
85 |
+
iface.launch()
|