Spaces:
Sleeping
Sleeping
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() |