Spaces:
Sleeping
Sleeping
tomas.helmfridsson
commited on
Commit
·
7894f40
1
Parent(s):
d7c8195
update guis 10
Browse files
app.py
CHANGED
@@ -8,72 +8,71 @@ from langchain_huggingface.llms import HuggingFacePipeline
|
|
8 |
from langchain.chains import RetrievalQA
|
9 |
from transformers import pipeline
|
10 |
|
11 |
-
# 1) Ladda och indexera
|
12 |
def load_vectorstore():
|
13 |
-
all_docs,
|
14 |
for fn in os.listdir("document"):
|
15 |
if fn.lower().endswith(".pdf"):
|
16 |
path = os.path.join("document", fn)
|
17 |
-
|
18 |
-
docs = loader.load_and_split()
|
19 |
all_docs.extend(docs)
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
return
|
24 |
|
25 |
-
# 2)
|
26 |
with gr.Blocks() as demo:
|
27 |
-
# A) Status
|
28 |
status = gr.Markdown("🔄 Laddar dokument och modell, vänta…", elem_id="status-text")
|
29 |
-
|
30 |
-
# B)
|
31 |
-
vectorstore,
|
32 |
llm_pipe = pipeline("text-generation", model="tiiuae/falcon-rw-1b", device=-1)
|
33 |
llm = HuggingFacePipeline(
|
34 |
pipeline=llm_pipe,
|
35 |
model_kwargs={"temperature": 0.3, "max_new_tokens": 512}
|
36 |
)
|
37 |
-
|
38 |
|
39 |
-
# C) Dölj status
|
40 |
status.visible = False
|
41 |
-
file_list_md = "\n".join(f"- {f}" for f in files)
|
42 |
gr.Markdown(
|
43 |
-
|
|
|
44 |
elem_id="status-text"
|
45 |
)
|
46 |
|
47 |
-
# D)
|
48 |
-
|
49 |
-
|
50 |
-
label="Temperatur
|
51 |
-
|
52 |
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
55 |
if len(message) > 1000:
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
60 |
try:
|
61 |
-
|
62 |
-
assistant_msg = resp["result"]
|
63 |
except Exception as e:
|
64 |
-
|
65 |
-
#
|
66 |
history = history or []
|
67 |
-
history.append(
|
68 |
-
history
|
69 |
-
return history, history
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
description="Hej! Ställ en fråga baserat på dina PDF-dokument.",
|
76 |
-
chatbot=gr.Chatbot(type="messages")
|
77 |
-
)
|
78 |
|
79 |
demo.launch()
|
|
|
8 |
from langchain.chains import RetrievalQA
|
9 |
from transformers import pipeline
|
10 |
|
11 |
+
# 1) Ladda och indexera PDF:er
|
12 |
def load_vectorstore():
|
13 |
+
all_docs, files = [], []
|
14 |
for fn in os.listdir("document"):
|
15 |
if fn.lower().endswith(".pdf"):
|
16 |
path = os.path.join("document", fn)
|
17 |
+
docs = PyPDFLoader(path).load_and_split()
|
|
|
18 |
all_docs.extend(docs)
|
19 |
+
files.append(fn)
|
20 |
+
emb = HuggingFaceEmbeddings(model_name="KBLab/sentence-bert-swedish-cased")
|
21 |
+
vs = FAISS.from_documents(all_docs, emb)
|
22 |
+
return vs, files
|
23 |
|
24 |
+
# 2) Skapa Blocks-layout
|
25 |
with gr.Blocks() as demo:
|
26 |
+
# A) Status under uppstart
|
27 |
status = gr.Markdown("🔄 Laddar dokument och modell, vänta…", elem_id="status-text")
|
28 |
+
|
29 |
+
# B) Indexera och initiera
|
30 |
+
vectorstore, loaded_files = load_vectorstore()
|
31 |
llm_pipe = pipeline("text-generation", model="tiiuae/falcon-rw-1b", device=-1)
|
32 |
llm = HuggingFacePipeline(
|
33 |
pipeline=llm_pipe,
|
34 |
model_kwargs={"temperature": 0.3, "max_new_tokens": 512}
|
35 |
)
|
36 |
+
qa = RetrievalQA.from_chain_type(llm=llm, retriever=vectorstore.as_retriever())
|
37 |
|
38 |
+
# C) Dölj status och visa PDF-lista
|
39 |
status.visible = False
|
|
|
40 |
gr.Markdown(
|
41 |
+
"✅ Klar! Du kan nu ställa frågor om dokumenten nedan:\n\n" +
|
42 |
+
"\n".join(f"- {f}" for f in loaded_files),
|
43 |
elem_id="status-text"
|
44 |
)
|
45 |
|
46 |
+
# D) Temperatur-slider och text-inmatning + knapp
|
47 |
+
with gr.Row():
|
48 |
+
txt = gr.Textbox(label="Din fråga:")
|
49 |
+
temp = gr.Slider(0, 1, value=0.3, step=0.05, label="Temperatur")
|
50 |
+
send = gr.Button("Skicka")
|
51 |
|
52 |
+
# E) Chatbot-komponent (OpenAI-stil)
|
53 |
+
chatbot = gr.Chatbot([], type="messages")
|
54 |
+
|
55 |
+
# F) Logik för chatten
|
56 |
+
def chat_fn(message, temperature, history):
|
57 |
+
# kortare skydd
|
58 |
if len(message) > 1000:
|
59 |
+
history = history or []
|
60 |
+
history.append(("⚠️ Din fråga är för lång, korta ner den.", ""))
|
61 |
+
return history
|
62 |
+
# uppdatera temp
|
63 |
+
llm.model_kwargs["temperature"] = temperature
|
64 |
try:
|
65 |
+
out = qa.invoke({"query": message})["result"]
|
|
|
66 |
except Exception as e:
|
67 |
+
out = f"Ett fel uppstod: {e}"
|
68 |
+
# bygg historia som tupler
|
69 |
history = history or []
|
70 |
+
history.append((message, out))
|
71 |
+
return history
|
|
|
72 |
|
73 |
+
# G) Koppla knapp till chat_fn
|
74 |
+
send.click(fn=chat_fn,
|
75 |
+
inputs=[txt, temp, chatbot],
|
76 |
+
outputs=chatbot)
|
|
|
|
|
|
|
77 |
|
78 |
demo.launch()
|