Spaces:
Running
on
Zero
Running
on
Zero
rollback: can't solve performance issues, rollback to lfc
Browse files
app.py
CHANGED
@@ -8,18 +8,13 @@ from sentence_transformers import SentenceTransformer
|
|
8 |
import gradio as gr
|
9 |
from threading import Thread
|
10 |
|
11 |
-
|
12 |
-
print("Loading FAISS index...")
|
13 |
-
cpu_index = faiss.read_index("vector_db/index.faiss")
|
14 |
-
gpu_index = None # GPU Index wird später erstellt
|
15 |
-
|
16 |
with open("vector_db/chunks.pkl", "rb") as f:
|
17 |
chunks = pickle.load(f)
|
18 |
with open("vector_db/metadata.pkl", "rb") as f:
|
19 |
metadata_dict = pickle.load(f)
|
20 |
|
21 |
ST = SentenceTransformer("BAAI/bge-large-en-v1.5")
|
22 |
-
|
23 |
github_base_url = "https://github.com/arsiba/EDPB-AI/blob/main/"
|
24 |
|
25 |
model_id = "HuggingFaceH4/zephyr-7b-beta"
|
@@ -37,7 +32,7 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
37 |
device_map={"": 0},
|
38 |
torch_dtype=torch.bfloat16,
|
39 |
trust_remote_code=True
|
40 |
-
)
|
41 |
|
42 |
SYS = (
|
43 |
"You are a legal AI assistant specialized in GDPR/EDPB."
|
@@ -47,21 +42,8 @@ SYS = (
|
|
47 |
|
48 |
@spaces.GPU()
|
49 |
def retrieve(q, k=3):
|
50 |
-
global gpu_index, cpu_index
|
51 |
-
|
52 |
-
# GPU Index beim ersten Aufruf erstellen
|
53 |
-
if gpu_index is None:
|
54 |
-
try:
|
55 |
-
print("Creating GPU index...")
|
56 |
-
res = faiss.StandardGpuResources()
|
57 |
-
gpu_index = faiss.index_cpu_to_gpu(res, 0, cpu_index)
|
58 |
-
print("FAISS index successfully moved to GPU")
|
59 |
-
except Exception as e:
|
60 |
-
print(f"Failed to move FAISS to GPU: {e}")
|
61 |
-
gpu_index = cpu_index # Fallback to CPU
|
62 |
-
|
63 |
emb = ST.encode(q)
|
64 |
-
D, I =
|
65 |
docs, file_sources = [], []
|
66 |
for i in I[0]:
|
67 |
chunk = chunks[i]
|
@@ -70,6 +52,7 @@ def retrieve(q, k=3):
|
|
70 |
file_sources.append(meta)
|
71 |
return docs, file_sources
|
72 |
|
|
|
73 |
def make_prompt(q, docs):
|
74 |
context = "\n\n".join(f"Title: {d['title']}\nPages: {d['pages']}" for d in docs)
|
75 |
prompt = f"detailed thinking off\n"
|
@@ -84,6 +67,7 @@ def build_markdown_links(file_input):
|
|
84 |
lines.append(line)
|
85 |
return "\n\n".join(lines)
|
86 |
|
|
|
87 |
def build_markdown_chunks(docs):
|
88 |
lines = []
|
89 |
for idx, d in enumerate(docs, start=1):
|
@@ -118,7 +102,6 @@ def qa_fn(faiss_search, question, top_k, temperature, max_tokens):
|
|
118 |
output = output.split("Output:", 1)[1].strip()
|
119 |
return "\n# Generated Answer\n", output,"\n# Used Documents\n", file_links, "\n# Used Context\n", markdown_chunks
|
120 |
|
121 |
-
# Gradio Interface
|
122 |
heading_answer = gr.Markdown(label="Answer Heading")
|
123 |
outputs_answer = gr.Textbox(label="Answer")
|
124 |
heading_links = gr.Markdown(label="Links Heading")
|
@@ -129,7 +112,7 @@ outputs_chunks = gr.Markdown(label="Used Chunks")
|
|
129 |
demo = gr.Interface(
|
130 |
fn=qa_fn,
|
131 |
inputs=[
|
132 |
-
gr.Textbox(lines=4, label="What Documents are you looking for?", placeholder="Please change to get propper results:\nDocuments covering the EDPB
|
133 |
gr.Textbox(lines=1, label="What is your question?", placeholder="Please change to get propper results:\nWhat does the EDPB recommend regarding automated decision-making and profiling under the GDPR, and what rights do individuals have in relation to such decisions?"),
|
134 |
],
|
135 |
additional_inputs=[
|
|
|
8 |
import gradio as gr
|
9 |
from threading import Thread
|
10 |
|
11 |
+
index = faiss.read_index("vector_db/index.faiss")
|
|
|
|
|
|
|
|
|
12 |
with open("vector_db/chunks.pkl", "rb") as f:
|
13 |
chunks = pickle.load(f)
|
14 |
with open("vector_db/metadata.pkl", "rb") as f:
|
15 |
metadata_dict = pickle.load(f)
|
16 |
|
17 |
ST = SentenceTransformer("BAAI/bge-large-en-v1.5")
|
|
|
18 |
github_base_url = "https://github.com/arsiba/EDPB-AI/blob/main/"
|
19 |
|
20 |
model_id = "HuggingFaceH4/zephyr-7b-beta"
|
|
|
32 |
device_map={"": 0},
|
33 |
torch_dtype=torch.bfloat16,
|
34 |
trust_remote_code=True
|
35 |
+
)
|
36 |
|
37 |
SYS = (
|
38 |
"You are a legal AI assistant specialized in GDPR/EDPB."
|
|
|
42 |
|
43 |
@spaces.GPU()
|
44 |
def retrieve(q, k=3):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
emb = ST.encode(q)
|
46 |
+
D, I = index.search(np.array([emb], dtype="float32"), k)
|
47 |
docs, file_sources = [], []
|
48 |
for i in I[0]:
|
49 |
chunk = chunks[i]
|
|
|
52 |
file_sources.append(meta)
|
53 |
return docs, file_sources
|
54 |
|
55 |
+
|
56 |
def make_prompt(q, docs):
|
57 |
context = "\n\n".join(f"Title: {d['title']}\nPages: {d['pages']}" for d in docs)
|
58 |
prompt = f"detailed thinking off\n"
|
|
|
67 |
lines.append(line)
|
68 |
return "\n\n".join(lines)
|
69 |
|
70 |
+
|
71 |
def build_markdown_chunks(docs):
|
72 |
lines = []
|
73 |
for idx, d in enumerate(docs, start=1):
|
|
|
102 |
output = output.split("Output:", 1)[1].strip()
|
103 |
return "\n# Generated Answer\n", output,"\n# Used Documents\n", file_links, "\n# Used Context\n", markdown_chunks
|
104 |
|
|
|
105 |
heading_answer = gr.Markdown(label="Answer Heading")
|
106 |
outputs_answer = gr.Textbox(label="Answer")
|
107 |
heading_links = gr.Markdown(label="Links Heading")
|
|
|
112 |
demo = gr.Interface(
|
113 |
fn=qa_fn,
|
114 |
inputs=[
|
115 |
+
gr.Textbox(lines=4, label="What Documents are you looking for?", placeholder="Please change to get propper results:\nDocuments covering the EDPB’s stance on automated decision-making, particularly profiling, under the GDPR. Guidelines on how organizations should inform data subjects about automated decisions and the rights of individuals to object to such decisions."),
|
116 |
gr.Textbox(lines=1, label="What is your question?", placeholder="Please change to get propper results:\nWhat does the EDPB recommend regarding automated decision-making and profiling under the GDPR, and what rights do individuals have in relation to such decisions?"),
|
117 |
],
|
118 |
additional_inputs=[
|