|
|
|
import os |
|
import spacy |
|
import gradio as gr |
|
from sentence_transformers import SentenceTransformer |
|
from sklearn.metrics.pairwise import cosine_similarity |
|
import numpy as np |
|
import zipfile |
|
|
|
|
|
zip_path = "en_core_web_lg-3.8.0.zip" |
|
extraction_dir = "./extracted_models" |
|
test_dir = "./extracted_models/en_core_web_lg-3.8.0" |
|
|
|
|
|
|
|
|
|
if not os.path.exists(test_dir): |
|
|
|
with zipfile.ZipFile(zip_path, 'r') as zip_ref: |
|
zip_ref.extractall(extraction_dir) |
|
print(f"Modello estratto correttamente nella cartella {extraction_dir}") |
|
|
|
|
|
model_path = os.path.join(extraction_dir, "en_core_web_lg-3.8.0") |
|
|
|
|
|
nlp = spacy.load(model_path) |
|
|
|
|
|
|
|
|
|
model = SentenceTransformer('sentence-transformers/paraphrase-multilingual-mpnet-base-v2', device='cpu') |
|
|
|
|
|
|
|
|
|
|
|
with open('testo.txt', 'r', encoding='utf-8') as file: |
|
text = file.read() |
|
|
|
|
|
doc = nlp(text) |
|
sentences = [sent.text for sent in doc.sents] |
|
|
|
|
|
embeddings = model.encode(sentences, batch_size=8, show_progress_bar=True) |
|
|
|
|
|
def find_relevant_sentences(query): |
|
query_embedding = model.encode([query]) |
|
similarities = cosine_similarity(query_embedding, embeddings).flatten() |
|
|
|
|
|
threshold = 0.2 |
|
filtered_results = [(idx, sim) for idx, sim in enumerate(similarities) if sim >= threshold] |
|
|
|
|
|
filtered_results.sort(key=lambda x: x[1], reverse=True) |
|
|
|
|
|
top_n = 5 |
|
relevant_sentences = [sentences[idx] for idx, _ in filtered_results[:top_n]] |
|
|
|
doc = nlp(" ".join(relevant_sentences)) |
|
|
|
grouped_results = [sent.text for sent in doc.sents] |
|
|
|
cleaned_results = [text.replace("\n", " ") for text in grouped_results] |
|
final_output = " ".join(cleaned_results) |
|
|
|
|
|
return final_output |
|
|
|
examples = [ |
|
["irresponsible use of the machine?"], |
|
["If I have a problem how can I get help? "], |
|
["precautions when using the cutting machine"], |
|
["How do I change the knife of the cutting machine?"], |
|
|
|
] |
|
|
|
|
|
iface = gr.Interface( |
|
fn=find_relevant_sentences, |
|
inputs=gr.Textbox(label="Insert your query"), |
|
outputs=gr.Textbox(label="Relevant sentences"), |
|
examples=examples, |
|
title="Manual Querying System", |
|
description="Enter a question about the machine, and this tool will find the most relevant sentences from the manual." |
|
) |
|
|
|
|
|
iface.launch() |
|
|