tomas.helmfridsson commited on
Commit
30881d9
Β·
1 Parent(s): ad7b39c
Files changed (1) hide show
  1. app.py +75 -37
app.py CHANGED
@@ -1,6 +1,9 @@
1
  import os
 
 
 
2
  import gradio as gr
3
- from transformers import pipeline
4
  from langchain_community.document_loaders import PyPDFLoader
5
  from langchain_community.vectorstores import FAISS
6
  from langchain_huggingface.embeddings import HuggingFaceEmbeddings
@@ -8,93 +11,128 @@ from langchain_huggingface.llms import HuggingFacePipeline
8
  from langchain.chains import RetrievalQA
9
  from langchain.text_splitter import RecursiveCharacterTextSplitter
10
 
11
- # ── 1) Ladda PDF:er och dela upp i korta chunkar ────────────
 
 
 
 
 
 
 
 
12
  all_docs, files = [], []
13
  splitter = RecursiveCharacterTextSplitter(chunk_size=250, chunk_overlap=30)
14
 
15
  for fn in os.listdir("document"):
16
  if fn.lower().endswith(".pdf"):
17
- path = os.path.join("document", fn)
18
  loader = PyPDFLoader(path)
19
- pages = loader.load() # en lista av Document-objekt
20
- chunks = splitter.split_documents(pages) # dela upp i mindre bitar
21
  all_docs.extend(chunks)
22
  files.append(fn)
23
 
24
- # ── 2) Skapa vektorer med svenska embeddings ────────────────
 
 
25
  emb = HuggingFaceEmbeddings(model_name="KBLab/sentence-bert-swedish-cased")
26
  vs = FAISS.from_documents(all_docs, emb)
 
27
 
28
- # ── 3) Initiera LLM-pipeline (CPU-only) ───────────────────────
29
  pipe = pipeline(
30
  "text-generation",
31
  model="tiiuae/falcon-rw-1b",
32
- device=-1, # CPU
33
- max_new_tokens=64 # kortare svar β†’ snabbare
34
  )
35
  llm = HuggingFacePipeline(
36
  pipeline=pipe,
37
  model_kwargs={"temperature": 0.3}
38
  )
 
39
 
40
- # ── 4) Bygg RetrievalQA med bara 1 chunk ────────────────────
41
  retriever = vs.as_retriever(search_kwargs={"k": 1})
42
  qa = RetrievalQA.from_chain_type(
43
  llm=llm,
44
  retriever=retriever,
45
  chain_type="stuff"
46
  )
 
 
 
 
 
 
47
 
48
- # ── 5) Chat-funktion som returnerar bΓ₯de history & state ─────
49
  def chat_fn(message, temperature, history):
 
50
  history = history or []
 
 
 
51
  if not message.strip():
52
- history.append({"role": "assistant", "content": "⚠️ Du mΓ₯ste skriva en frΓ₯ga."})
 
53
  return history, history
54
 
55
  # LΓ€gg till anvΓ€ndarens frΓ₯ga
56
- history.append({"role": "user", "content": message})
57
 
58
- # FΓΆr lΓ₯nga frΓ₯gor
59
  if len(message) > 1000:
60
- history.append({
61
- "role": "assistant",
62
- "content": f"⚠️ FrΓ₯gan Γ€r fΓΆr lΓ₯ng ({len(message)} tecken)."
63
- })
64
  return history, history
65
 
66
  # Justera temperatur
67
  llm.model_kwargs["temperature"] = temperature
68
 
69
- # KΓΆr RAG-kedjan
 
 
 
 
 
 
70
  try:
71
- svar = qa.invoke({"query": message})["result"]
 
72
  except Exception as e:
73
- svar = f"❌ Ett fel uppstod: {e}"
 
 
 
 
74
 
75
- history.append({"role": "assistant", "content": svar})
76
  return history, history
77
 
78
- # ── 6) Bygg Gradio-UI & publicera ─────────────────────────────
79
  with gr.Blocks() as demo:
80
- gr.Markdown("## 🌟 Dokumentassistent (Svenska)")
81
  gr.Markdown("**βœ… Laddade PDF-filer:**\n\n" + "\n".join(f"- {f}" for f in files))
82
 
83
  with gr.Row():
84
- txt = gr.Textbox(
85
- lines=2,
86
- label="Din frΓ₯ga:",
87
- placeholder="Exempel: Vad anges fΓΆrberedelser infΓΆr mΓΆte?"
88
- )
89
- temp = gr.Slider(
90
- 0.0, 1.0, value=0.3, step=0.05,
91
- label="Temperatur"
92
- )
93
- send = gr.Button("Skicka")
94
-
95
- chatbot = gr.Chatbot(value=[], type="messages")
96
  chat_state = gr.State([])
97
 
 
 
 
 
98
  send.click(
99
  fn=chat_fn,
100
  inputs=[txt, temp, chat_state],
@@ -102,5 +140,5 @@ with gr.Blocks() as demo:
102
  )
103
 
104
  if __name__ == "__main__":
105
- # share=True ger en publik lΓ€nk till ditt Space
106
  demo.launch(share=True)
 
 
1
  import os
2
+ import time
3
+ import logging
4
+
5
  import gradio as gr
6
+ from transformers import pipeline, logging as hf_logging
7
  from langchain_community.document_loaders import PyPDFLoader
8
  from langchain_community.vectorstores import FAISS
9
  from langchain_huggingface.embeddings import HuggingFaceEmbeddings
 
11
  from langchain.chains import RetrievalQA
12
  from langchain.text_splitter import RecursiveCharacterTextSplitter
13
 
14
+ # ── 1) Setup logging ───────────────────────────────────────────
15
+ logging.basicConfig(
16
+ format="%(asctime)s %(levelname)s %(message)s",
17
+ level=logging.INFO
18
+ )
19
+ # GΓΆr Transformers mer detaljerade i loggarna
20
+ hf_logging.set_verbosity_debug()
21
+
22
+ # ── 2) Ladda & chunka PDF:er ────────────────────────────────────
23
  all_docs, files = [], []
24
  splitter = RecursiveCharacterTextSplitter(chunk_size=250, chunk_overlap=30)
25
 
26
  for fn in os.listdir("document"):
27
  if fn.lower().endswith(".pdf"):
28
+ path = os.path.join("document", fn)
29
  loader = PyPDFLoader(path)
30
+ pages = loader.load()
31
+ chunks = splitter.split_documents(pages)
32
  all_docs.extend(chunks)
33
  files.append(fn)
34
 
35
+ logging.info(f"βœ… Laddade och chunkade {len(files)} PDF:er β†’ totalt {len(all_docs)} chunkar")
36
+
37
+ # ── 3) Skapa embedding + FAISS───────────────────────────────────
38
  emb = HuggingFaceEmbeddings(model_name="KBLab/sentence-bert-swedish-cased")
39
  vs = FAISS.from_documents(all_docs, emb)
40
+ logging.info("βœ… FAISS-vektorstore skapat")
41
 
42
+ # ── 4) Initiera CPU-pipeline fΓΆr Falcon-1B───────────────────────
43
  pipe = pipeline(
44
  "text-generation",
45
  model="tiiuae/falcon-rw-1b",
46
+ device=-1,
47
+ max_new_tokens=64
48
  )
49
  llm = HuggingFacePipeline(
50
  pipeline=pipe,
51
  model_kwargs={"temperature": 0.3}
52
  )
53
+ logging.info("βœ… LLM-pipeline initierad (CPU, max_new_tokens=64)")
54
 
55
+ # ── 5) Bygg RetrievalQA med bara 1 chunk───────────────────────
56
  retriever = vs.as_retriever(search_kwargs={"k": 1})
57
  qa = RetrievalQA.from_chain_type(
58
  llm=llm,
59
  retriever=retriever,
60
  chain_type="stuff"
61
  )
62
+ logging.info("βœ… RetrievalQA-kedja skapad (k=1)")
63
+
64
+ # ── 6) Ping-funktion fΓΆr sanity-check────────────────────────────
65
+ def ping():
66
+ logging.info("πŸ“ Ping mottagen")
67
+ return "pong"
68
 
69
+ # ── 7) Chat-funktion som returnerar list-of-tuples────────────────
70
  def chat_fn(message, temperature, history):
71
+ start_time = time.time()
72
  history = history or []
73
+ logging.info(f"β†’ chat_fn start – message={message!r}, temp={temperature}")
74
+
75
+ # Tom frΓ₯ga?
76
  if not message.strip():
77
+ history.append(("assistant", "⚠️ Du mΓ₯ste skriva en frΓ₯ga."))
78
+ logging.info("← chat_fn exit – tom frΓ₯ga")
79
  return history, history
80
 
81
  # LΓ€gg till anvΓ€ndarens frΓ₯ga
82
+ history.append(("user", message))
83
 
84
+ # FΓΆr lΓ₯ng frΓ₯ga?
85
  if len(message) > 1000:
86
+ warn = f"⚠️ FrΓ₯gan Γ€r fΓΆr lΓ₯ng ({len(message)} tecken)."
87
+ history.append(("assistant", warn))
88
+ logging.info("← chat_fn exit – fΓΆr lΓ₯ng frΓ₯ga")
 
89
  return history, history
90
 
91
  # Justera temperatur
92
  llm.model_kwargs["temperature"] = temperature
93
 
94
+ # Retrieval
95
+ t0 = time.time()
96
+ docs = retriever.get_relevant_documents(message)
97
+ logging.info(f" πŸ” Retrieval tog {time.time() - t0:.2f}s, hittade {len(docs)} docs")
98
+
99
+ # Inference
100
+ t1 = time.time()
101
  try:
102
+ result = qa.invoke({"query": message})["result"]
103
+ logging.info(f" πŸ€– QA.invoke tog {time.time() - t1:.2f}s")
104
  except Exception as e:
105
+ logging.exception("❌ Fel i QA.invoke")
106
+ result = f"Fel vid QA: {e}"
107
+
108
+ # LΓ€gg till svaret
109
+ history.append(("assistant", result))
110
 
111
+ logging.info(f"← chat_fn klar pΓ₯ {time.time() - start_time:.2f}s med {len(history)} meddelanden")
112
  return history, history
113
 
114
+ # ── 8) Bygg Gradio-UI & lansera publicerat────────────────────────
115
  with gr.Blocks() as demo:
116
+ gr.Markdown("# 🌟 Dokumentassistent (Svenska)")
117
  gr.Markdown("**βœ… Laddade PDF-filer:**\n\n" + "\n".join(f"- {f}" for f in files))
118
 
119
  with gr.Row():
120
+ # Ping-test
121
+ ping_btn = gr.Button("πŸ“ Ping")
122
+ ping_out = gr.Textbox(label="Ping-svar")
123
+
124
+ # RAG-chat
125
+ txt = gr.Textbox(lines=2, label="Din frΓ₯ga:", placeholder="Ex: Vad anges fΓΆr krav?")
126
+ temp = gr.Slider(0.0, 1.0, value=0.3, step=0.05, label="Temperatur")
127
+ send = gr.Button("πŸ“¨ Skicka")
128
+
129
+ chatbot = gr.Chatbot() # default == tuple-format
 
 
130
  chat_state = gr.State([])
131
 
132
+ # Koppla Ping
133
+ ping_btn.click(fn=ping, inputs=[], outputs=[ping_out])
134
+
135
+ # Koppla chat
136
  send.click(
137
  fn=chat_fn,
138
  inputs=[txt, temp, chat_state],
 
140
  )
141
 
142
  if __name__ == "__main__":
 
143
  demo.launch(share=True)
144
+