Mauro24 commited on
Commit
e21292c
·
verified ·
1 Parent(s): 816a5d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -23
app.py CHANGED
@@ -2,11 +2,13 @@
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
 
11
  # Percorsi ZIP per manuali e problemi
12
  zip_path_m = "faiss_manual_index.zip"
@@ -30,20 +32,6 @@ embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/LaBSE"
30
  manual_vectorstore = FAISS.load_local(faiss_manual_index, embedding_model, allow_dangerous_deserialization=True)
31
  problems_vectorstore = FAISS.load_local(faiss_problems_index, embedding_model, allow_dangerous_deserialization=True)
32
 
33
- # Caricamento del modello GPT-J da Hugging Face
34
- model_name = "EleutherAI/gpt-j-6B"
35
-
36
- # Forza l'uso della CPU
37
- model = AutoModelForCausalLM.from_pretrained(
38
- model_name,
39
- torch_dtype=torch.float32, # float32 per la CPU
40
- device_map={"": "cpu"} # Specifica CPU come dispositivo
41
- )
42
-
43
- tokenizer = AutoTokenizer.from_pretrained(model_name)
44
- tokenizer = AutoTokenizer.from_pretrained(model_name)
45
- model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
46
-
47
  # Funzione per la ricerca e il riassunto
48
  def search_and_summarize(query):
49
  # Ricerca nei manuali e problemi
@@ -55,12 +43,17 @@ def search_and_summarize(query):
55
 
56
  combined_text = f"Manual Results:\n{manual_output}\n\nProblems Results:\n{problems_output}"
57
 
58
- # Generazione del riassunto con GPT-J
59
  input_text = f"Riassumi le seguenti informazioni:\n{combined_text}\n\nRiassunto:"
60
- inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
61
- output = model.generate(inputs.input_ids, max_length=300, temperature=0.7)
62
- summary = tokenizer.decode(output[0], skip_special_tokens=True)
63
-
 
 
 
 
 
64
  return manual_output, problems_output, summary
65
 
66
  # Interfaccia Gradio
@@ -70,16 +63,17 @@ iface = gr.Interface(
70
  outputs=[
71
  gr.Textbox(label="Manual Results"),
72
  gr.Textbox(label="Issues Results"),
73
- gr.Textbox(label="Summary by GPT-J")
74
  ],
75
  examples=[
76
  ["How to change the knife?"],
77
  ["What are the safety precautions for using the machine?"],
78
  ["How can I get help with the machine?"]
79
  ],
80
- title="Manual Querying System with GPT-J Summarization",
81
- description="Enter a question to get information from the manual and the common issues, summarized by GPT-J."
82
  )
83
 
84
  # Avvia l'app Gradio
85
  iface.launch()
 
 
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
+ import openai
8
  import torch
9
 
10
+ # Carica la chiave API di OpenAI dalle variabili d'ambiente
11
+ openai.api_key = os.getenv("OPENAI_API_KEY")
12
 
13
  # Percorsi ZIP per manuali e problemi
14
  zip_path_m = "faiss_manual_index.zip"
 
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
 
43
 
44
  combined_text = f"Manual Results:\n{manual_output}\n\nProblems Results:\n{problems_output}"
45
 
46
+ # Generazione del riassunto con OpenAI
47
  input_text = f"Riassumi le seguenti informazioni:\n{combined_text}\n\nRiassunto:"
48
+ response = openai.Completion.create(
49
+ engine="text-davinci-003", # Puoi scegliere un altro modello se preferisci
50
+ prompt=input_text,
51
+ max_tokens=300,
52
+ temperature=0.7
53
+ )
54
+
55
+ summary = response.choices[0].text.strip()
56
+
57
  return manual_output, problems_output, summary
58
 
59
  # Interfaccia Gradio
 
63
  outputs=[
64
  gr.Textbox(label="Manual Results"),
65
  gr.Textbox(label="Issues Results"),
66
+ gr.Textbox(label="Summary by OpenAI")
67
  ],
68
  examples=[
69
  ["How to change the knife?"],
70
  ["What are the safety precautions for using the machine?"],
71
  ["How can I get help with the machine?"]
72
  ],
73
+ title="Manual Querying System with OpenAI Summarization",
74
+ description="Enter a question to get information from the manual and the common issues, summarized by OpenAI."
75
  )
76
 
77
  # Avvia l'app Gradio
78
  iface.launch()
79
+