Spaces:
Sleeping
Sleeping
import gradio as gr | |
from sentence_transformers import SentenceTransformer | |
from scipy.spatial.distance import cosine | |
from sklearn.metrics.pairwise import cosine_similarity | |
import nltk | |
# Carica il modello | |
model = SentenceTransformer('sentence-transformers/all-roberta-large-v1', device='cpu') | |
nltk.download('punkt') | |
# Preprocessamento manuale (potresti caricare il manuale da un file o base di dati) | |
with open('manual.txt', 'r', encoding='utf-8') as file: | |
text = file.read() | |
# Tokenizza il testo | |
sentences = nltk.sent_tokenize(text) | |
# Crea gli embedding per il manuale | |
embeddings = model.encode(sentences, batch_size=8, show_progress_bar=True) | |
# Funzione per ottenere le frasi più rilevanti | |
def find_relevant_sentences(query): | |
query_embedding = model.encode([query]) | |
similarities = cosine_similarity(query_embedding, embeddings).flatten() | |
# Filtra i risultati in base alla similitudine | |
threshold = 0.5 | |
filtered_results = [(idx, sim) for idx, sim in enumerate(similarities) if sim >= threshold] | |
# Ordina i risultati per similitudine | |
filtered_results.sort(key=lambda x: x[1], reverse=True) | |
# Ottieni le frasi più rilevanti | |
top_n = 4 | |
relevant_sentences = [sentences[idx] for idx, _ in filtered_results[:top_n]] | |
return relevant_sentences | |
# Interfaccia Gradio | |
iface = gr.Interface( | |
fn=find_relevant_sentences, | |
inputs=gr.Textbox(label="Insert your query"), | |
outputs=gr.Textbox(label="Relevant sentences"), | |
title="Manual Querying System", | |
description="Enter a question about the machine, and this tool will find the most relevant sentences from the manual." | |
) | |
# Avvia l'app Gradio | |
iface.launch() |