Spaces:
Runtime error
Runtime error
tomas.helmfridsson
commited on
Commit
Β·
39f5e31
1
Parent(s):
1f80d02
better tracing
Browse files
app.py
CHANGED
|
@@ -25,6 +25,7 @@ CTX_TOK_MAX = 750 # sparar marginal till frΓ₯ga + svar
|
|
| 25 |
MAX_NEW_TOKENS = 128
|
| 26 |
K = 3
|
| 27 |
DEFAULT_TEMP = 0.3
|
|
|
|
| 28 |
|
| 29 |
# ββ LOGGING ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 30 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
|
@@ -87,13 +88,20 @@ def chat_fn(q, temp, max_new_tokens, k, ctx_tok_max, history):
|
|
| 87 |
history = history or []
|
| 88 |
history.append({"role": "user", "content": q})
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
if not docs:
|
| 93 |
history.append({"role": "assistant", "content": "π« Hittade inget relevant."})
|
| 94 |
return history, history
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
history.append({"role": "system", "content": f"π Chunkar som anvΓ€nds:\n{chunk_info}"})
|
| 98 |
|
| 99 |
def build_prompt_dynamic(query, docs, ctx_tok_max):
|
|
@@ -132,9 +140,9 @@ def chat_fn(q, temp, max_new_tokens, k, ctx_tok_max, history):
|
|
| 132 |
try:
|
| 133 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 134 |
future = executor.submit(generate)
|
| 135 |
-
ans = future.result(timeout=
|
| 136 |
except concurrent.futures.TimeoutError:
|
| 137 |
-
ans = "β° Ingen respons frΓ₯n modellen inom
|
| 138 |
except Exception as e:
|
| 139 |
log.exception("Genereringsβfel")
|
| 140 |
ans = f"β Fel vid generering: {type(e).__name__}: {e}\n\nPrompt:\n{prompt}"
|
|
@@ -161,12 +169,14 @@ with gr.Blocks() as demo:
|
|
| 161 |
k = gr.Slider(1, 10, value=K, step=1, label="Antal chunkar (K)")
|
| 162 |
ctx_tok_max = gr.Slider(100, 2000, value=CTX_TOK_MAX, step=50, label="Max kontexttokens")
|
| 163 |
b_send = gr.Button("Skicka")
|
|
|
|
| 164 |
|
| 165 |
chat = gr.Chatbot(type="messages", label="Chat")
|
| 166 |
chat_hist = gr.State([])
|
| 167 |
|
| 168 |
b_test.click(test_retrieval, inputs=[q_test], outputs=[o_test])
|
| 169 |
b_send.click(chat_fn, inputs=[q_in, temp, max_new_tokens, k, ctx_tok_max, chat_hist], outputs=[chat, chat_hist])
|
|
|
|
| 170 |
|
| 171 |
if __name__ == "__main__":
|
| 172 |
demo.launch(share=True) # ta bort share=True om du vill hΓ₯lla den privat
|
|
|
|
| 25 |
MAX_NEW_TOKENS = 128
|
| 26 |
K = 3
|
| 27 |
DEFAULT_TEMP = 0.3
|
| 28 |
+
GEN_TIMEOUT = 180 # Timeout fΓΆr generering i sekunder
|
| 29 |
|
| 30 |
# ββ LOGGING ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 31 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
|
|
|
| 88 |
history = history or []
|
| 89 |
history.append({"role": "user", "content": q})
|
| 90 |
|
| 91 |
+
# HΓ€mta chunkar och poΓ€ng
|
| 92 |
+
docs_and_scores = vs.similarity_search_with_score(q, k=int(k))
|
| 93 |
+
docs = [doc for doc, score in docs_and_scores]
|
| 94 |
+
scores = [score for doc, score in docs_and_scores]
|
| 95 |
+
|
| 96 |
if not docs:
|
| 97 |
history.append({"role": "assistant", "content": "π« Hittade inget relevant."})
|
| 98 |
return history, history
|
| 99 |
|
| 100 |
+
# Visa chunkar och poΓ€ng
|
| 101 |
+
chunk_info = "\n\n".join([
|
| 102 |
+
f"{i+1}. ({d.metadata['source']}) score={scores[i]:.3f}\n{d.page_content[:160]}β¦"
|
| 103 |
+
for i, d in enumerate(docs)
|
| 104 |
+
])
|
| 105 |
history.append({"role": "system", "content": f"π Chunkar som anvΓ€nds:\n{chunk_info}"})
|
| 106 |
|
| 107 |
def build_prompt_dynamic(query, docs, ctx_tok_max):
|
|
|
|
| 140 |
try:
|
| 141 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 142 |
future = executor.submit(generate)
|
| 143 |
+
ans = future.result(timeout=GEN_TIMEOUT) # Timeout in seconds
|
| 144 |
except concurrent.futures.TimeoutError:
|
| 145 |
+
ans = "β° Ingen respons frΓ₯n modellen inom {GEN_TIMEOUT} sekunder."
|
| 146 |
except Exception as e:
|
| 147 |
log.exception("Genereringsβfel")
|
| 148 |
ans = f"β Fel vid generering: {type(e).__name__}: {e}\n\nPrompt:\n{prompt}"
|
|
|
|
| 169 |
k = gr.Slider(1, 10, value=K, step=1, label="Antal chunkar (K)")
|
| 170 |
ctx_tok_max = gr.Slider(100, 2000, value=CTX_TOK_MAX, step=50, label="Max kontexttokens")
|
| 171 |
b_send = gr.Button("Skicka")
|
| 172 |
+
b_stop = gr.Button("Stoppa") # LΓ€gg till stoppknapp
|
| 173 |
|
| 174 |
chat = gr.Chatbot(type="messages", label="Chat")
|
| 175 |
chat_hist = gr.State([])
|
| 176 |
|
| 177 |
b_test.click(test_retrieval, inputs=[q_test], outputs=[o_test])
|
| 178 |
b_send.click(chat_fn, inputs=[q_in, temp, max_new_tokens, k, ctx_tok_max, chat_hist], outputs=[chat, chat_hist])
|
| 179 |
+
b_stop.click(None, cancels=[b_send])
|
| 180 |
|
| 181 |
if __name__ == "__main__":
|
| 182 |
demo.launch(share=True) # ta bort share=True om du vill hΓ₯lla den privat
|