|
|
|
import gradio as gr |
|
from langchain.vectorstores import FAISS |
|
from langchain.embeddings import HuggingFaceEmbeddings |
|
import zipfile |
|
import os |
|
|
|
|
|
zip_path_m = "faiss_manual_index.zip" |
|
extraction_dir_m = "./extracted_models/manual_index" |
|
testm_dir = "./extracted_models/manual_index/faiss_manual_index" |
|
|
|
|
|
zip_path_p = "faiss_problems_index.zip" |
|
extraction_dir_p = "./extracted_models/problems_index" |
|
testp_dir = "./extracted_models/problems_index/faiss_problems_index" |
|
|
|
|
|
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}") |
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
|
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/LaBSE") |
|
|
|
|
|
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): |
|
|
|
manual_results = manual_vectorstore.similarity_search(query, k=2) |
|
manual_output = "\n\n".join([doc.page_content for doc in manual_results]) |
|
|
|
|
|
problems_results = problems_vectorstore.similarity_search(query, k=2) |
|
problems_output = "\n\n".join([doc.page_content for doc in problems_results]) |
|
|
|
|
|
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?"] |
|
] |
|
|
|
|
|
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." |
|
) |
|
|
|
|
|
iface.launch() |