Update app.py
Browse files
app.py
CHANGED
@@ -1,85 +1,20 @@
|
|
1 |
|
2 |
-
import openai
|
3 |
-
import gradio as gr
|
4 |
-
from langchain_community.vectorstores import FAISS
|
5 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
6 |
-
import zipfile
|
7 |
import os
|
8 |
-
import
|
9 |
|
10 |
-
#
|
11 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
faiss_manual_index = "faiss_manual_index"
|
16 |
-
|
17 |
-
zip_path_p = "faiss_problems_index.zip"
|
18 |
-
faiss_problems_index = "faiss_problems_index"
|
19 |
-
|
20 |
-
# Estrazione dei file ZIP se necessario
|
21 |
-
for zip_path, output_dir in [(zip_path_m, faiss_manual_index), (zip_path_p, faiss_problems_index)]:
|
22 |
-
if not os.path.exists(output_dir):
|
23 |
-
os.makedirs(output_dir)
|
24 |
-
if os.path.exists(zip_path):
|
25 |
-
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
26 |
-
zip_ref.extractall(output_dir)
|
27 |
-
|
28 |
-
# Caricamento del modello di embedding
|
29 |
-
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/LaBSE")
|
30 |
-
|
31 |
-
# Caricamento dei vectorstore FAISS
|
32 |
-
manual_vectorstore = FAISS.load_local(faiss_manual_index, embedding_model, allow_dangerous_deserialization=True)
|
33 |
-
problems_vectorstore = FAISS.load_local(faiss_problems_index, embedding_model, allow_dangerous_deserialization=True)
|
34 |
-
|
35 |
-
# Funzione per la ricerca e il riassunto
|
36 |
-
def search_and_summarize(query):
|
37 |
-
# Ricerca nei manuali e problemi
|
38 |
-
manual_results = manual_vectorstore.similarity_search(query, k=2)
|
39 |
-
manual_output = "\n\n".join([doc.page_content for doc in manual_results])
|
40 |
-
|
41 |
-
problems_results = problems_vectorstore.similarity_search(query, k=2)
|
42 |
-
problems_output = "\n\n".join([doc.page_content for doc in problems_results])
|
43 |
-
|
44 |
-
combined_text = f"Manual Results:\n{manual_output}\n\nProblems Results:\n{problems_output}"
|
45 |
-
|
46 |
-
# Generazione del riassunto con OpenAI GPT-3 (nuova API)
|
47 |
-
input_text = f"Riassumi le seguenti informazioni:\n{combined_text}\n\nRiassunto:"
|
48 |
-
|
49 |
-
# Chiamata all'API OpenAI per generare il riassunto usando la nuova interfaccia
|
50 |
response = openai.ChatCompletion.create(
|
51 |
-
model="gpt-3.5-turbo",
|
52 |
-
messages=[
|
53 |
-
{"role": "system", "content": "Sei un assistente che aiuta con la sintesi di informazioni."},
|
54 |
-
{"role": "user", "content": input_text}
|
55 |
-
],
|
56 |
-
max_tokens=150,
|
57 |
-
temperature=0.7
|
58 |
)
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
# Interfaccia Gradio
|
65 |
-
iface = gr.Interface(
|
66 |
-
fn=search_and_summarize,
|
67 |
-
inputs=gr.Textbox(lines=2, placeholder="Enter your question here..."),
|
68 |
-
outputs=[
|
69 |
-
gr.Textbox(label="Manual Results"),
|
70 |
-
gr.Textbox(label="Issues Results"),
|
71 |
-
gr.Textbox(label="Summary by GPT-3")
|
72 |
-
],
|
73 |
-
examples=[
|
74 |
-
["How to change the knife?"],
|
75 |
-
["What are the safety precautions for using the machine?"],
|
76 |
-
["How can I get help with the machine?"]
|
77 |
-
],
|
78 |
-
title="Manual Querying System with GPT-3 Summarization",
|
79 |
-
description="Enter a question to get information from the manual and the common issues, summarized by GPT-3."
|
80 |
-
)
|
81 |
|
82 |
-
# Avvia l'app Gradio
|
83 |
-
iface.launch()
|
84 |
|
85 |
|
|
|
1 |
|
|
|
|
|
|
|
|
|
|
|
2 |
import os
|
3 |
+
import openai
|
4 |
|
5 |
+
# Recupera la chiave API dai secrets
|
6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
|
8 |
+
# Verifica il collegamento con OpenAI
|
9 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
response = openai.ChatCompletion.create(
|
11 |
+
model="gpt-3.5-turbo",
|
12 |
+
messages=[{"role": "user", "content": "Test di connessione con OpenAI."}]
|
|
|
|
|
|
|
|
|
|
|
13 |
)
|
14 |
+
print("Collegamento riuscito! Risposta di OpenAI:")
|
15 |
+
print(response['choices'][0]['message']['content'])
|
16 |
+
except Exception as e:
|
17 |
+
print("Errore nel collegamento con OpenAI:", e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
|
|
|
|
19 |
|
20 |
|