Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,78 +7,35 @@ from sklearn.metrics.pairwise import cosine_similarity
|
|
7 |
import numpy as np
|
8 |
import zipfile
|
9 |
|
10 |
-
# Percorso del file zip
|
11 |
-
zip_path = './en_core_web_sm.zip'
|
12 |
|
13 |
-
#
|
14 |
-
|
|
|
|
|
|
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
# Carica il modello Spacy
|
21 |
-
nlp = spacy.load(extraction_dir)
|
22 |
|
|
|
|
|
|
|
23 |
|
24 |
-
#
|
25 |
-
|
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 |
-
]
|
72 |
|
73 |
# Interfaccia Gradio
|
74 |
-
|
75 |
-
fn=
|
76 |
-
inputs=gr.
|
77 |
-
outputs=
|
78 |
-
|
79 |
-
|
80 |
-
description="Enter a question about the machine, and this tool will find the most relevant sentences from the manual."
|
81 |
)
|
82 |
|
83 |
-
# Avvia l'
|
84 |
-
|
|
|
|
|
|
7 |
import numpy as np
|
8 |
import zipfile
|
9 |
|
|
|
|
|
10 |
|
11 |
+
# Funzione per estrarre il file ZIP
|
12 |
+
def extract_zip(file):
|
13 |
+
# Verifica che il file sia uno ZIP valido
|
14 |
+
if not zipfile.is_zipfile(file.name):
|
15 |
+
return "Errore: Il file caricato non è uno ZIP valido."
|
16 |
|
17 |
+
# Directory di estrazione
|
18 |
+
extraction_dir = "./extracted_files"
|
19 |
+
os.makedirs(extraction_dir, exist_ok=True)
|
|
|
|
|
|
|
20 |
|
21 |
+
# Estrazione dei file
|
22 |
+
with zipfile.ZipFile(file.name, 'r') as zip_ref:
|
23 |
+
zip_ref.extractall(extraction_dir)
|
24 |
|
25 |
+
# Lista dei file estratti
|
26 |
+
extracted_files = os.listdir(extraction_dir)
|
27 |
+
return f"File estratti con successo in '{extraction_dir}':\n" + "\n".join(extracted_files)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
# Interfaccia Gradio
|
30 |
+
interface = gr.Interface(
|
31 |
+
fn=extract_zip,
|
32 |
+
inputs=gr.File(label="Carica il file ZIP"),
|
33 |
+
outputs="text",
|
34 |
+
title="Estrattore di File ZIP",
|
35 |
+
description="Carica un file ZIP e verrà estratto in una directory chiamata 'extracted_files'."
|
|
|
36 |
)
|
37 |
|
38 |
+
# Avvia l'applicazione Gradio
|
39 |
+
if __name__ == "__main__":
|
40 |
+
interface.launch()
|
41 |
+
|