File size: 13,376 Bytes
3c3d49f
d6058e5
f509ff5
0217536
f509ff5
 
2ddede4
0217536
d6058e5
 
 
 
 
 
 
 
 
 
 
7be7203
d6058e5
 
 
 
7be7203
d6058e5
 
 
 
 
 
 
 
 
 
7be7203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6058e5
 
 
0217536
 
 
 
 
 
 
7be7203
0217536
 
d6058e5
0217536
 
 
 
d6058e5
 
 
0217536
7be7203
0217536
 
 
 
 
 
 
 
 
 
2ddede4
0217536
 
d6058e5
 
7be7203
 
 
 
d6058e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7be7203
d6058e5
 
 
 
 
 
 
7be7203
d6058e5
7be7203
d6058e5
 
 
 
 
 
 
 
 
7be7203
 
 
d6058e5
 
7be7203
 
 
 
 
 
d6058e5
7be7203
 
d6058e5
 
7be7203
 
 
d6058e5
7be7203
d6058e5
7be7203
d6058e5
7be7203
d6058e5
 
 
 
 
 
 
 
 
 
7be7203
d6058e5
 
 
 
7be7203
 
d6058e5
 
7be7203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6058e5
7be7203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6058e5
7be7203
 
 
 
 
 
 
 
 
d6058e5
7be7203
 
 
 
 
f509ff5
 
d6058e5
 
 
 
 
7be7203
 
 
 
 
 
 
d6058e5
7be7203
d6058e5
 
 
3c3d49f
f509ff5
 
7be7203
d6058e5
 
 
 
 
 
 
 
0217536
7be7203
d6058e5
 
7be7203
d6058e5
 
 
 
 
 
 
 
 
 
 
 
 
 
7be7203
f509ff5
d6058e5
 
 
f509ff5
d6058e5
 
 
 
 
f509ff5
 
 
7be7203
0217536
d6058e5
0217536
f509ff5
7be7203
d6058e5
 
0217536
 
 
 
d6058e5
f509ff5
0217536
f509ff5
d6058e5
 
 
 
 
f509ff5
7be7203
d6058e5
 
 
 
 
 
f509ff5
d6058e5
 
0217536
f509ff5
0217536
d6058e5
f509ff5
0217536
f509ff5
0217536
 
f509ff5
d6058e5
3c3d49f
 
 
1
2
3
4
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import json
import re
import tempfile
from typing import List, Dict, Any, Tuple

import gradio as gr
from transformers import pipeline

# ---------------------------
# Minimal sample (id, question, answer only)
# ---------------------------
SAMPLE_JSON_MIN = """[
  {
    "id": "ex-001",
    "question": "question",
    "answer": "answer"
  },
  {
    "id": "ex-002",
    "question": "question",
    "answer": "answer"
  },
  {
    "id": "ex-003",
    "question": "question",
    "answer": "answer"
  }
]"""

def download_minimal_sample_json():
    with tempfile.NamedTemporaryFile(delete=False, suffix=".json", mode="w", encoding="utf-8") as tmp:
        tmp.write(SAMPLE_JSON_MIN)
        tmp.flush()
        return tmp.name

# ---------------------------
# Helpers (formatting & menu-path)
# ---------------------------
def _normalize_ws(s: str) -> str:
    return re.sub(r"\s+", " ", (s or "").strip())

def _sentence_case(s: str) -> str:
    s = _normalize_ws(s)
    if not s:
        return s
    # single lowercase 'i' -> 'I'
    s = re.sub(r"\bi\b", "I", s)
    if s[0].islower():
        s = s[0].upper() + s[1:]
    if s[-1] not in ".!?":
        s += "."
    return s

def _join_path(section: str | None, option: str | None) -> str | None:
    section = (section or "").strip()
    option = (option or "").strip()
    if section and option:
        return f"{section} > {option}"
    if option:
        return option
    if section:
        return section
    return None

# ---------------------------
# Model: reward/quality (Transformers uyumlu)
# ---------------------------
MODEL_ID = "OpenAssistant/reward-model-deberta-v3-large-v2"

try:
    quality_pipe = pipeline(
        task="text-classification",
        model=MODEL_ID,
        tokenizer=MODEL_ID,
        function_to_apply="none"  # regression score
    )
    MODEL_READY = True
    LOAD_ERR = ""
except Exception as e:
    MODEL_READY = False
    LOAD_ERR = str(e)

# ---------------------------
# Scoring & labeling
# ---------------------------
def score_pair(question: str, answer: str) -> float:
    """Reward score (higher = better). If model not ready, use light heuristic."""
    if not MODEL_READY:
        base = 0.3
        if question.strip().endswith("?"):
            base += 0.1
        if len(answer.split()) >= 6:
            base += 0.2
        if answer.strip().endswith((".", "!", "?")):
            base += 0.1
        return base
    text = f"Human: {question}\nAssistant: {answer}"
    out = quality_pipe(text, truncation=True)[0]  # top_k=1 default
    return float(out["score"])

def label_mapper_from_distribution(scores: List[float]):
    """
    Scores may be negative; use distribution-based thresholds:
      low   : < 33rd percentile
      medium: 33–66
      high  : >= 66
    """
    if not scores:
        return lambda s: "medium"
    s_sorted = sorted(scores)
    def pct(p):
        if len(s_sorted) == 1:
            return s_sorted[0]
        idx = int(round((p/100) * (len(s_sorted)-1)))
        return s_sorted[idx]
    low_th = pct(33)
    high_th = pct(66)
    def mapper(s: float) -> str:
        if s >= high_th:
            return "high"
        elif s >= low_th:
            return "medium"
        else:
            return "low"
    return mapper

# ---------------------------
# Smart rewrite helpers
# ---------------------------
def _extract_module(item: Dict[str, Any], q_text: str) -> str | None:
    """
    Extract uppercase 3+ letter 'module-like' token from question/context, e.g., CUSTOMERS.
    """
    ctx = f"{item.get('context','')} {q_text}"
    m = re.search(r"\b([A-Z]{3,})\b", ctx)
    return m.group(1) if m else None

def _roles_from_answer(ans: str) -> List[str]:
    """
    Pull a role list from the answer; Title Case; drop empties.
    """
    parts = re.split(r",| and ", ans or "", flags=re.IGNORECASE)
    roles = []
    for p in parts:
        t = p.strip(" .")
        if not t:
            continue
        t = " ".join(w.capitalize() for w in t.split())
        roles.append(t)
    return [r for r in roles if r]

# ---------------------------
# Rewriter (WHO/WHERE/WHAT/HOW aware)
# ---------------------------
def improve_smart(item: Dict[str, Any]) -> Dict[str, Any]:
    """
    LLM-free safe rewrite:
    - WHO + MODULE: use roles pattern. (Only here!)
    - WHERE: produce menu-path sentence (e.g., Settings > Inventory Parameters).
    - WHAT: definition/purpose or 'allows searching by …' sentence.
    - HOW: short procedural sentence.
    - Else: grammar/format normalization.
    """
    q = _normalize_ws(item.get("question") or "")
    a = _normalize_ws(item.get("answer") or "")
    meta = item.get("metadata") or {}
    qtype = (meta.get("question_type") or "").lower()
    orig_q = _normalize_ws(item.get("original_question") or "")
    orig_a = _normalize_ws(item.get("original_answer") or "")
    base = orig_a or a  # prefer original to keep semantics

    module = _extract_module(item, q)  # e.g., CUSTOMERS

    # -- WHO: ONLY here we use roles pattern
    if qtype == "who" and module:
        roles = _roles_from_answer(base)
        new_q = f"Which roles are authorized to access the {module} module in DealerTIQ?"
        if roles:
            if len(roles) == 1:
                roles_str = roles[0]
            elif len(roles) == 2:
                roles_str = " and ".join(roles)
            else:
                roles_str = ", ".join(roles[:-1]) + f", and {roles[-1]}"
            new_a = f"Authorized roles include {roles_str}."
        else:
            new_a = _sentence_case(base) if base else _sentence_case(a)

        if item.get("context"):
            item["context"] = f"DealerTIQ β€” {module} module"

        item["question"] = _sentence_case(new_q[:-1] + "?")
        item["answer"] = _sentence_case(new_a)
        return item

    # -- WHERE: menu path sentence
    if qtype == "where":
        text = base or a
        # "under the Settings section"
        m_sec = re.search(r"under the\s+([A-Za-z ]+?)\s+section", text or "", flags=re.IGNORECASE)
        section = m_sec.group(1).strip().title() if m_sec else None
        # quoted option: "Inventory Parameters"
        quotes = re.findall(r'"([^"]+)"', text or "")
        option = quotes[0].strip() if quotes else None

        path = _join_path(section, option)
        target = option or (module and f"{module} module") or "page"
        new_q = f"Where is the {target} located in DealerTIQ?"
        new_a = f"It is located under {path} in the left navigation menu." if path else (text or "It is available in the left navigation menu.")

        item["question"] = _sentence_case(new_q[:-1] + "?")
        item["answer"] = _sentence_case(new_a)
        return item

    # -- WHAT: definition/purpose or 'allows searching by …'
    if qtype == "what":
        text = base or a
        if re.search(r"allows\s+search(ing)?\s+by", text or "", flags=re.IGNORECASE):
            m = re.search(r"such as\s+(.+)", text or "", flags=re.IGNORECASE)
            if m:
                feats = m.group(1).strip().rstrip(".")
                new_q = f"What can you search for in {module or 'this module'}?"
                new_a = f"It allows searching by criteria such as {feats}."
            else:
                new_q = orig_q or q or "What can you search for in this module?"
                new_a = text or "It allows searching by multiple criteria."
        else:
            if re.search(r"\b(configure|configuration|settings)\b", text or "", flags=re.IGNORECASE):
                target = module or "Inventory Parameters"
                new_q = f"What is configured in the {target}?"
                new_a = text or "It configures related settings and rules."
            else:
                new_q = orig_q or q or "What is the purpose of this module?"
                new_a = text or "It provides the core functionality for this area."
        item["question"] = _sentence_case(new_q[:-1] + "?")
        item["answer"] = _sentence_case(new_a)
        return item

    # -- HOW: short procedure
    if qtype == "how":
        text = base or a
        quotes = re.findall(r'"([^"]+)"', text or "")  # "Add Channel", "Inventory Parameters"
        m_sec = re.search(r"under the\s+([A-Za-z ]+?)\s+section", text or "", flags=re.IGNORECASE)
        section = m_sec.group(1).strip().title() if m_sec else None
        path = _join_path(section, quotes[0] if quotes else None)

        new_q = orig_q or q or f"How do I perform this action in {module or 'the module'}?"
        steps = _normalize_ws(text or "")
        steps = re.sub(r"\bclick on\b", "select", steps, flags=re.IGNORECASE)
        if path and "left navigation" not in steps.lower():
            steps = f"Go to {path} in the left navigation menu, then {steps[0].lower() + steps[1:]}" if steps else f"Go to {path} in the left navigation menu."

        item["question"] = _sentence_case(new_q[:-1] + "?") if not new_q.endswith("?") else _sentence_case(new_q)
        item["answer"] = _sentence_case(steps or "Follow the on-screen instructions to complete the action.")
        return item

    # -- Fallback: grammar/format normalization only
    if q and not q.endswith("?"):
        q += "?"
    item["question"] = _sentence_case(q)
    item["answer"] = _sentence_case(base or a)
    return item

# ---------------------------
# Pipeline
# ---------------------------
def process_json(file) -> Tuple[List[Dict[str, Any]], str, str, str]:
    """
    Input: JSON (list or single object)
    Steps:
      1) First scoring pass for all items
      2) Label via distribution thresholds (low/medium/high)
      3) Auto-rewrite items labeled 'low' (improve_smart)
      4) Rescore & write quality_before / quality_after
    Output:
      - Summary (Dataframe)
      - Preview JSON (first 50)
      - Download JSON path
      - Warn/Info
    """
    data = json.load(open(file.name))
    items: List[Dict[str, Any]] = data if isinstance(data, list) else [data]

    # 1) First scoring pass
    first = []
    scores = []
    for raw in items:
        it = dict(raw)
        s = score_pair(it.get("question",""), it.get("answer",""))
        it["quality_before"] = {"score": round(s, 3)}
        first.append(it)
        scores.append(s)

    # 2) Dynamic label function
    to_label = label_mapper_from_distribution(scores)

    # 3) Label, rewrite if 'low', then rescore
    processed = []
    for it in first:
        base_label = to_label(it["quality_before"]["score"])
        it["quality_before"]["label"] = base_label

        if base_label == "low":
            it = improve_smart(it)
            s2 = score_pair(it.get("question",""), it.get("answer",""))
            it["quality_after"] = {
                "score": round(s2, 3),
                "label": to_label(s2)
            }
        processed.append(it)

    # Summary table
    summary = []
    for idx, it in enumerate(processed):
        qb = it.get("quality_before", {})
        qa = it.get("quality_after")
        summary.append({
            "id": it.get("id", idx),
            "before_label": qb.get("label"),
            "before_score": qb.get("score"),
            "after_label": qa.get("label") if qa else None,
            "after_score": qa.get("score") if qa else None,
            "question_preview": (it.get("question") or "")[:120]
        })

    # Downloadable JSON
    tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".json", mode="w", encoding="utf-8")
    json.dump(processed, tmp, indent=2, ensure_ascii=False)
    tmp.flush(); tmp.close()

    # Preview
    preview = json.dumps(processed[:50], indent=2, ensure_ascii=False)
    if len(processed) > 50:
        preview += "\n\n// NOTE: Showing first 50 items. Download full file below."

    warn = ""
    if not MODEL_READY:
        warn = f"Warning: model '{MODEL_ID}' could not be loaded; heuristic scoring used. Error: {LOAD_ERR}"

    return summary, preview, tmp.name, warn

# ---------------------------
# UI
# ---------------------------
with gr.Blocks(title="Q&A Quality Upgrader", theme=gr.themes.Soft()) as demo:
    gr.Markdown("## Q&A Quality Upgrader\nUpload your JSON. Low-quality items will be auto-rewritten and rescored.")

    # ---- Minimal sample accordion (show + download) ----
    with gr.Accordion("Minimal sample JSON (only id, question, answer)", open=False):
        gr.Markdown("Upload a JSON **array of objects** with the following schema:")
        gr.Code(value=SAMPLE_JSON_MIN, language="json", lines=18, label="Minimal JSON example")
        sample_btn = gr.Button("Download minimal sample.json")
        sample_file = gr.File(label="minimal-sample.json")
        sample_btn.click(fn=download_minimal_sample_json, outputs=sample_file)

    # ---- Upload & Run ----
    inp = gr.File(file_types=[".json"], label="Upload JSON (list of objects)")
    run = gr.Button("Run")

    with gr.Tab("Summary"):
        tbl = gr.Dataframe(headers=["id","before_label","before_score","after_label","after_score","question_preview"])
    with gr.Tab("Preview JSON"):
        code = gr.Code(language="json", lines=34, label="Preview (first 50 items)")
    with gr.Tab("Download"):
        dfile = gr.File(label="Download full JSON")
    warnbox = gr.Markdown("")

    run.click(process_json, inputs=[inp], outputs=[tbl, code, dfile, warnbox])

if __name__ == "__main__":
    demo.launch()