Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from pdfminer.high_level import extract_text
|
| 4 |
from sentence_transformers import SentenceTransformer
|
|
@@ -6,13 +7,11 @@ from qdrant_client import QdrantClient
|
|
| 6 |
from qdrant_client.http import models as qmodels
|
| 7 |
import requests
|
| 8 |
|
| 9 |
-
|
| 10 |
-
EMBED_MODEL_ID = "intfloat/multilingual-e5-small" # 384-dim, multilingual
|
| 11 |
_embedder = None
|
| 12 |
_client = None
|
| 13 |
-
_collection =
|
| 14 |
|
| 15 |
-
# --------- HELPERS ---------
|
| 16 |
def get_embedder():
|
| 17 |
global _embedder
|
| 18 |
if _embedder is None:
|
|
@@ -20,16 +19,15 @@ def get_embedder():
|
|
| 20 |
return _embedder
|
| 21 |
|
| 22 |
def connect_qdrant(url: str, api_key: str, collection: str):
|
| 23 |
-
"""Connect & ensure collection exists."""
|
| 24 |
global _client, _collection
|
| 25 |
-
_collection = collection or "docs"
|
| 26 |
_client = QdrantClient(url=url, api_key=api_key) if url else None
|
|
|
|
| 27 |
if _client:
|
| 28 |
try:
|
| 29 |
-
_client.get_collection(
|
| 30 |
except Exception:
|
| 31 |
_client.recreate_collection(
|
| 32 |
-
collection_name=
|
| 33 |
vectors_config=qmodels.VectorParams(size=384, distance=qmodels.Distance.COSINE)
|
| 34 |
)
|
| 35 |
return True
|
|
@@ -39,9 +37,9 @@ def chunk_text(text: str, size: int = 1500, overlap: int = 200):
|
|
| 39 |
chunks, i = [], 0
|
| 40 |
step = max(1, size - overlap)
|
| 41 |
while i < len(text):
|
| 42 |
-
|
| 43 |
-
if
|
| 44 |
-
chunks.append(
|
| 45 |
i += step
|
| 46 |
return chunks
|
| 47 |
|
|
@@ -53,32 +51,27 @@ def embed_query(text: str):
|
|
| 53 |
emb = get_embedder()
|
| 54 |
return emb.encode([f"query: {text}"], normalize_embeddings=True)[0].tolist()
|
| 55 |
|
| 56 |
-
# --------- INGEST ---------
|
| 57 |
def ingest_pdfs(files, topic: str, chunk_chars: int, overlap: int):
|
| 58 |
if not _client:
|
| 59 |
-
return "❌ Najprv
|
| 60 |
-
|
| 61 |
for f in files or []:
|
| 62 |
data = f.read()
|
| 63 |
text = extract_text(io.BytesIO(data))
|
| 64 |
chunks = chunk_text(text, chunk_chars, overlap)
|
| 65 |
vecs = embed_passages(chunks)
|
| 66 |
-
points = [
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
]
|
| 70 |
_client.upsert(collection_name=_collection, points=points)
|
| 71 |
-
|
| 72 |
-
return f"✅
|
| 73 |
|
| 74 |
-
# --------- SEARCH + (optional) WEB ---------
|
| 75 |
def search_local(query: str, topk: int, topic: str):
|
| 76 |
if not _client:
|
| 77 |
return []
|
| 78 |
qvec = embed_query(query)
|
| 79 |
-
qfilter = qmodels.Filter(must=[qmodels.FieldCondition(
|
| 80 |
-
key="topic", match=qmodels.MatchValue(value=topic)
|
| 81 |
-
)]) if topic else None
|
| 82 |
res = _client.search(collection_name=_collection, query_vector=qvec, limit=topk, query_filter=qfilter)
|
| 83 |
return [{"score": r.score, "text": r.payload.get("text",""), "payload": r.payload} for r in res]
|
| 84 |
|
|
@@ -87,7 +80,10 @@ def web_search(query: str, n: int = 3, api_key: str | None = None):
|
|
| 87 |
if not key:
|
| 88 |
return []
|
| 89 |
r = requests.post("https://api.tavily.com/search", json={
|
| 90 |
-
"api_key": key,
|
|
|
|
|
|
|
|
|
|
| 91 |
}, timeout=30)
|
| 92 |
data = r.json()
|
| 93 |
out = []
|
|
@@ -97,59 +93,58 @@ def web_search(query: str, n: int = 3, api_key: str | None = None):
|
|
| 97 |
return out
|
| 98 |
|
| 99 |
def answer_from_context(question: str, local_hits, web_snips):
|
| 100 |
-
|
| 101 |
-
lines = [f"**Otázka:** {question}", "\n**Odpoveď (zo zdrojov):**"]
|
| 102 |
if local_hits:
|
| 103 |
-
|
| 104 |
for i,h in enumerate(local_hits):
|
| 105 |
-
snippet = h['text'][:400].strip().replace(
|
| 106 |
-
|
| 107 |
if web_snips:
|
| 108 |
-
|
| 109 |
for j,w in enumerate(web_snips):
|
| 110 |
-
snippet = w['text'][:300].strip().replace(
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
# --------- GRADIO UI ---------
|
| 117 |
with gr.Blocks(title="EDM & Fanuc – Hybrid RAG (PDF + Web)") as demo:
|
| 118 |
gr.Markdown("# 🔧 EDM & Fanuc – Hybrid RAG (PDF + Web)")
|
| 119 |
|
| 120 |
with gr.Row():
|
| 121 |
-
# LEFT: settings + ingest
|
| 122 |
with gr.Column(scale=1):
|
| 123 |
gr.Markdown("### Qdrant nastavenie")
|
| 124 |
-
q_url = gr.Textbox(label="QDRANT_URL", value=os.getenv("QDRANT_URL",""))
|
| 125 |
-
q_key = gr.Textbox(label="QDRANT_API_KEY", value=os.getenv("QDRANT_API_KEY",""), type="password")
|
| 126 |
collection_tb = gr.Textbox(label="Collection", value="docs")
|
| 127 |
connect_btn = gr.Button("🔌 Connect")
|
| 128 |
-
status = gr.
|
| 129 |
-
|
| 130 |
|
| 131 |
def _connect(u,k,c):
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
except Exception as e:
|
| 136 |
-
return "❌ Connection error", f"```\n{e}\n```"
|
| 137 |
-
connect_btn.click(_connect, inputs=[q_url,q_key,collection_tb], outputs=[status, err_md])
|
| 138 |
|
| 139 |
gr.Markdown("### Ingest PDF")
|
| 140 |
topic_dd = gr.Dropdown(["EDM","FANUC","general"], value="EDM", label="Topic")
|
| 141 |
chunk_chars = gr.Slider(800, 2500, value=1500, step=100, label="Chunk size")
|
| 142 |
overlap = gr.Slider(0, 400, value=200, step=20, label="Overlap")
|
| 143 |
-
upload = gr.File(file_count="multiple", file_types=[".pdf"], label="
|
| 144 |
ingest_btn = gr.Button("📥 Indexovať do Qdrant")
|
| 145 |
-
ingest_out = gr.
|
| 146 |
|
| 147 |
def _ingest(files, topic, size, ov):
|
| 148 |
-
msg = ingest_pdfs(files, topic, size, ov)
|
| 149 |
return msg
|
| 150 |
ingest_btn.click(_ingest, inputs=[upload, topic_dd, chunk_chars, overlap], outputs=ingest_out)
|
| 151 |
|
| 152 |
-
# RIGHT: chat
|
| 153 |
with gr.Column(scale=2):
|
| 154 |
gr.Markdown("### Chat (PDF + voliteľne web)")
|
| 155 |
question = gr.Textbox(label="Otázka", placeholder="Napr. G41 vs G42 na Fanuc…", lines=3)
|
|
@@ -170,5 +165,4 @@ with gr.Blocks(title="EDM & Fanuc – Hybrid RAG (PDF + Web)") as demo:
|
|
| 170 |
ask_btn.click(_ask, inputs=[question, topic_chat, topk, minscore, use_web, tavily_key], outputs=answer_md)
|
| 171 |
|
| 172 |
if __name__ == "__main__":
|
| 173 |
-
# Spaces dá port do env var PORT
|
| 174 |
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import os, io, re, json
|
| 3 |
import gradio as gr
|
| 4 |
from pdfminer.high_level import extract_text
|
| 5 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 7 |
from qdrant_client.http import models as qmodels
|
| 8 |
import requests
|
| 9 |
|
| 10 |
+
EMBED_MODEL_ID = "intfloat/multilingual-e5-small"
|
|
|
|
| 11 |
_embedder = None
|
| 12 |
_client = None
|
| 13 |
+
_collection = None
|
| 14 |
|
|
|
|
| 15 |
def get_embedder():
|
| 16 |
global _embedder
|
| 17 |
if _embedder is None:
|
|
|
|
| 19 |
return _embedder
|
| 20 |
|
| 21 |
def connect_qdrant(url: str, api_key: str, collection: str):
|
|
|
|
| 22 |
global _client, _collection
|
|
|
|
| 23 |
_client = QdrantClient(url=url, api_key=api_key) if url else None
|
| 24 |
+
_collection = collection
|
| 25 |
if _client:
|
| 26 |
try:
|
| 27 |
+
_client.get_collection(collection)
|
| 28 |
except Exception:
|
| 29 |
_client.recreate_collection(
|
| 30 |
+
collection_name=collection,
|
| 31 |
vectors_config=qmodels.VectorParams(size=384, distance=qmodels.Distance.COSINE)
|
| 32 |
)
|
| 33 |
return True
|
|
|
|
| 37 |
chunks, i = [], 0
|
| 38 |
step = max(1, size - overlap)
|
| 39 |
while i < len(text):
|
| 40 |
+
chunk = text[i:i+size]
|
| 41 |
+
if chunk.strip():
|
| 42 |
+
chunks.append(chunk)
|
| 43 |
i += step
|
| 44 |
return chunks
|
| 45 |
|
|
|
|
| 51 |
emb = get_embedder()
|
| 52 |
return emb.encode([f"query: {text}"], normalize_embeddings=True)[0].tolist()
|
| 53 |
|
|
|
|
| 54 |
def ingest_pdfs(files, topic: str, chunk_chars: int, overlap: int):
|
| 55 |
if not _client:
|
| 56 |
+
return "❌ Najprv nastav QDRANT_URL a QDRANT_API_KEY vľavo.", None
|
| 57 |
+
total_chunks = 0
|
| 58 |
for f in files or []:
|
| 59 |
data = f.read()
|
| 60 |
text = extract_text(io.BytesIO(data))
|
| 61 |
chunks = chunk_text(text, chunk_chars, overlap)
|
| 62 |
vecs = embed_passages(chunks)
|
| 63 |
+
points = []
|
| 64 |
+
for c, v in zip(chunks, vecs):
|
| 65 |
+
points.append(qmodels.PointStruct(vector=v, payload={"text": c, "topic": topic}))
|
|
|
|
| 66 |
_client.upsert(collection_name=_collection, points=points)
|
| 67 |
+
total_chunks += len(chunks)
|
| 68 |
+
return f"✅ Uložené chunky: {total_chunks}", None
|
| 69 |
|
|
|
|
| 70 |
def search_local(query: str, topk: int, topic: str):
|
| 71 |
if not _client:
|
| 72 |
return []
|
| 73 |
qvec = embed_query(query)
|
| 74 |
+
qfilter = qmodels.Filter(must=[qmodels.FieldCondition(key="topic", match=qmodels.MatchValue(value=topic))]) if topic else None
|
|
|
|
|
|
|
| 75 |
res = _client.search(collection_name=_collection, query_vector=qvec, limit=topk, query_filter=qfilter)
|
| 76 |
return [{"score": r.score, "text": r.payload.get("text",""), "payload": r.payload} for r in res]
|
| 77 |
|
|
|
|
| 80 |
if not key:
|
| 81 |
return []
|
| 82 |
r = requests.post("https://api.tavily.com/search", json={
|
| 83 |
+
"api_key": key,
|
| 84 |
+
"query": query,
|
| 85 |
+
"search_depth": "basic",
|
| 86 |
+
"max_results": n
|
| 87 |
}, timeout=30)
|
| 88 |
data = r.json()
|
| 89 |
out = []
|
|
|
|
| 93 |
return out
|
| 94 |
|
| 95 |
def answer_from_context(question: str, local_hits, web_snips):
|
| 96 |
+
parts = ["**Otázka:** " + question, "\n**Odpoveď (kompilované zo zdrojov):**"]
|
|
|
|
| 97 |
if local_hits:
|
| 98 |
+
parts.append("- Z lokálnych PDF vyplýva:")
|
| 99 |
for i,h in enumerate(local_hits):
|
| 100 |
+
snippet = h['text'][:400].strip().replace('\n',' ')
|
| 101 |
+
parts.append(f" [{i+1}] {snippet} … (score {h['score']:.2f})")
|
| 102 |
if web_snips:
|
| 103 |
+
parts.append("- Z webu:")
|
| 104 |
for j,w in enumerate(web_snips):
|
| 105 |
+
snippet = w['text'][:300].strip().replace('\n',' ')
|
| 106 |
+
parts.append(f" [W{j+1}] {snippet} … (zdroj: {w['url']})")
|
| 107 |
+
parts.append("\n**Poznámka:** Toto je súhrn citovaných pasáží. Pre presnejšiu odpoveď pridaj viac PDF alebo zapni lepší model neskôr.")
|
| 108 |
+
return "\n".join(parts)
|
| 109 |
+
|
| 110 |
+
def on_connect(url, key, collection):
|
| 111 |
+
try:
|
| 112 |
+
connect_qdrant(url, key, collection)
|
| 113 |
+
return f"✅ Pripojené k Qdrant: {url}", ""
|
| 114 |
+
except Exception as e:
|
| 115 |
+
return "❌ Chyba pripojenia", str(e)
|
| 116 |
|
|
|
|
| 117 |
with gr.Blocks(title="EDM & Fanuc – Hybrid RAG (PDF + Web)") as demo:
|
| 118 |
gr.Markdown("# 🔧 EDM & Fanuc – Hybrid RAG (PDF + Web)")
|
| 119 |
|
| 120 |
with gr.Row():
|
|
|
|
| 121 |
with gr.Column(scale=1):
|
| 122 |
gr.Markdown("### Qdrant nastavenie")
|
| 123 |
+
q_url = gr.Textbox(label="QDRANT_URL", value=os.getenv("QDRANT_URL", ""))
|
| 124 |
+
q_key = gr.Textbox(label="QDRANT_API_KEY", value=os.getenv("QDRANT_API_KEY", ""), type="password")
|
| 125 |
collection_tb = gr.Textbox(label="Collection", value="docs")
|
| 126 |
connect_btn = gr.Button("🔌 Connect")
|
| 127 |
+
status = gr.Textbox(label="Status")
|
| 128 |
+
err = gr.Textbox(label="Error")
|
| 129 |
|
| 130 |
def _connect(u,k,c):
|
| 131 |
+
s,e = on_connect(u,k,c)
|
| 132 |
+
return s,e
|
| 133 |
+
connect_btn.click(_connect, inputs=[q_url,q_key,collection_tb], outputs=[status, err])
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
gr.Markdown("### Ingest PDF")
|
| 136 |
topic_dd = gr.Dropdown(["EDM","FANUC","general"], value="EDM", label="Topic")
|
| 137 |
chunk_chars = gr.Slider(800, 2500, value=1500, step=100, label="Chunk size")
|
| 138 |
overlap = gr.Slider(0, 400, value=200, step=20, label="Overlap")
|
| 139 |
+
upload = gr.File(file_count="multiple", file_types=[".pdf"], label="PDF súbory")
|
| 140 |
ingest_btn = gr.Button("📥 Indexovať do Qdrant")
|
| 141 |
+
ingest_out = gr.Textbox(label="Výstup")
|
| 142 |
|
| 143 |
def _ingest(files, topic, size, ov):
|
| 144 |
+
msg,_ = ingest_pdfs(files, topic, size, ov)
|
| 145 |
return msg
|
| 146 |
ingest_btn.click(_ingest, inputs=[upload, topic_dd, chunk_chars, overlap], outputs=ingest_out)
|
| 147 |
|
|
|
|
| 148 |
with gr.Column(scale=2):
|
| 149 |
gr.Markdown("### Chat (PDF + voliteľne web)")
|
| 150 |
question = gr.Textbox(label="Otázka", placeholder="Napr. G41 vs G42 na Fanuc…", lines=3)
|
|
|
|
| 165 |
ask_btn.click(_ask, inputs=[question, topic_chat, topk, minscore, use_web, tavily_key], outputs=answer_md)
|
| 166 |
|
| 167 |
if __name__ == "__main__":
|
|
|
|
| 168 |
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|