Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,90 +2,74 @@
|
|
2 |
import gradio as gr
|
3 |
from langchain_community.vectorstores import FAISS
|
4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
|
5 |
import zipfile
|
6 |
import os
|
|
|
7 |
|
8 |
-
# Percorsi per
|
9 |
-
zip_path_m = "faiss_manual_index.zip"
|
10 |
-
faiss_manual_index = "faiss_manual_index"
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
os.makedirs(faiss_manual_index) # Crea la directory
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
#
|
21 |
-
if not os.path.exists(faiss_problems_index):
|
22 |
-
os.makedirs(faiss_problems_index) # Crea la directory
|
23 |
-
|
24 |
-
# Controlla i file presenti nella cartella faiss_manual_index prima dell'estrazione
|
25 |
-
print(f"Contenuto della directory {faiss_manual_index} prima dell'estrazione:", os.listdir(faiss_manual_index))
|
26 |
-
|
27 |
-
# Estrai il primo file ZIP se non esiste già
|
28 |
-
if os.path.exists(zip_path_m): # Controlla che il file zip esista
|
29 |
-
with zipfile.ZipFile(zip_path_m, 'r') as zip_ref:
|
30 |
-
zip_ref.extractall(faiss_manual_index)
|
31 |
-
print(f"Files estratti nella directory: {faiss_manual_index}")
|
32 |
-
else:
|
33 |
-
print(f"File {zip_path_m} non trovato. Assicurati di caricarlo nello Space.")
|
34 |
-
|
35 |
-
# Verifica di nuovo il contenuto della cartella faiss_manual_index dopo l'estrazione
|
36 |
-
print(f"Contenuto della directory {faiss_manual_index} dopo l'estrazione:", os.listdir(faiss_manual_index))
|
37 |
-
|
38 |
-
# Estrai il secondo file ZIP se non esiste già
|
39 |
-
if os.path.exists(zip_path_p): # Controlla che il file zip esista
|
40 |
-
with zipfile.ZipFile(zip_path_p, 'r') as zip_ref:
|
41 |
-
zip_ref.extractall(faiss_problems_index)
|
42 |
-
print(f"Files estratti nella directory: {faiss_problems_index}")
|
43 |
-
else:
|
44 |
-
print(f"File {zip_path_p} non trovato. Assicurati di caricarlo nello Space.")
|
45 |
-
|
46 |
-
# Verifica di nuovo il contenuto della cartella faiss_problems_index dopo l'estrazione
|
47 |
-
print(f"Contenuto della directory {faiss_problems_index} dopo l'estrazione:", os.listdir(faiss_problems_index))
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
# Carica il modello di embedding
|
53 |
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/LaBSE")
|
54 |
|
55 |
-
#
|
56 |
manual_vectorstore = FAISS.load_local(faiss_manual_index, embedding_model, allow_dangerous_deserialization=True)
|
57 |
problems_vectorstore = FAISS.load_local(faiss_problems_index, embedding_model, allow_dangerous_deserialization=True)
|
58 |
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
manual_results = manual_vectorstore.similarity_search(query, k=2)
|
62 |
manual_output = "\n\n".join([doc.page_content for doc in manual_results])
|
63 |
|
64 |
-
# Cerca nei problemi
|
65 |
problems_results = problems_vectorstore.similarity_search(query, k=2)
|
66 |
problems_output = "\n\n".join([doc.page_content for doc in problems_results])
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
76 |
|
77 |
# Interfaccia Gradio
|
78 |
iface = gr.Interface(
|
79 |
-
fn=
|
80 |
-
|
81 |
outputs=[
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
88 |
)
|
89 |
|
90 |
-
# Avvia l'app
|
91 |
-
iface.launch()
|
|
|
2 |
import gradio as gr
|
3 |
from langchain_community.vectorstores import FAISS
|
4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
6 |
import zipfile
|
7 |
import os
|
8 |
+
import torch
|
9 |
|
10 |
+
# Percorsi ZIP per manuali e problemi
|
11 |
+
zip_path_m = "faiss_manual_index.zip"
|
12 |
+
faiss_manual_index = "faiss_manual_index"
|
13 |
|
14 |
+
zip_path_p = "faiss_problems_index.zip"
|
15 |
+
faiss_problems_index = "faiss_problems_index"
|
|
|
16 |
|
17 |
+
# Estrazione dei file ZIP se necessario
|
18 |
+
for zip_path, output_dir in [(zip_path_m, faiss_manual_index), (zip_path_p, faiss_problems_index)]:
|
19 |
+
if not os.path.exists(output_dir):
|
20 |
+
os.makedirs(output_dir)
|
21 |
+
if os.path.exists(zip_path):
|
22 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
23 |
+
zip_ref.extractall(output_dir)
|
24 |
|
25 |
+
# Caricamento del modello di embedding
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/LaBSE")
|
27 |
|
28 |
+
# Caricamento dei vectorstore FAISS
|
29 |
manual_vectorstore = FAISS.load_local(faiss_manual_index, embedding_model, allow_dangerous_deserialization=True)
|
30 |
problems_vectorstore = FAISS.load_local(faiss_problems_index, embedding_model, allow_dangerous_deserialization=True)
|
31 |
|
32 |
+
# Caricamento del modello GPT-J da Hugging Face
|
33 |
+
model_name = "EleutherAI/gpt-j-6B"
|
34 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
35 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
|
36 |
+
|
37 |
+
# Funzione per la ricerca e il riassunto
|
38 |
+
def search_and_summarize(query):
|
39 |
+
# Ricerca nei manuali e problemi
|
40 |
manual_results = manual_vectorstore.similarity_search(query, k=2)
|
41 |
manual_output = "\n\n".join([doc.page_content for doc in manual_results])
|
42 |
|
|
|
43 |
problems_results = problems_vectorstore.similarity_search(query, k=2)
|
44 |
problems_output = "\n\n".join([doc.page_content for doc in problems_results])
|
45 |
|
46 |
+
combined_text = f"Manual Results:\n{manual_output}\n\nProblems Results:\n{problems_output}"
|
47 |
+
|
48 |
+
# Generazione del riassunto con GPT-J
|
49 |
+
input_text = f"Riassumi le seguenti informazioni:\n{combined_text}\n\nRiassunto:"
|
50 |
+
inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
|
51 |
+
output = model.generate(inputs.input_ids, max_length=300, temperature=0.7)
|
52 |
+
summary = tokenizer.decode(output[0], skip_special_tokens=True)
|
53 |
+
|
54 |
+
return manual_output, problems_output, summary
|
55 |
|
56 |
# Interfaccia Gradio
|
57 |
iface = gr.Interface(
|
58 |
+
fn=search_and_summarize,
|
59 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your question here..."),
|
60 |
outputs=[
|
61 |
+
gr.Textbox(label="Manual Results"),
|
62 |
+
gr.Textbox(label="Issues Results"),
|
63 |
+
gr.Textbox(label="Summary by GPT-J")
|
64 |
+
],
|
65 |
+
examples=[
|
66 |
+
["How to change the knife?"],
|
67 |
+
["What are the safety precautions for using the machine?"],
|
68 |
+
["How can I get help with the machine?"]
|
69 |
+
],
|
70 |
+
title="Manual Querying System with GPT-J Summarization",
|
71 |
+
description="Enter a question to get information from the manual and the common issues, summarized by GPT-J."
|
72 |
)
|
73 |
|
74 |
+
# Avvia l'app Gradio
|
75 |
+
iface.launch()
|