Spaces:
Sleeping
Sleeping
File size: 2,964 Bytes
38f8736 6c05acd 0cea8e5 0deea66 652437c 0deea66 0cea8e5 3818f5a 0cea8e5 3818f5a 0cea8e5 b474dc5 0cea8e5 b474dc5 0cea8e5 56adc9e 3d3f8f8 0cea8e5 3d3f8f8 3818f5a 0cea8e5 3d3f8f8 0cea8e5 ed1dd60 0cea8e5 3818f5a 0cea8e5 ed1dd60 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import gradio as gr
from langchain.vectorstores import FAISS
from langchain.embeddings import HuggingFaceEmbeddings
import zipfile
import os
# Percorsi per il primo file ZIP
zip_path_m = "faiss_manual_index.zip" # File ZIP per l'indice manuale
extraction_dir_m = "./extracted_models/manual_index" # Sottocartella per estrazione manuale
testm_dir = "./extracted_models/manual_index/faiss_manual_index" # Cartella finale
# Percorsi per il secondo file ZIP
zip_path_p = "faiss_problems_index.zip" # File ZIP per l'indice problemi
extraction_dir_p = "./extracted_models/problems_index" # Sottocartella per estrazione problemi
testp_dir = "./extracted_models/problems_index/faiss_problems_index" # Cartella finale
# Estrai il primo file ZIP se non esiste già
if not os.path.exists(testm_dir):
with zipfile.ZipFile(zip_path_m, 'r') as zip_ref:
zip_ref.extractall(extraction_dir_m)
print(f"Indice Manuale estratto correttamente nella cartella {extraction_dir_m}")
else:
print(f"Indice Manuale già presente in {testm_dir}")
# Estrai il secondo file ZIP se non esiste già
if not os.path.exists(testp_dir):
with zipfile.ZipFile(zip_path_p, 'r') as zip_ref:
zip_ref.extractall(extraction_dir_p)
print(f"Indice Problemi estratto correttamente nella cartella {extraction_dir_p}")
else:
print(f"Indice Problemi già presente in {testp_dir}")
# Carica il modello di embedding
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/LaBSE")
# Carica i vectorstore FAISS salvati
vectorstore = FAISS.load_local("faiss_index", embedding_model, allow_dangerous_deserialization=True)
manual_vectorstore = FAISS.load_local("faiss_manual_index", embedding_model, allow_dangerous_deserialization=True)
problems_vectorstore = FAISS.load_local("faiss_problems_index", embedding_model, allow_dangerous_deserialization=True)
def search_query(query):
# Cerca nei manuali
manual_results = manual_vectorstore.similarity_search(query, k=2)
manual_output = "\n\n".join([doc.page_content for doc in manual_results])
# Cerca nei problemi
problems_results = problems_vectorstore.similarity_search(query, k=2)
problems_output = "\n\n".join([doc.page_content for doc in problems_results])
# Restituisce i risultati come output diviso
return manual_output, problems_output
examples = [
["How to change the knife?"],
["What are the safety precautions for using the machine?"],
["How can I get help with the machine?"]
]
# Interfaccia Gradio
iface = gr.Interface(
fn=search_query,
inputs=gr.Textbox(lines=2, placeholder="Enter your question here..."),
outputs=[
gr.Textbox(label="Manual Results"),
gr.Textbox(label="Issues Results")
],
examples=examples,
title="Manual Querying System",
description="Enter a question to get relevant information extracted from the manual and the most common related issues."
)
# Avvia l'app
iface.launch() |