Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,180 +1,580 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
| 43 |
|
| 44 |
-
def get_recommendations(query, category="All", gender="All", diversity=False, n=3):
|
| 45 |
-
emb = model.encode(query, convert_to_numpy=True).reshape(1, -1)
|
| 46 |
-
sims = cosine_similarity(emb, EMBED_MATRIX)[0]
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
indices = _mmr_select(sims, fsims, n) if diversity \
|
| 55 |
-
else np.argsort(fsims)[::-1][:n].tolist()
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
results = []
|
| 58 |
-
for idx in
|
| 59 |
-
row
|
| 60 |
-
ds_idx = id_to_idx.get(int(row["id"]), -1)
|
| 61 |
results.append({
|
| 62 |
-
"
|
| 63 |
-
"
|
| 64 |
-
"
|
| 65 |
-
"
|
| 66 |
-
"
|
| 67 |
-
"similarity": float(sims[idx]),
|
| 68 |
})
|
| 69 |
return results
|
| 70 |
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
</div>"""
|
| 83 |
-
html += "</div>"
|
| 84 |
-
return html
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
def text_search(text, category, gender, diversity):
|
| 89 |
-
if not text.strip():
|
| 90 |
-
return None, None, None, "<p style='color:red'>โ ๏ธ Please enter a search query.</p>"
|
| 91 |
-
r = get_recommendations(text, category, gender, diversity)
|
| 92 |
-
imgs = [x["image"] for x in r]
|
| 93 |
-
while len(imgs) < 3: imgs.append(None)
|
| 94 |
-
return imgs[0], imgs[1], imgs[2], build_info_html(r)
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
|
| 107 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="Fashion Visual Search") as demo:
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
-
# โโ LEFT: Inputs โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 120 |
-
with gr.Column(scale=1, min_width=300):
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
with gr.Tab("๐ผ๏ธ Image Search"):
|
| 131 |
-
image_input = gr.Image(type="pil", label="Upload a fashion item photo")
|
| 132 |
-
image_btn = gr.Button("๐ Find Similar Items", variant="primary", size="lg")
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
| 139 |
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
with gr.Column(scale=2):
|
| 142 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
with gr.Row():
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
out3 = gr.Image(label="Match #3", show_label=True)
|
| 147 |
-
info_out = gr.HTML()
|
| 148 |
-
|
| 149 |
-
# โโ Examples โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 150 |
-
gr.Examples(
|
| 151 |
-
examples=[
|
| 152 |
-
["elegant women black handbag"],
|
| 153 |
-
["blue casual men shirt"],
|
| 154 |
-
["sports running shoes"],
|
| 155 |
-
["gold women watch"],
|
| 156 |
-
["red summer dress women"],
|
| 157 |
-
],
|
| 158 |
-
inputs=text_input,
|
| 159 |
-
label="๐ก Try these examples (click to load, then Search)"
|
| 160 |
-
)
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
)
|
| 174 |
-
|
| 175 |
-
fn=
|
| 176 |
-
|
| 177 |
-
outputs=[out1, out2, out3, info_out]
|
| 178 |
)
|
| 179 |
|
| 180 |
-
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
Text2Receipt โ Hugging Face Gradio Space
|
| 4 |
+
=========================================
|
| 5 |
+
Pipeline:
|
| 6 |
+
raw Hebrew note
|
| 7 |
+
โ fine-tuned parser (gemma-2-2b-it + LoRA)
|
| 8 |
+
โ agentic clarifier (+5% bonus โ detects legally-ambiguous fields)
|
| 9 |
+
โ deterministic complete() (VAT ยท serial ยท allocation number)
|
| 10 |
+
โ fiscal document display
|
| 11 |
+
โ FAISS recommender (3 similar past receipts)
|
| 12 |
+
|
| 13 |
+
Repos used:
|
| 14 |
+
Dataset : yonilev/Text2Receipt
|
| 15 |
+
Model : yonilev/Text2Receipt-parser (LoRA adapter + embeddings)
|
| 16 |
+
Space : yonilev/Text2Receipt
|
| 17 |
+
"""
|
| 18 |
+
import os, json, re, random
|
| 19 |
import numpy as np
|
| 20 |
+
import gradio as gr
|
| 21 |
+
|
| 22 |
+
# โโ repo constants โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 23 |
+
DATASET_REPO = "yonilev/Text2Receipt"
|
| 24 |
+
MODEL_REPO = "yonilev/Text2Receipt-parser"
|
| 25 |
+
BASE_MODEL = os.environ.get("BASE_MODEL", "unsloth/gemma-2-2b-it")
|
| 26 |
+
|
| 27 |
+
import t2r_core as core
|
| 28 |
+
|
| 29 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 30 |
+
# 1. Lazy init โ load heavy resources once on first request
|
| 31 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 32 |
+
_STATE: dict = {"ready": False}
|
| 33 |
+
|
| 34 |
+
def _lazy_init():
|
| 35 |
+
if _STATE["ready"]:
|
| 36 |
+
return
|
| 37 |
+
import torch
|
| 38 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 39 |
+
from peft import PeftModel
|
| 40 |
+
from sentence_transformers import SentenceTransformer
|
| 41 |
+
import faiss
|
| 42 |
+
from huggingface_hub import hf_hub_download
|
| 43 |
+
import pandas as pd
|
| 44 |
+
|
| 45 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 46 |
+
_STATE["device"] = device
|
| 47 |
+
_STATE["rng"] = random.Random(42)
|
| 48 |
+
|
| 49 |
+
# โโ parser model โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 50 |
+
bnb = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4",
|
| 51 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 52 |
+
bnb_4bit_use_double_quant=True)
|
| 53 |
+
tok = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 54 |
+
if tok.pad_token is None:
|
| 55 |
+
tok.pad_token = tok.eos_token
|
| 56 |
+
tok.padding_side = "left"
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
base = AutoModelForCausalLM.from_pretrained(
|
| 60 |
+
BASE_MODEL, quantization_config=bnb, device_map="auto",
|
| 61 |
+
torch_dtype=torch.float16, attn_implementation="eager")
|
| 62 |
+
model = PeftModel.from_pretrained(base, MODEL_REPO)
|
| 63 |
+
print("โ
loaded fine-tuned adapter from", MODEL_REPO)
|
| 64 |
+
except Exception as e:
|
| 65 |
+
print(f"โ adapter load failed ({e}); falling back to base model")
|
| 66 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 67 |
+
BASE_MODEL, quantization_config=bnb, device_map="auto",
|
| 68 |
+
torch_dtype=torch.float16, attn_implementation="eager")
|
| 69 |
+
|
| 70 |
+
model.eval()
|
| 71 |
+
_STATE["tok"] = tok
|
| 72 |
+
_STATE["model"] = model
|
| 73 |
+
|
| 74 |
+
# โโ embeddings + FAISS โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 75 |
+
emb_path = hf_hub_download(MODEL_REPO, "receipts_embeddings.npy", repo_type="model")
|
| 76 |
+
store_path = hf_hub_download(MODEL_REPO, "receipts_store.parquet", repo_type="model")
|
| 77 |
+
manifest_path = hf_hub_download(MODEL_REPO, "embeddings_manifest.json", repo_type="model")
|
| 78 |
+
|
| 79 |
+
emb = np.load(emb_path).astype("float32")
|
| 80 |
+
store = pd.read_parquet(store_path)
|
| 81 |
+
with open(manifest_path) as f:
|
| 82 |
+
manifest = json.load(f)
|
| 83 |
+
|
| 84 |
+
faiss.normalize_L2(emb)
|
| 85 |
+
index = faiss.IndexFlatIP(emb.shape[1])
|
| 86 |
+
index.add(emb)
|
| 87 |
+
|
| 88 |
+
enc = SentenceTransformer(manifest["embed_model"],
|
| 89 |
+
device=device)
|
| 90 |
+
_STATE["enc"] = enc
|
| 91 |
+
_STATE["index"] = index
|
| 92 |
+
_STATE["store"] = store
|
| 93 |
+
_STATE["e5_family"] = manifest.get("e5_family", False)
|
| 94 |
+
_STATE["ready"] = True
|
| 95 |
+
print("โ
FAISS index ready โ", emb.shape[0], "vectors")
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 99 |
+
# 2. Parser helpers
|
| 100 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 101 |
+
INSTRUCTION = (
|
| 102 |
+
"ืืชื ืืืืจ ืืขืจืช ืืื ืกื ืืืคืฉืืช ืืขืืจืืช ืืืื ื JSON. "
|
| 103 |
+
"ืืืฅ ืื ืืจืง ืืช ืื ืฉืืชืื ืืืขืจื: ืฉื ืืืงืื (client_name), "
|
| 104 |
+
"ืืื ืืืงืื ืขืกืง (client_is_business), ืืจืฉืืืช ืคืจืืืื (items) "
|
| 105 |
+
"ืืืฉืจ ืืื ืคืจืื ืชืืืืจ (description), ืืืืจ ืืืืืื (unit_price) ืืืืืช (quantity). "
|
| 106 |
+
"ืืืืจ JSON ืชืงืื ืืืื, ืืื ืืงืกื ื ืืกืฃ."
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
def _build_prompt(raw_text: str) -> str:
|
| 110 |
+
return f"{INSTRUCTION}\n\nืืขืจื: {raw_text}\n\nJSON:"
|
| 111 |
+
|
| 112 |
+
def _extract_json(text: str) -> dict | None:
|
| 113 |
+
s = text.find("{")
|
| 114 |
+
if s < 0:
|
| 115 |
+
return None
|
| 116 |
+
depth = 0
|
| 117 |
+
for i in range(s, len(text)):
|
| 118 |
+
if text[i] == "{": depth += 1
|
| 119 |
+
elif text[i] == "}":
|
| 120 |
+
depth -= 1
|
| 121 |
+
if depth == 0:
|
| 122 |
+
try: return json.loads(text[s:i+1])
|
| 123 |
+
except: return None
|
| 124 |
+
return None
|
| 125 |
+
|
| 126 |
+
def model_parse(raw_text: str) -> dict | None:
|
| 127 |
+
"""Run fine-tuned model; return parse dict or None on failure."""
|
| 128 |
+
tok, model = _STATE["tok"], _STATE["model"]
|
| 129 |
+
msgs = [{"role": "user", "content": _build_prompt(raw_text)}]
|
| 130 |
+
prompt = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
|
| 131 |
+
enc = tok(prompt, return_tensors="pt",
|
| 132 |
+
add_special_tokens=False).to(model.device)
|
| 133 |
+
import torch
|
| 134 |
+
with torch.no_grad():
|
| 135 |
+
out = model.generate(**enc, max_new_tokens=200,
|
| 136 |
+
do_sample=False,
|
| 137 |
+
pad_token_id=tok.pad_token_id)
|
| 138 |
+
decoded = tok.decode(out[0, enc["input_ids"].shape[1]:],
|
| 139 |
+
skip_special_tokens=True)
|
| 140 |
+
return _extract_json(decoded)
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 144 |
+
# 3. Agentic clarification logic (+5% bonus)
|
| 145 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 146 |
+
import datetime as _dt
|
| 147 |
+
|
| 148 |
+
def _today() -> _dt.date:
|
| 149 |
+
return _dt.date.today()
|
| 150 |
+
|
| 151 |
+
def _missing_fields(parse: dict | None) -> list[str]:
|
| 152 |
+
"""Return list of missing mandatory fields that affect the document legally."""
|
| 153 |
+
missing = []
|
| 154 |
+
if parse is None:
|
| 155 |
+
return ["parse_failed"]
|
| 156 |
+
if not parse.get("client_name", "").strip():
|
| 157 |
+
missing.append("client_name")
|
| 158 |
+
if not parse.get("items"):
|
| 159 |
+
missing.append("items")
|
| 160 |
+
# doc_type not extractable from notes โ always needs clarification for allocation
|
| 161 |
+
return missing
|
| 162 |
+
|
| 163 |
+
def _needs_allocation_clarification(parse: dict) -> bool:
|
| 164 |
+
"""Return True when the doc_type choice changes whether allocation is required."""
|
| 165 |
+
if parse is None:
|
| 166 |
+
return False
|
| 167 |
+
subtotal = sum(
|
| 168 |
+
it.get("unit_price", 0) * it.get("quantity", 1)
|
| 169 |
+
for it in parse.get("items", [])
|
| 170 |
+
)
|
| 171 |
+
threshold = core.allocation_threshold_for_date(_today())
|
| 172 |
+
is_biz = parse.get("client_is_business", False)
|
| 173 |
+
# allocation only matters for authorized dealer + biz client + subtotal โฅ threshold
|
| 174 |
+
# but we don't know issuer status here; ask when subtotal is near or above threshold
|
| 175 |
+
return bool(is_biz) and subtotal >= threshold * 0.8 # 80% buffer
|
| 176 |
+
|
| 177 |
+
def clarification_questions(parse: dict | None, answers: dict) -> list[str]:
|
| 178 |
+
"""
|
| 179 |
+
Agentic loop: generate next question(s) given current parse + prior answers.
|
| 180 |
+
Returns [] when all necessary info is available.
|
| 181 |
+
"""
|
| 182 |
+
questions = []
|
| 183 |
+
missing = _missing_fields(parse)
|
| 184 |
+
|
| 185 |
+
if "parse_failed" in missing:
|
| 186 |
+
questions.append("ืื ืืฆืืืชื ืืืืฅ ืคืจืืื. ืืื ืชืืื ืื ืกื ืืืืฉ? ืืืืืื: 'ืงืืืืชื 500โช ื[ืฉื] ืขื [ืฉืืจืืช]'")
|
| 187 |
+
return questions
|
| 188 |
+
|
| 189 |
+
if "client_name" in missing and "client_name" not in answers:
|
| 190 |
+
questions.append("ืื ืฉื ืืืงืื?")
|
| 191 |
+
if "items" in missing and "items" not in answers:
|
| 192 |
+
questions.append("ืื ืืฉืืจืืช/ืืืืฆืจ ืฉืกืืคืง ืืืืืื ืืืืจ?")
|
| 193 |
+
|
| 194 |
+
# ask doc_type only if not already answered and allocation is relevant
|
| 195 |
+
if "doc_type" not in answers:
|
| 196 |
+
if parse and _needs_allocation_clarification(parse):
|
| 197 |
+
questions.append(
|
| 198 |
+
"ืืื ืืื ืคืืง ืืฉืืื ืืช ืืก/ืงืืื (ืืขืกืง ืขื ืืข\"ื) ืื ืงืืื ืคืฉืืื?"
|
| 199 |
+
)
|
| 200 |
+
elif parse and not parse.get("doc_type"):
|
| 201 |
+
questions.append(
|
| 202 |
+
"ืืืื ืืกืื ืืืืฆืจ? ืงืืื / ืืฉืืื ืืช ืืก / ืืฉืืื ืืช ืืก ืืงืืื?"
|
| 203 |
+
)
|
| 204 |
|
| 205 |
+
return questions[:3] # max 3 questions per round
|
| 206 |
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
+
def apply_answers(parse: dict | None, answers: dict) -> dict:
|
| 209 |
+
"""Merge user answers into parse dict."""
|
| 210 |
+
if parse is None:
|
| 211 |
+
parse = {}
|
| 212 |
+
p = dict(parse)
|
| 213 |
+
if "client_name" in answers:
|
| 214 |
+
p["client_name"] = answers["client_name"]
|
| 215 |
+
if "doc_type" in answers:
|
| 216 |
+
raw = answers["doc_type"].strip()
|
| 217 |
+
if "ืืก/ืงืืื" in raw or "ืืก ืืงืืื" in raw:
|
| 218 |
+
p["doc_type"] = "tax_invoice_receipt"
|
| 219 |
+
elif "ืืก" in raw:
|
| 220 |
+
p["doc_type"] = "tax_invoice"
|
| 221 |
+
else:
|
| 222 |
+
p["doc_type"] = "receipt"
|
| 223 |
+
# set safe defaults for missing required fields
|
| 224 |
+
p.setdefault("doc_type", "receipt")
|
| 225 |
+
p.setdefault("date", _today().isoformat())
|
| 226 |
+
p.setdefault("payment_method", "bank_transfer")
|
| 227 |
+
p.setdefault("amount_basis", "net")
|
| 228 |
+
p.setdefault("currency", "ILS")
|
| 229 |
+
p.setdefault("client_tax_id", None)
|
| 230 |
+
p.setdefault("client_is_business", False)
|
| 231 |
+
p.setdefault("items", [])
|
| 232 |
+
return p
|
| 233 |
|
|
|
|
|
|
|
| 234 |
|
| 235 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 236 |
+
# 4. FAISS recommender
|
| 237 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 238 |
+
def recommend(query_text: str, k: int = 3) -> list[dict]:
|
| 239 |
+
"""Return k most similar receipts from the full corpus store."""
|
| 240 |
+
enc = _STATE["enc"]
|
| 241 |
+
index = _STATE["index"]
|
| 242 |
+
store = _STATE["store"]
|
| 243 |
+
pref = "query: " if _STATE["e5_family"] else ""
|
| 244 |
+
q_emb = enc.encode([pref + query_text],
|
| 245 |
+
normalize_embeddings=True).astype("float32")
|
| 246 |
+
_, I = index.search(q_emb, k + 1) # +1 in case query is in corpus
|
| 247 |
results = []
|
| 248 |
+
for idx in I[0][:k]:
|
| 249 |
+
row = store.iloc[int(idx)]
|
|
|
|
| 250 |
results.append({
|
| 251 |
+
"raw_text": row["raw_text"],
|
| 252 |
+
"category": row["category"],
|
| 253 |
+
"doc_type_he": row["doc_type_he"],
|
| 254 |
+
"total": float(row["total"]),
|
| 255 |
+
"client_name": row["client_name"],
|
|
|
|
| 256 |
})
|
| 257 |
return results
|
| 258 |
|
| 259 |
|
| 260 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 261 |
+
# 5. Document renderer (HTML card)
|
| 262 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 263 |
+
def render_document(completed: dict) -> str:
|
| 264 |
+
c = completed
|
| 265 |
+
issuer = c.get("issuer", {})
|
| 266 |
+
client = c.get("client", {})
|
| 267 |
+
lines = c.get("lines", [])
|
| 268 |
+
|
| 269 |
+
alloc_html = ""
|
| 270 |
+
if c.get("allocation_required"):
|
| 271 |
+
alloc_html = f"""
|
| 272 |
+
<div class="alloc-badge">
|
| 273 |
+
๐ ืืกืคืจ ืืงืฆืื: <strong>{c.get('allocation_number','โ')}</strong>
|
| 274 |
</div>"""
|
|
|
|
|
|
|
| 275 |
|
| 276 |
+
line_rows = "".join(
|
| 277 |
+
f"<tr><td>{ln['description']}</td>"
|
| 278 |
+
f"<td>{ln['quantity']}</td>"
|
| 279 |
+
f"<td>โช{ln['unit_price']:,.2f}</td>"
|
| 280 |
+
f"<td>โช{ln['line_total']:,.2f}</td></tr>"
|
| 281 |
+
for ln in lines
|
| 282 |
+
)
|
| 283 |
|
| 284 |
+
vat_pct = int(round(c.get("vat_rate", 0) * 100))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
|
| 286 |
+
return f"""
|
| 287 |
+
<div dir="rtl" style="font-family:'Arial Hebrew',Arial,sans-serif;
|
| 288 |
+
background:#0f1117; color:#e8eaf6; border-radius:16px;
|
| 289 |
+
padding:28px 32px; max-width:680px; margin:0 auto;
|
| 290 |
+
border:1px solid #2a2d3e; box-shadow:0 4px 32px rgba(0,0,0,.4);">
|
| 291 |
|
| 292 |
+
<!-- header -->
|
| 293 |
+
<div style="display:flex;justify-content:space-between;align-items:flex-start;
|
| 294 |
+
border-bottom:2px solid #0d9488;padding-bottom:16px;margin-bottom:20px;">
|
| 295 |
+
<div>
|
| 296 |
+
<div style="font-size:22px;font-weight:700;color:#0d9488;">
|
| 297 |
+
{c.get('doc_type_he','ืืกืื')}
|
| 298 |
+
</div>
|
| 299 |
+
<div style="font-size:13px;color:#9e9e9e;margin-top:4px;">
|
| 300 |
+
ืืกืณ {c.get('serial_number','')} | {c.get('issue_date','')}
|
| 301 |
+
</div>
|
| 302 |
+
</div>
|
| 303 |
+
<div style="text-align:left;">
|
| 304 |
+
<div style="font-size:15px;font-weight:600;">{issuer.get('name','')}</div>
|
| 305 |
+
<div style="font-size:12px;color:#9e9e9e;">ื.ืค. {issuer.get('tax_id','')}</div>
|
| 306 |
+
<div style="font-size:12px;color:#9e9e9e;">
|
| 307 |
+
{'ืขืืกืง ืืืจืฉื' if issuer.get('status')=='authorized_dealer' else 'ืขืืกืง ืคืืืจ'}
|
| 308 |
+
</div>
|
| 309 |
+
</div>
|
| 310 |
+
</div>
|
| 311 |
|
| 312 |
+
<!-- client -->
|
| 313 |
+
<div style="background:#1a1d2e;border-radius:10px;padding:14px 18px;
|
| 314 |
+
margin-bottom:20px;font-size:14px;">
|
| 315 |
+
<span style="color:#9e9e9e;">ืืงืื: </span>
|
| 316 |
+
<strong>{client.get('name','')}</strong>
|
| 317 |
+
{'<span style="margin-right:10px;font-size:12px;color:#9e9e9e;">ืขืกืง</span>' if client.get('is_business') else ''}
|
| 318 |
+
{f'<span style="font-size:12px;color:#9e9e9e;"> ยท ื.ืค. {client.get("tax_id","")}</span>' if client.get('tax_id') else ''}
|
| 319 |
+
</div>
|
| 320 |
|
| 321 |
+
{alloc_html}
|
|
|
|
| 322 |
|
| 323 |
+
<!-- line items -->
|
| 324 |
+
<table style="width:100%;border-collapse:collapse;font-size:14px;
|
| 325 |
+
margin-bottom:18px;">
|
| 326 |
+
<thead>
|
| 327 |
+
<tr style="color:#0d9488;border-bottom:1px solid #2a2d3e;text-align:right;">
|
| 328 |
+
<th style="padding:6px 0;">ืฉืืจืืช/ืืืฆืจ</th>
|
| 329 |
+
<th style="padding:6px 8px;">ืืืืช</th>
|
| 330 |
+
<th style="padding:6px 8px;">ืืืืจ ืืืืื</th>
|
| 331 |
+
<th style="padding:6px 0;">ืกื"ื ืฉืืจื</th>
|
| 332 |
+
</tr>
|
| 333 |
+
</thead>
|
| 334 |
+
<tbody style="color:#e8eaf6;">
|
| 335 |
+
{line_rows}
|
| 336 |
+
</tbody>
|
| 337 |
+
</table>
|
| 338 |
|
| 339 |
+
<!-- totals -->
|
| 340 |
+
<div style="border-top:1px solid #2a2d3e;padding-top:14px;
|
| 341 |
+
font-size:14px;text-align:left;">
|
| 342 |
+
<div style="display:flex;justify-content:space-between;margin-bottom:4px;">
|
| 343 |
+
<span style="color:#9e9e9e;">ืกืืื ืืคื ื ืืข"ื</span>
|
| 344 |
+
<span>โช{c.get('subtotal',0):,.2f}</span>
|
| 345 |
+
</div>
|
| 346 |
+
<div style="display:flex;justify-content:space-between;margin-bottom:4px;">
|
| 347 |
+
<span style="color:#9e9e9e;">ืืข"ื {vat_pct}%</span>
|
| 348 |
+
<span>โช{c.get('vat_amount',0):,.2f}</span>
|
| 349 |
+
</div>
|
| 350 |
+
<div style="display:flex;justify-content:space-between;
|
| 351 |
+
font-size:18px;font-weight:700;color:#0d9488;margin-top:8px;">
|
| 352 |
+
<span>ืกื"ื ืืชืฉืืื</span>
|
| 353 |
+
<span>โช{c.get('total',0):,.2f}</span>
|
| 354 |
+
</div>
|
| 355 |
+
<div style="font-size:12px;color:#9e9e9e;margin-top:8px;text-align:right;">
|
| 356 |
+
ืืืฆืขื ืชืฉืืื: {core.PAYMENT_HE.get(c.get('payment_method',''),'โ')}
|
| 357 |
+
</div>
|
| 358 |
+
</div>
|
| 359 |
+
</div>"""
|
| 360 |
|
|
|
|
|
|
|
| 361 |
|
| 362 |
+
def render_recommendations(recs: list[dict]) -> str:
|
| 363 |
+
if not recs:
|
| 364 |
+
return ""
|
| 365 |
+
cards = []
|
| 366 |
+
for r in recs:
|
| 367 |
+
cards.append(f"""
|
| 368 |
+
<div style="background:#1a1d2e;border-radius:10px;padding:14px 16px;
|
| 369 |
+
border:1px solid #2a2d3e;font-size:13px;">
|
| 370 |
+
<div style="color:#0d9488;font-weight:600;margin-bottom:6px;">
|
| 371 |
+
{r['doc_type_he']} ยท {r['category']}
|
| 372 |
+
</div>
|
| 373 |
+
<div style="color:#e8eaf6;margin-bottom:4px;line-height:1.4;">
|
| 374 |
+
{r['raw_text'][:80]}{'โฆ' if len(r['raw_text'])>80 else ''}
|
| 375 |
+
</div>
|
| 376 |
+
<div style="color:#9e9e9e;">
|
| 377 |
+
{r['client_name']} ยท โช{r['total']:,.0f}
|
| 378 |
+
</div>
|
| 379 |
+
</div>""")
|
| 380 |
+
|
| 381 |
+
return f"""
|
| 382 |
+
<div dir="rtl" style="font-family:Arial,sans-serif;max-width:680px;margin:16px auto 0;">
|
| 383 |
+
<div style="font-size:14px;color:#9e9e9e;margin-bottom:10px;">
|
| 384 |
+
๐ 3 ืงืืืืช ืืืืืช ืืืืืืจ
|
| 385 |
+
</div>
|
| 386 |
+
<div style="display:grid;grid-template-columns:repeat(3,1fr);gap:12px;">
|
| 387 |
+
{''.join(cards)}
|
| 388 |
+
</div>
|
| 389 |
+
</div>"""
|
| 390 |
+
|
| 391 |
+
|
| 392 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 393 |
+
# 6. Gradio UI
|
| 394 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 395 |
+
CSS = """
|
| 396 |
+
:root {
|
| 397 |
+
--bg: #0b0b0f;
|
| 398 |
+
--panel: #111118;
|
| 399 |
+
--border: #1e2130;
|
| 400 |
+
--teal: #0d9488;
|
| 401 |
+
--text: #e8eaf6;
|
| 402 |
+
--muted: #9e9e9e;
|
| 403 |
+
}
|
| 404 |
+
html, body, .gradio-container {
|
| 405 |
+
background: var(--bg) !important;
|
| 406 |
+
color: var(--text);
|
| 407 |
+
font-family: Arial, 'Arial Hebrew', sans-serif;
|
| 408 |
+
}
|
| 409 |
+
.gr-button-primary {
|
| 410 |
+
background: var(--teal) !important;
|
| 411 |
+
border: none !important;
|
| 412 |
+
}
|
| 413 |
+
textarea, input[type=text] {
|
| 414 |
+
background: var(--panel) !important;
|
| 415 |
+
color: var(--text) !important;
|
| 416 |
+
border: 1px solid var(--border) !important;
|
| 417 |
+
border-radius: 8px !important;
|
| 418 |
+
}
|
| 419 |
+
.chat-bubble-user { background: #1a2a3a !important; }
|
| 420 |
+
.chat-bubble-bot { background: #1a1d2e !important; }
|
| 421 |
+
footer { display: none !important; }
|
| 422 |
+
"""
|
| 423 |
+
|
| 424 |
+
QUICK_STARTERS = [
|
| 425 |
+
("๐ผ ืขืืกืง ืืืจืฉื + ืืงืื ืขืกืงื ืืืื",
|
| 426 |
+
"ืืืจืช ืืขืื ืืข\"ื ืฉืืืื 36,500 ืฉ\"ื ืขื ืคืจืืืงื ืืชืจ ืืื. ืืขืืจื ืื ืงืืืช."),
|
| 427 |
+
("๐งพ ืขืืกืง ืคืืืจ - ืฉืืขืืจ ืคืจืื",
|
| 428 |
+
"ืงืืืืชื 220 ืฉืงื ืืืืืจ ืขื ืฉืืขืืจ ืฉืืืื ืคืจืื, ืืืืื"),
|
| 429 |
+
("๐ฆ ืืฉืืื ืืช ืืก - ืืื ืคืจืืืื",
|
| 430 |
+
"ื ืืขื ืืื ืฉืืืื 2 * 350 ืฉ\"ื ืืืขืืฅ ืขืกืงื + 180 ืฉ\"ื ืืื ืช ืืฆืืช"),
|
| 431 |
+
]
|
| 432 |
+
|
| 433 |
+
DEMO_ISSUER = {
|
| 434 |
+
"name": "ืืื ืฉืงืืื",
|
| 435 |
+
"tax_id": "962569844",
|
| 436 |
+
"status": "authorized_dealer",
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
def _run_pipeline(raw_text: str, chat_history: list,
|
| 440 |
+
pending_parse: dict | None, answers: dict):
|
| 441 |
+
"""
|
| 442 |
+
Main pipeline step.
|
| 443 |
+
Returns: (chat_history, doc_html, recs_html, pending_parse, answers)
|
| 444 |
+
"""
|
| 445 |
+
_lazy_init()
|
| 446 |
+
|
| 447 |
+
# Step 1 โ parse
|
| 448 |
+
if pending_parse is None:
|
| 449 |
+
parse = model_parse(raw_text)
|
| 450 |
+
else:
|
| 451 |
+
parse = pending_parse
|
| 452 |
+
|
| 453 |
+
# Step 2 โ check for questions
|
| 454 |
+
questions = clarification_questions(parse, answers)
|
| 455 |
+
if questions:
|
| 456 |
+
q_text = "\n".join(f"โข {q}" for q in questions)
|
| 457 |
+
chat_history = chat_history + [
|
| 458 |
+
{"role": "assistant",
|
| 459 |
+
"content": f"ืืฉ ืื ืืื ืฉืืืืช ืืคื ื ืฉืืคืืง ืืช ืืืกืื:\n\n{q_text}"}
|
| 460 |
+
]
|
| 461 |
+
return chat_history, "", "", parse, answers
|
| 462 |
+
|
| 463 |
+
# Step 3 โ apply answers + complete
|
| 464 |
+
final_parse = apply_answers(parse, answers)
|
| 465 |
+
try:
|
| 466 |
+
completed = core.complete(DEMO_ISSUER, final_parse, _STATE["rng"])
|
| 467 |
+
except Exception as e:
|
| 468 |
+
return (chat_history + [{"role": "assistant",
|
| 469 |
+
"content": f"ืฉืืืื ืืขืืืื: {e}"}],
|
| 470 |
+
"", "", None, {})
|
| 471 |
+
|
| 472 |
+
# Step 4 โ render
|
| 473 |
+
doc_html = render_document(completed)
|
| 474 |
+
recs = recommend(raw_text)
|
| 475 |
+
recs_html = render_recommendations(recs)
|
| 476 |
+
|
| 477 |
+
chat_history = chat_history + [
|
| 478 |
+
{"role": "assistant",
|
| 479 |
+
"content": "โ
ืืืกืื ืืืคืง ืืืฆืืื! ืจืื ืืืื."}
|
| 480 |
+
]
|
| 481 |
+
return chat_history, doc_html, recs_html, None, {}
|
| 482 |
|
|
|
|
|
|
|
|
|
|
| 483 |
|
| 484 |
+
def _handle_answer(user_msg: str, chat_history: list,
|
| 485 |
+
pending_parse: dict | None, answers: dict,
|
| 486 |
+
raw_text: str):
|
| 487 |
+
"""Parse a free-text answer and route back through pipeline."""
|
| 488 |
+
a = dict(answers)
|
| 489 |
+
low = user_msg.strip()
|
| 490 |
|
| 491 |
+
# simple intent detection for clarification answers
|
| 492 |
+
if any(w in low for w in ["ืืฉืืื ืืช ืืก", "ืืก ืืงืืื", "ืืก/ืงืืื"]):
|
| 493 |
+
a["doc_type"] = low
|
| 494 |
+
elif "ืงืืื" in low:
|
| 495 |
+
a["doc_type"] = "receipt"
|
| 496 |
+
elif not a.get("client_name") and pending_parse and not pending_parse.get("client_name"):
|
| 497 |
+
a["client_name"] = user_msg.strip()
|
| 498 |
+
|
| 499 |
+
chat_history = chat_history + [{"role": "user", "content": user_msg}]
|
| 500 |
+
return _run_pipeline(raw_text, chat_history, pending_parse, a)
|
| 501 |
+
|
| 502 |
+
|
| 503 |
+
# โโ build UI โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 504 |
+
with gr.Blocks(css=CSS, title="Text2Receipt") as demo:
|
| 505 |
+
|
| 506 |
+
gr.HTML("""
|
| 507 |
+
<div dir="rtl" style="text-align:center;padding:24px 0 8px;">
|
| 508 |
+
<div style="font-size:36px;margin-bottom:6px;">๐งพ</div>
|
| 509 |
+
<div style="font-size:28px;font-weight:800;color:#0d9488;">Text2Receipt</div>
|
| 510 |
+
<div style="font-size:14px;color:#9e9e9e;margin-top:4px;">
|
| 511 |
+
ืืขืจืช ืืื ืกื ืืืคืฉืืช ืืขืืจืืช โ ืืกืื ืคืืกืงืื ืืฉืจืืื ืชืงืื
|
| 512 |
+
</div>
|
| 513 |
+
</div>""")
|
| 514 |
+
|
| 515 |
+
# state
|
| 516 |
+
st_parse = gr.State(None)
|
| 517 |
+
st_answers = gr.State({})
|
| 518 |
+
st_raw = gr.State("")
|
| 519 |
+
|
| 520 |
+
with gr.Row():
|
| 521 |
with gr.Column(scale=2):
|
| 522 |
+
note_input = gr.Textbox(
|
| 523 |
+
label="ืืขืจืช ืืื ืกื (ืขืืจืืช ืืืคืฉืืช)",
|
| 524 |
+
placeholder="ืืืืืื: ืงืืืืชื 1,200 ืฉ\"ื ืืืฉื ืืื ืขื ืืืขืืฅ ืขืกืงื",
|
| 525 |
+
lines=3, rtl=True,
|
| 526 |
+
)
|
| 527 |
with gr.Row():
|
| 528 |
+
submit_btn = gr.Button("โก ืืคืง ืืกืื", variant="primary")
|
| 529 |
+
clear_btn = gr.Button("๐ ื ืงื")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 530 |
|
| 531 |
+
gr.Markdown("**ืืืืืืืช ืืืืจืืช:**", rtl=True)
|
| 532 |
+
for label, example in QUICK_STARTERS:
|
| 533 |
+
gr.Button(label).click(
|
| 534 |
+
fn=lambda ex=example: ex,
|
| 535 |
+
outputs=note_input
|
| 536 |
+
)
|
| 537 |
+
|
| 538 |
+
with gr.Column(scale=3):
|
| 539 |
+
chatbot = gr.Chatbot(
|
| 540 |
+
label="ืกืืื ืืืืจื",
|
| 541 |
+
type="messages",
|
| 542 |
+
rtl=True,
|
| 543 |
+
height=220,
|
| 544 |
+
)
|
| 545 |
+
answer_input = gr.Textbox(
|
| 546 |
+
label="ืชืฉืืื ืืฉืืืช ืืกืืื",
|
| 547 |
+
placeholder="ืืงืื ืชืฉืืื ืืืืฅ Enter...",
|
| 548 |
+
visible=True, rtl=True,
|
| 549 |
+
)
|
| 550 |
+
|
| 551 |
+
doc_output = gr.HTML(label="ืืกืื ืคืืกืงืื")
|
| 552 |
+
recs_output = gr.HTML(label="ืงืืืืช ืืืืืช")
|
| 553 |
+
|
| 554 |
+
# โโ event wiring โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 555 |
+
def on_submit(raw, history, pending, answers):
|
| 556 |
+
history = history + [{"role": "user", "content": raw}]
|
| 557 |
+
return _run_pipeline(raw, history, None, {})
|
| 558 |
+
|
| 559 |
+
submit_btn.click(
|
| 560 |
+
fn=on_submit,
|
| 561 |
+
inputs=[note_input, chatbot, st_parse, st_answers],
|
| 562 |
+
outputs=[chatbot, doc_output, recs_output, st_parse, st_answers],
|
| 563 |
+
)
|
| 564 |
+
note_input.submit(
|
| 565 |
+
fn=on_submit,
|
| 566 |
+
inputs=[note_input, chatbot, st_parse, st_answers],
|
| 567 |
+
outputs=[chatbot, doc_output, recs_output, st_parse, st_answers],
|
| 568 |
+
)
|
| 569 |
+
answer_input.submit(
|
| 570 |
+
fn=_handle_answer,
|
| 571 |
+
inputs=[answer_input, chatbot, st_parse, st_answers, note_input],
|
| 572 |
+
outputs=[chatbot, doc_output, recs_output, st_parse, st_answers],
|
| 573 |
)
|
| 574 |
+
clear_btn.click(
|
| 575 |
+
fn=lambda: ([], "", "", None, {}, ""),
|
| 576 |
+
outputs=[chatbot, doc_output, recs_output, st_parse, st_answers, note_input],
|
|
|
|
| 577 |
)
|
| 578 |
|
| 579 |
+
if __name__ == "__main__":
|
| 580 |
+
demo.launch()
|