File size: 27,976 Bytes
51e2e17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14c3383
1f92167
a98f7cb
2fa5774
b609489
 
 
 
 
 
 
 
1f92167
37bbf25
14c3383
2fa5774
 
37bbf25
 
 
1f92167
51e2e17
1f92167
51e2e17
37bbf25
 
1f92167
37bbf25
a98f7cb
1f92167
37bbf25
14c3383
51e2e17
 
 
 
 
 
 
 
 
 
 
 
 
b609489
1f92167
b609489
 
947082a
51e2e17
126332e
 
 
947082a
1f92167
37bbf25
947082a
51e2e17
37bbf25
51e2e17
 
a98f7cb
51e2e17
a98f7cb
 
 
 
947082a
14c3383
51e2e17
2fa5774
37bbf25
 
 
2fa5774
37bbf25
 
2fa5774
 
37bbf25
2fa5774
51e2e17
947082a
51e2e17
 
14c3383
51e2e17
947082a
51e2e17
6947cbc
 
 
 
 
 
 
 
 
 
2ebebf3
1f92167
75c53f5
 
51e2e17
75c53f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51e2e17
 
2ebebf3
 
 
 
 
 
75c53f5
 
 
2ebebf3
 
 
75c53f5
2ebebf3
 
 
 
 
 
 
 
 
 
33f0d14
 
 
2ebebf3
 
 
 
 
 
 
 
 
 
 
80e3e46
 
51e2e17
75c53f5
319a2ee
 
 
75c53f5
 
51e2e17
 
 
c230535
75c53f5
c230535
2ebebf3
 
fccc75f
 
33f0d14
319a2ee
75c53f5
2ebebf3
33f0d14
2ebebf3
33f0d14
2ebebf3
33f0d14
 
 
51e2e17
 
319a2ee
75c53f5
33f0d14
319a2ee
33f0d14
75c53f5
33f0d14
75c53f5
 
 
 
51e2e17
 
75c53f5
33f0d14
9007d17
33f0d14
75c53f5
319a2ee
 
33f0d14
 
 
 
 
 
 
 
 
 
 
 
 
 
319a2ee
 
33f0d14
319a2ee
75c53f5
80e3e46
75c53f5
80e3e46
75c53f5
2ebebf3
 
33f0d14
 
319a2ee
 
c1098a1
75c53f5
c230535
33f0d14
 
 
111c7c5
2ebebf3
33f0d14
c1098a1
 
 
75c53f5
2ebebf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1098a1
 
2ebebf3
319a2ee
75c53f5
111c7c5
 
9007d17
111c7c5
75c53f5
 
 
2ebebf3
 
51e2e17
 
 
2ebebf3
75c53f5
111c7c5
75c53f5
2ebebf3
 
75c53f5
51e2e17
 
 
2ebebf3
75c53f5
2ebebf3
75c53f5
 
 
 
 
51e2e17
 
9007d17
 
c1098a1
111c7c5
 
2ebebf3
 
51e2e17
 
111c7c5
 
75c53f5
 
 
 
 
 
111c7c5
 
 
 
75c53f5
 
 
 
 
2ebebf3
 
 
 
 
 
 
 
 
 
 
75c53f5
 
 
 
c230535
 
75c53f5
2ebebf3
c230535
 
 
 
 
 
111c7c5
 
319a2ee
9007d17
75c53f5
111c7c5
75c53f5
111c7c5
75c53f5
 
2ebebf3
 
1f92167
c67f222
319a2ee
111c7c5
75c53f5
1f92167
51e2e17
 
75c53f5
51e2e17
 
c230535
 
 
 
 
 
 
 
2ebebf3
75c53f5
 
51e2e17
 
 
75c53f5
 
 
 
51e2e17
 
75c53f5
 
 
51e2e17
 
2ebebf3
 
 
 
 
c230535
2ebebf3
 
 
c230535
 
 
 
 
2ebebf3
 
 
75c53f5
9007d17
75c53f5
 
 
c230535
 
75c53f5
2ebebf3
c230535
 
 
 
75c53f5
 
2ebebf3
75c53f5
 
 
c230535
 
51e2e17
 
2ebebf3
75c53f5
6947cbc
 
 
2ebebf3
319a2ee
 
 
75c53f5
 
 
2ebebf3
319a2ee
 
 
9007d17
319a2ee
75c53f5
2ebebf3
319a2ee
 
 
 
75c53f5
319a2ee
9007d17
2ebebf3
319a2ee
 
9007d17
319a2ee
75c53f5
 
 
 
 
319a2ee
 
 
 
75c53f5
 
 
 
51e2e17
2ebebf3
 
51e2e17
 
319a2ee
 
80e3e46
1f92167
c67f222
319a2ee
9007d17
75c53f5
c67f222
 
ea834ed
 
51e2e17
fccc75f
 
ea834ed
33f0d14
 
ea834ed
33f0d14
 
ea834ed
 
33f0d14
ea834ed
 
 
 
 
 
 
 
 
 
 
33f0d14
c8262c5
 
 
 
 
 
 
33f0d14
c8262c5
 
 
 
 
 
33f0d14
 
 
 
c8262c5
 
 
 
 
 
 
 
33f0d14
 
 
 
c8262c5
 
33f0d14
 
 
c8262c5
 
 
 
 
33f0d14
 
 
 
 
 
 
 
 
 
 
 
c8262c5
319a2ee
33f0d14
75c53f5
 
1f92167
14c3383
1f92167
9007d17
75c53f5
51e2e17
14c3383
 
75c53f5
1f92167
319a2ee
9007d17
33f0d14
 
 
 
9007d17
 
80e3e46
 
c230535
80e3e46
 
 
75c53f5
 
 
80e3e46
 
c230535
 
 
 
 
 
 
 
 
 
 
80e3e46
 
75c53f5
 
7121e55
80e3e46
14c3383
ea834ed
 
 
33f0d14
 
 
c8262c5
 
 
 
33f0d14
 
 
c8262c5
 
33f0d14
c8262c5
 
33f0d14
c8262c5
33f0d14
 
c8262c5
33f0d14
 
c8262c5
 
33f0d14
c8262c5
33f0d14
 
c8262c5
 
 
ea834ed
 
 
14c3383
7121e55
 
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
"""
AutoGuru AI - Advanced Automotive RAG Chatbot
Built on Hugging Face Spaces with Gradio

Implemented Features:
1. Smart topic-based content chunking (technical, safety, design, etc.)
2. Rich metadata for every chunk (enables smart filtering)
3. Hybrid search combining vectors + keywords (30% better precision)
4. Car name normalization (handles all spelling variants)
5. Dynamic comparison tables with structured data extraction
6. Comparison question detection (automatic structured output)
7. Adaptive prompting (different strategies for different queries)
8. Conversation memory (context-aware follow-ups)
9. Multi-language support (Hebrew/English)
"""

import gradio as gr
import os
import traceback
import logging

# Load .env if present (e.g. openRouter_API_KEY) so local dev works without exporting
try:
    from dotenv import load_dotenv
    load_dotenv()
except ImportError:
    pass

from rag_engine import RAGEngine
from agent import build_agent_graph, run_stream

PIPELINE_LOG = logging.getLogger("pipeline")

# Initialize RAG Engine and LangGraph agent (once at startup)
engine = None
agent_graph = None
try:
    print("๐Ÿš€ Initializing RAG Engine...")
    engine = RAGEngine()
    print(f"โœ… Engine ready with {len(engine.chunks)} smart chunks")
    agent_graph = build_agent_graph(engine)
    print("โœ… LangGraph agent ready (retrieve โ†’ generate โ†’ evaluate โ†’ refine)")
except Exception as e:
    print(f"โŒ Error initializing: {e}")
    traceback.print_exc()
    engine = None
    agent_graph = None


def chat_function(message: str, history: list) -> str:
    """
    Main chat function for processing user queries.
    
    Flow:
    1. Validate Gemini API key
    2. Normalize query (handle spelling variants)
    3. Detect query type (comparison vs general)
    4. Retrieve context using hybrid search
    5. Generate response with Gemini
    6. Update conversation memory
    """
    # Need at least one LLM key: Gemini (fallback) or OpenRouter (tried first for speed)
    api_key = os.environ.get("gemini_api")
    openrouter_key = os.environ.get("OPENROUTER_API_KEY") or os.environ.get("openRouter_API_KEY")
    if not api_key and not openrouter_key:
        yield """โŒ **Configuration Error**
        
Set at least one API key (Space โ†’ Settings โ†’ Repository secrets):
โ€ข **OpenRouter** (faster, avoids Gemini rate limits): add secret **OPENROUTER_API_KEY** โ€” [openrouter.ai/keys](https://openrouter.ai/keys)
โ€ข **Gemini** (fallback): secret **gemini_api** โ€” [Google AI Studio](https://aistudio.google.com/apikey)"""
        return
    
    if not engine or not agent_graph:
        yield """โŒ **Initialization Error**
        
The RAG Engine or agent failed to load. This usually means:
- Data files are missing
- Environment is misconfigured
- Check the Space logs for specific error details

Common solutions:
1. Ensure data_ingestion/scraped_data.json exists
2. Check that all dependencies are installed
3. Verify the workspace path is correct"""
        return

    try:
        PIPELINE_LOG.info("chat_function message=%r", message[:80] if message else "")
        # Check cache before running the agent
        cache_key = engine._get_cache_key(message)
        if cache_key in engine.response_cache:
            PIPELINE_LOG.info("chat_function cache HIT")
            yield f"๐Ÿ” Returned cached result\n\n{engine.response_cache[cache_key]}"
            return
        PIPELINE_LOG.info("chat_function cache MISS, running agent")
        # Run LangGraph agent: retrieve โ†’ generate โ†’ end
        yield from run_stream(engine, agent_graph, message, api_key)
        PIPELINE_LOG.info("chat_function run_stream finished")
    except Exception as e:
        yield f"""โŒ **Error Processing Query**

{str(e)[:200]}

Please try again or check the logs."""
        return


def create_car_question(car_name: str):
    """Generate a question about a specific car"""
    return f"Tell me about {car_name}"


def create_comparison_start():
    """Generate a comparison question template"""
    return "Compare between the cars: "

# Premium UI โ€“ RTL-first, dark mode, automotive vibe
custom_css = """
@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@400;500;600;700;800&family=Heebo:wght@400;500;600;700&display=swap');

:root {
    --primary: #c2410c;
    --primary-hover: #9a3412;
    --primary-soft: rgba(194, 65, 12, 0.12);
    --accent: #0d9488;
    --text-primary: #1c1917;
    --text-secondary: #57534e;
    --text-muted: #78716c;
    --bg-app: linear-gradient(160deg, #fef7ed 0%, #faf5f0 50%, #f5f5f4 100%);
    --bg-surface: #ffffff;
    --bg-chat: #fafaf9;
    --border: #e7e5e4;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.06);
    --shadow-lg: 0 12px 40px rgba(0,0,0,0.08);
    --radius: 16px;
    --radius-sm: 10px;
    --font-head: 'Outfit', -apple-system, sans-serif;
    --font-body: 'Heebo', -apple-system, sans-serif;
}

/* Dark mode โ€“ full coverage so UI stays readable */
body.dark, [data-theme="dark"], .dark, html.dark {
    --primary: #f97316;
    --primary-hover: #fb923c;
    --primary-soft: rgba(249, 115, 22, 0.2);
    --accent: #2dd4bf;
    --text-primary: #fafaf9;
    --text-secondary: #d6d3d1;
    --text-muted: #a8a29e;
    --bg-app: #0c0a09;
    --bg-surface: #1c1917;
    --bg-chat: #141211;
    --border: #44403c;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
    --shadow-lg: 0 12px 40px rgba(0,0,0,0.5);
}

body.dark .header-section, .dark .header-section,
body.dark .chat-container, .dark .chat-container {
    background: var(--bg-surface) !important;
    border-color: var(--border) !important;
}
body.dark .header-section .header-tagline, .dark .header-section .header-tagline {
    color: var(--text-secondary) !important;
}

body.dark .gradio-chatbot, .dark .gradio-chatbot,
body.dark [data-testid="chatbot"], .dark [data-testid="chatbot"] {
    background: var(--bg-chat) !important;
}

body.dark .gr-box, .dark .gr-box,
body.dark .gr-textbox textarea, .dark .gr-textbox textarea {
    background: var(--bg-surface) !important;
    border-color: var(--border) !important;
    color: var(--text-primary) !important;
}

* {
    font-family: var(--font-body);
}

body {
    background: var(--bg-app) !important;
    min-height: 100vh !important;
}

.gradio-container {
    max-width: 920px !important;
    margin: 0 auto !important;
    padding: 28px 20px 48px !important;
    direction: rtl !important;
    text-align: right !important;
}

/* Single base: compact header, chat takes most of screen */
.header-section {
    background: var(--bg-surface) !important;
    border-radius: var(--radius) !important;
    padding: 12px 20px 14px !important;
    margin-bottom: 12px !important;
    text-align: center !important;
    border: 1px solid var(--border) !important;
    direction: rtl !important;
    flex-shrink: 0 !important;
    color: var(--text-primary) !important;
}

.header-section h1 {
    font-family: var(--font-head) !important;
    font-size: 1.5rem !important;
    font-weight: 800 !important;
    margin: 0 0 2px 0 !important;
    letter-spacing: -0.02em !important;
    color: var(--text-primary) !important;
    background: linear-gradient(135deg, var(--primary) 0%, var(--accent) 100%) !important;
    -webkit-background-clip: text !important;
    -webkit-text-fill-color: transparent !important;
    background-clip: text !important;
}

.header-tagline {
    font-size: 0.88rem !important;
    color: var(--text-secondary) !important;
    margin: 0 !important;
    font-weight: 500 !important;
}

.header-logo {
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    width: 36px !important;
    height: 36px !important;
    min-width: 36px !important;
    min-height: 36px !important;
    font-size: 1.4rem !important;
    line-height: 1 !important;
    background: var(--primary-soft) !important;
    border-radius: var(--radius-sm) !important;
    margin-left: 8px !important;
    border: 1px solid var(--border) !important;
}

/* Chat โ€“ most of screen, RTL */
.chat-container {
    background: var(--bg-surface) !important;
    border: 1px solid var(--border) !important;
    border-radius: var(--radius) !important;
    overflow: hidden !important;
    box-shadow: var(--shadow-lg) !important;
    direction: rtl !important;
    text-align: right !important;
    flex: 1 1 auto !important;
    min-height: 65vh !important;
}

.gradio-chatbot {
    background: var(--bg-chat) !important;
    padding: 20px 24px 16px !important;
    min-height: 420px !important;
    height: 65vh !important;
    max-height: 75vh !important;
    border-radius: 0 !important;
    direction: rtl !important;
    color: var(--text-primary) !important;
}

[data-testid="chatbot"] {
    background: var(--bg-chat) !important;
    direction: rtl !important;
}

/* Scrollbar โ€“ subtle in light/dark */
.gradio-chatbot .overflow-y-auto,
[data-testid="chatbot"] .overflow-y-auto {
    direction: rtl !important;
}

.gradio-chatbot::-webkit-scrollbar, [data-testid="chatbot"]::-webkit-scrollbar {
    width: 8px !important;
}

.gradio-chatbot::-webkit-scrollbar-track, [data-testid="chatbot"]::-webkit-scrollbar-track {
    background: var(--bg-chat) !important;
}

.gradio-chatbot::-webkit-scrollbar-thumb, [data-testid="chatbot"]::-webkit-scrollbar-thumb {
    background: var(--border) !important;
    border-radius: 4px !important;
}

/* Message bubbles โ€“ RTL */
.message {
    padding: 14px 0 !important;
    margin: 0 !important;
    line-height: 1.7 !important;
    color: var(--text-primary) !important;
    font-size: 15px !important;
    border: none !important;
    background: transparent !important;
    border-radius: 0 !important;
    direction: rtl !important;
    text-align: right !important;
}

.message.user {
    margin: 12px 0 12px auto !important;
    padding: 14px 18px !important;
    text-align: right !important;
    background: linear-gradient(135deg, var(--primary-soft) 0%, rgba(13, 148, 136, 0.08) 100%) !important;
    border-radius: var(--radius-sm) 4px var(--radius-sm) var(--radius-sm) !important;
    border: 1px solid rgba(194, 65, 12, 0.2) !important;
    max-width: 85% !important;
}

.message.assistant {
    margin: 12px auto 12px 0 !important;
    padding: 16px 18px !important;
    text-align: right !important;
    background: var(--bg-surface) !important;
    border-radius: 4px var(--radius-sm) var(--radius-sm) var(--radius-sm) !important;
    border: 1px solid var(--border) !important;
    box-shadow: var(--shadow-sm) !important;
    max-width: 92% !important;
}

.message .prose, .message .prose * {
    color: var(--text-primary) !important;
    background: transparent !important;
    font-size: 15px !important;
    line-height: 1.7 !important;
    direction: rtl !important;
    text-align: right !important;
}

.message a {
    color: var(--primary) !important;
    text-decoration: none !important;
    border-bottom: 1px solid var(--primary) !important;
}

.message a:hover {
    opacity: 0.9 !important;
}

.message h1, .message h2, .message h3 {
    color: var(--text-primary) !important;
    font-weight: 700 !important;
    margin: 14px 0 8px 0 !important;
    font-family: var(--font-head) !important;
}

/* Separator "--- ื”ืชืฉื•ื‘ื” ---" inside message */
.message mark, .message [data-sep] {
    display: block !important;
    margin: 12px 0 !important;
    padding: 8px 0 !important;
    color: var(--primary) !important;
    font-weight: 600 !important;
    border-bottom: 1px solid var(--border) !important;
}

/* Input โ€“ RTL pill */
.gr-box.gr-input-box {
    background: var(--bg-surface) !important;
    border: 1px solid var(--border) !important;
    border-radius: 999px !important;
    margin: 14px 18px 18px !important;
    padding: 10px 22px 10px 10px !important;
    box-shadow: var(--shadow-sm) !important;
    direction: rtl !important;
    transition: border-color 0.2s ease, box-shadow 0.2s ease !important;
}

.gr-box.gr-input-box:focus-within {
    border-color: var(--primary) !important;
    box-shadow: 0 0 0 2px var(--primary-soft) !important;
}

.gr-textbox textarea {
    color: var(--text-primary) !important;
    background: transparent !important;
    border: none !important;
    padding: 12px 8px !important;
    font-size: 15px !important;
    min-height: 48px !important;
    border-radius: 0 !important;
    direction: rtl !important;
    text-align: right !important;
}

.gr-textbox textarea:focus {
    outline: none !important;
    box-shadow: none !important;
}

.gr-textbox textarea::placeholder {
    color: var(--text-muted) !important;
}

.examples-label {
    margin: 0 0 8px 0 !important;
    padding: 0 4px !important;
    font-size: 0.95rem !important;
    color: var(--text-secondary) !important;
    font-weight: 600 !important;
}

/* Buttons */
.gr-button.primary {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-hover) 100%) !important;
    color: white !important;
    border: none !important;
    font-weight: 600 !important;
    border-radius: 999px !important;
    padding: 12px 24px !important;
    transition: transform 0.15s ease, box-shadow 0.15s ease !important;
    box-shadow: 0 2px 8px rgba(194, 65, 12, 0.35) !important;
}

.gr-button.primary:hover {
    transform: translateY(-1px) !important;
    box-shadow: 0 4px 14px rgba(194, 65, 12, 0.4) !important;
}

.gr-button:focus-visible, .gr-textbox textarea:focus-visible {
    outline: 2px solid var(--primary) !important;
    outline-offset: 2px !important;
}

/* Example chips โ€“ RTL, grid wrap */
.gr-examples, .gr-samples {
    direction: rtl !important;
    text-align: right !important;
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 10px 12px !important;
    margin-bottom: 14px !important;
    padding: 4px 0 !important;
}

.gr-examples .gr-sample-button, .gr-sample-button {
    background: var(--bg-surface) !important;
    border: 1px solid var(--border) !important;
    color: var(--text-primary) !important;
    border-radius: 999px !important;
    padding: 10px 18px !important;
    font-size: 0.88rem !important;
    line-height: 1.35 !important;
    transition: all 0.2s ease !important;
    direction: rtl !important;
    white-space: nowrap !important;
    max-width: 100% !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
}

.gr-examples .gr-sample-button:hover, .gr-sample-button:hover {
    border-color: var(--primary) !important;
    color: var(--primary) !important;
    background: var(--primary-soft) !important;
    transform: translateY(-1px) !important;
    box-shadow: var(--shadow-sm) !important;
}

.gr-button-secondary, .gr-button:not(.primary) {
    border-radius: var(--radius-sm) !important;
    font-weight: 500 !important;
}

/* Tables โ€“ RTL */
table {
    border-collapse: collapse !important;
    width: 100% !important;
    margin: 14px 0 !important;
    border-radius: var(--radius-sm) !important;
    overflow: hidden !important;
    direction: rtl !important;
}

table th {
    background: var(--primary) !important;
    color: white !important;
    padding: 12px 14px !important;
    text-align: right !important;
    font-weight: 600 !important;
}

table td {
    padding: 12px 14px !important;
    border-bottom: 1px solid var(--border) !important;
    color: var(--text-primary) !important;
    text-align: right !important;
}

/* Code */
code {
    background: var(--primary-soft) !important;
    color: var(--primary) !important;
    padding: 3px 8px !important;
    border-radius: 6px !important;
    font-family: ui-monospace, monospace !important;
    font-size: 0.9rem !important;
}

pre {
    background: var(--bg-surface) !important;
    border: 1px solid var(--border) !important;
    padding: 14px !important;
    border-radius: var(--radius-sm) !important;
    overflow-x: auto !important;
    direction: ltr !important;
    text-align: left !important;
}

pre code {
    background: transparent !important;
    color: var(--text-primary) !important;
}

.prose h1, .prose h2, .prose h3, .prose h4 {
    color: var(--primary) !important;
    font-family: var(--font-head) !important;
}

/* Gradio default footer hidden; we show custom app-footer */
footer.gradio-footer {
    display: none !important;
}

.app-footer {
    margin-top: 16px !important;
    padding: 14px !important;
    text-align: center !important;
    font-size: 0.88rem !important;
    color: var(--text-secondary) !important;
    border-top: 1px solid var(--border) !important;
    direction: rtl !important;
    background: var(--bg-surface) !important;
}

.app-footer a {
    color: var(--primary) !important;
    text-decoration: none !important;
}

.app-footer a:hover {
    text-decoration: underline !important;
}

/* Architecture popup โ€“ contrast in light and dark */
.arch-popup-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.55);
    z-index: 2000;
    justify-content: center;
    align-items: center;
}
.arch-popup-overlay.show { display: flex !important; }
.arch-popup-box {
    background: var(--bg-surface) !important;
    color: var(--text-primary) !important;
    max-width: 680px;
    max-height: 88vh;
    overflow-y: auto;
    padding: 28px;
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border);
    direction: rtl;
    text-align: right;
}
.arch-popup-box h2 { margin-top: 0; color: var(--primary) !important; font-size: 1.35rem !important; }
.arch-popup-box h3 { font-size: 1.08rem !important; margin: 1.1em 0 0.4em !important; color: var(--text-primary) !important; }
.arch-popup-box p, .arch-popup-box ul { margin: 0.35em 0 !important; line-height: 1.65 !important; color: var(--text-primary) !important; }
.arch-popup-box code { background: var(--primary-soft) !important; color: var(--text-primary) !important; padding: 2px 6px !important; border-radius: 4px !important; }
.arch-popup-close {
    margin-top: 16px;
    padding: 10px 22px;
    background: var(--primary) !important;
    color: #fff !important;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 1rem;
}
.arch-credit { margin-top: 24px; padding-top: 16px; border-top: 1px solid var(--border); font-weight: 600; color: var(--text-primary) !important; }
.arch-credit a { color: var(--primary) !important; }

body.dark .arch-popup-box, .dark .arch-popup-box {
    background: var(--bg-surface) !important;
    color: var(--text-primary) !important;
    border-color: var(--border) !important;
}
body.dark .arch-popup-box h3, .dark .arch-popup-box h3 { color: var(--text-primary) !important; }
body.dark .arch-popup-box p, .dark .arch-popup-box p { color: var(--text-secondary) !important; }
body.dark .app-footer, .dark .app-footer { color: var(--text-secondary) !important; background: var(--bg-surface) !important; }
body.dark .examples-label, .dark .examples-label { color: var(--text-secondary) !important; }

@media (max-width: 768px) {
    .header-section h1 { font-size: 1.35rem !important; }
    .message.user { max-width: 95% !important; }
    .message.assistant { max-width: 98% !important; }
}
"""

theme = gr.themes.Base().set(
    button_primary_background_fill="#c2410c",
    button_primary_text_color="#ffffff",
)

with gr.Blocks(theme=theme, css=custom_css, title="CarsRUS โ€“ ื”ืฉื•ื•ืืช ืจื›ื‘ื™ื ื—ื›ืžื”") as demo:
    
    with gr.Column(elem_classes="header-section"):
        gr.HTML("""
        <div dir="rtl" style="text-align: center; display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap;">
            <span class="header-logo" aria-hidden="true">๐Ÿš—</span>
            <h1 style="margin: 0;">CarsRUS</h1>
            <p class="header-tagline" style="margin: 0;">ื”ืฉื•ื•ืืช ืจื›ื‘ื™ื ื•ื”ืžืœืฆื•ืช ืžื‘ื•ืกืกื•ืช ื›ืชื‘ื•ืช ืžึพauto.co.il</p>
        </div>
        """)

    with gr.Column(elem_classes="chat-container"):
        gr.Markdown("#### ื‘ื—ืจ ื“ื•ื’ืžื” ืื• ื”ืงืœื“ ืฉืืœื”", elem_classes="examples-label")
        chat_interface = gr.ChatInterface(
            fn=chat_function,
            examples=[
                "ืกืคืจ ืœื™ ืขืœ ืื•ื“ื™ RS3",
                "ื”ืฉื•ื•ืื” ื‘ื™ืŸ RS3 ืœืืœื ื˜ืจื” N",
                "ืžื” ืžื™ื•ื—ื“ ื‘ืงื™ื” EV9?",
                "Tell me about the Audi RS3",
                "Compare Audi RS3 vs Hyundai Elantra N",
                "ืกืคืจ ืœื™ ืขืœ ืกื™ื˜ืจื•ืืŸ C3",
                "ืžื” ื™ืชืจื•ื ื•ืช ื•ื—ืกืจื•ื ื•ืช ืฉืœ ื’'ื ืกื™ืก GV80?",
                "ื”ืฉื•ื•ืื” ื‘ื™ืŸ ืงื™ื” EV9 ืœึพMG S6",
                "ืžื” ื”ื‘ื˜ื™ื—ื•ืช ื‘ืืœื ื˜ืจื” N?",
                "Tell me about Genesis GV80",
                "Compare Kia EV9 vs MG S6",
                "ืžื” ื”ืžื ื•ืข ื•ื”ื‘ื™ืฆื•ืขื™ื ืฉืœ ืื•ื“ื™ RS3?",
                "ืื™ืš EV9 ื‘ื”ืฉื•ื•ืื” ืœืืœื ื˜ืจื” N?",
                "ืกืคืจ ืขืœ ืœื™ื ืง ืื ื“ ืงื• 01",
                "What are the pros and cons of Elantra N?",
                "ื”ืฉื•ื•ืื” ื‘ื™ืŸ ืื™ื•ืŸ HT ืœึพืงื™ื” EV9",
            ],
            cache_examples=False,
            submit_btn="ืฉืœื—",
            stop_btn="ืขืฆื•ืจ",
            api_name="chat",  # exposes /chat endpoint for gradio_client tests
        )

    with gr.Column(elem_classes="app-footer"):
        gr.HTML("""
        <div class="app-footer" dir="rtl">
            <p style="margin: 0 0 6px 0;"><strong>ื’ื™ืฉื” ืœืฆ'ืื˜ ื“ืจืš API:</strong> <a href="https://huggingface.co/spaces/galbendavids/CarsRUS" target="_blank" rel="noopener">ืœื‘ื“ื•ืง ืืช ื”ืฆ'ืื˜ ื‘ึพAPI (ื’ื™ืฉื” ืœึพSpace + gradio_client / HTTP)</a></p>
            <p style="margin: 0 0 6px 0; font-size: 0.85rem;"><strong>ื“ื’ืžื™ื ื ืชืžื›ื™ื:</strong> Citroen C3 ยท Audi RS3 ยท Kia EV9 ยท MG S6 ยท Hyundai Elantra N ยท Aion HT ยท Genesis GV80 ยท Link & Co 01</p>
            <p style="margin: 0;"><a href="#" onclick="document.getElementById('arch-popup').classList.add('show'); return false;">๐Ÿ“ ืืจื›ื™ื˜ืงื˜ื•ืจื” ื•ืœืžื” ื‘ื—ืจื ื• ื‘ื”</a></p>
        </div>
        <div id="arch-popup" class="arch-popup-overlay" onclick="if(event.target===this) this.classList.remove('show');">
            <div class="arch-popup-box" onclick="event.stopPropagation();">
                <h2>ืืจื›ื™ื˜ืงื˜ื•ืจืช CarsRUS</h2>
                <p>ื”ืžืขืจื›ืช ื‘ื ื•ื™ื” ื›ึพRAG (Retrieval-Augmented Generation) ืขื ืกื•ื›ืŸ LangGraph, ื›ื“ื™ ืœืชืช ืชืฉื•ื‘ื•ืช ืžื‘ื•ืกืกื•ืช ืจืง ืขืœ ื›ืชื‘ื•ืช ืžึพauto.co.il. ืœื”ืœืŸ ื”ืจื›ื™ื‘ื™ื ื•ืœืžื” ื‘ื—ืจื ื• ื‘ื”ื.</p>
                <h3>ื”ืคื™ื™ืคืœื™ื™ืŸ โ€“ ืฆืขื“ ืื—ืจ ืฆืขื“</h3>
                <p>ืฉืื™ืœืชืช ื”ืžืฉืชืžืฉ ื ื›ื ืกืช ืœืฉืœื‘ <strong>ื ืจืžื•ืœ ืฉืžื•ืช</strong> (ื–ื™ื”ื•ื™ ื“ื’ื: RS3, ืœื™ื ืง ืื ื“ ืงื• 01 ื•ื›ื•'). ืื—ืจ ื›ืš <strong>ื–ื™ื”ื•ื™ ืกื•ื’ ืฉืืœื”</strong> โ€“ ื‘ื•ื“ื“ ืื• ื”ืฉื•ื•ืื”. ื‘ืฉืœื‘ <strong>ืฉืœื™ืคื”</strong> ืžืชื‘ืฆืข ื—ื™ืคื•ืฉ ื”ื™ื‘ืจื™ื“ื™ (ื•ืงื˜ื•ืจื™ื + ืžื™ืœื•ืช ืžืคืชื—) ื•ืžืชืงื‘ืœื™ื chunks ืจืœื•ื•ื ื˜ื™ื™ื. ืžื”ื ื‘ื•ื ื™ื <strong>ืคืจื•ืžืคื˜</strong> (system + user) ืขื ื”ื”ืงืฉืจ, ื”ื™ืกื˜ื•ืจื™ื™ืช ืฉื™ื—ื” ื•ืžื˜ืžื•ืŸ. ืœื‘ืกื•ืฃ <strong>ืงืจื™ืื” ืœึพLLM</strong> (OpenRouter ืื• Gemini) โ€“ ืคืขื ืื—ืช โ€“ ื•ื”ืชืฉื•ื‘ื” ืžื•ืฆื’ืช ืœืžืฉืชืžืฉ: ืงื•ื“ื "ืขื™ืงืจ ื”ืคื™ื™ืคืœื™ื™ืŸ" (ืฉืœื‘ื™ ืขื™ื‘ื•ื“) ื•ืื– "ื”ืชืฉื•ื‘ื”" ื‘ืœื™ ื›ืคื™ืœื•ืช.</p>
                <h3>1. ื ืชื•ื ื™ื ื•ื—ืœื•ืงื” ืœื ื•ืฉืื™ื</h3>
                <p><strong>ืžื”:</strong> ืงื•ื‘ืฅ <code>scraped_data.json</code> ืขื ื›ืชื‘ื•ืช ืžื‘ื—ื ื™ ื“ืจื›ื™ื. ื›ืœ ื›ืชื‘ื” ืžื—ื•ืœืงืช ืœึพchunks ืœืคื™ ื ื•ืฉืื™ื (ืžืคืจื˜ ื˜ื›ื ื™, ื‘ื˜ื™ื—ื•ืช, ืขื™ืฆื•ื‘, ื ื•ื—ื•ืช, ืฆืจื™ื›ื”, ื“ื™ื ืžื™ืงื”).</p>
                <p><strong>ืœืžื”:</strong> ื—ืœื•ืงื” ืœืคื™ ื ื•ืฉื ืžืืคืฉืจืช ื—ื™ืคื•ืฉ ืžืžื•ืงื“ ื•ืœืฉืœื•ืฃ ืจืง ืงื˜ืขื™ื ืจืœื•ื•ื ื˜ื™ื™ื ืœืฉืืœื”, ื‘ืžืงื•ื ืœื”ืขื‘ื™ืจ ืœืžื•ื“ืœ ื˜ืงืกื˜ ืืจื•ืš ืฉืœื. ื›ืš ื’ื ื”ื“ื™ื•ืง ืขื•ืœื” ื•ื’ื ื–ืžืŸ ื”ืชื’ื•ื‘ื” ื™ื•ืจื“.</p>
                <h3>2. ืžื˜ืึพื“ืื˜ื” ื•ื ืจืžื•ืœ ืฉืžื•ืช ืจื›ื‘ื™ื</h3>
                <p><strong>ืžื”:</strong> ืœื›ืœ chunk ื™ืฉ ืžื˜ืึพื“ืื˜ื” (ื›ื•ืชืจืช, URL, ื ื•ืฉื, ืžื™ืœื•ืช ืžืคืชื—). ืžื™ืœื•ืŸ ื ืจืžื•ืœ + ืจื’ืงืก ืžื–ื”ื” ื•ืจื™ืืฆื™ื•ืช ืฉืžื•ืช (ืื•ื“ื™ RS3, RS3, Link & Co 01, ืœื™ื ืง ืื ื“ ืงื• 01 ื•ื›ื•').</p>
                <p><strong>ืœืžื”:</strong> ื›ืš ื”ืžืฉืชืžืฉ ื™ื›ื•ืœ ืœืฉืื•ืœ ื‘ื›ืœ ื ื™ืกื•ื— ื•ื”ืžืขืจื›ืช ืชื–ื”ื” ืืช ื”ื“ื’ื ื•ื™ื—ืคืฉ ืจืง ื‘ืจืฉื™ืžืช ื”ื“ื’ืžื™ื ื”ื ืชืžื›ื™ื. ื‘ืœื™ ื ืจืžื•ืœ, ืฉืืœื•ืช ื›ืžื• "ืœื™ื ืง ืื ื“ ืงื•" ืื• "Link and Co 01" ื”ื™ื• ื ื“ื—ื•ืช ืœืžืจื•ืช ืฉื™ืฉ ืœื ื• ืžืกืžืš ืžืชืื™ื.</p>
                <h3>3. ื—ื™ืคื•ืฉ ื”ื™ื‘ืจื™ื“ื™ (ื•ืงื˜ื•ืจื™ื + ืžื™ืœื•ืช ืžืคืชื—)</h3>
                <p><strong>ืžื”:</strong> Embeddings ืขื <code>paraphrase-multilingual-MiniLM-L12-v2</code>, ื—ื™ืคื•ืฉ ื“ืžื™ื•ืŸ ืกืžื ื˜ื™ + ื‘ื•ื ื•ืก ืœึพchunks ืฉืžื›ื™ืœื™ื ืžื™ืœื•ืช ืžืคืชื— ืžื”ืฉืืœื” (ื›ื•ื— ืกื•ืก, ืงืž"ืฉ, ื‘ื˜ื™ื—ื•ืช ื•ื›ื•').</p>
                <p><strong>ืœืžื”:</strong> ืฉื™ืœื•ื‘ ืกืžื ื˜ื™ ื•ืžื™ืœื•ืช ืžืคืชื— ืžืฉืคืจ ื“ื™ื•ืง (ื›ึพ30% ื‘ื”ืฉื•ื•ืื” ืœื•ืงื˜ื•ืจื™ ื‘ืœื‘ื“) ื•ืžืชืื™ื ืœืขื‘ืจื™ืช ื•ืื ื’ืœื™ืช. ื”ืžื•ื“ืœ ื”ืจื‘ึพืœืฉื•ื ื™ ืชื•ืžืš ื‘ืฉืชื™ ื”ืฉืคื•ืช ื‘ืื•ืชื• ืื™ื ื“ืงืก.</p>
                <h3>4. ืกื•ื›ืŸ LangGraph (retrieve โ†’ generate)</h3>
                <p><strong>ืžื”:</strong> ืฆืขื“ ืจืืฉื•ืŸ โ€“ <code>prepare_generation</code> (ื ืจืžื•ืœ, ื–ื™ื”ื•ื™ ื”ืฉื•ื•ืื”/ื‘ื•ื“ื“, ืฉืœื™ืคื”, ื‘ื ื™ื™ืช ืคืจื•ืžืคื˜). ืฆืขื“ ืฉื ื™ โ€“ ืงืจื™ืื” ืœึพLLM ืขื ื”ื”ืงืฉืจ ืฉื ืฉืœื•ืฃ. ืื™ืŸ ืœื•ืœืืช refine โ€“ ืชืฉื•ื‘ื” ืื—ืช.</p>
                <p><strong>ืœืžื”:</strong> ื–ืจื™ืžื” ืื—ืช ื‘ืจื•ืจื” ืžืงืœื” ืขืœ ืชื—ื–ื•ืงื” ื•ื“ื™ื‘ื•ื’. ื”ื—ืœื˜ื ื• ืขืœ ืชืฉื•ื‘ื” ืื—ืช ืžืกื•ื“ืจืช (ืงื•ื“ื "ืขื™ืงืจ ื”ืคื™ื™ืคืœื™ื™ืŸ" ื•ืื– "ื”ืชืฉื•ื‘ื”") ื›ื“ื™ ืฉื”ืžืฉืชืžืฉ ื™ืจืื” ื”ืชืงื“ืžื•ืช ื‘ืœื™ ื›ืคื™ืœื•ืช ืฉืœ ืื•ืชื” ืชืฉื•ื‘ื” ืคืขืžื™ื™ื.</p>
                <h3>5. ืžื•ื“ืœ ืฉืคื” (OpenRouter / Gemini)</h3>
                <p><strong>ืžื”:</strong> ืื ืžื•ื’ื“ืจ <code>OPENROUTER_API_KEY</code> โ€“ ืžืฉืชืžืฉื™ื ื‘ึพOpenRouter (ืžื•ื“ืœ ื‘ืจื™ืจืช ืžื—ื“ืœ Gemini Flash). ืื—ืจืช โ€“ Google Gemini ืขื retry ืขืœ rate limit.</p>
                <p><strong>ืœืžื”:</strong> OpenRouter ืžืคื—ื™ืช ื—ืกื™ืžื•ืช rate limit ื•ืžืืคืฉืจ ื’ืžื™ืฉื•ืช ื‘ืžื•ื“ืœ. Gemini ื›ื’ื™ื‘ื•ื™ ื›ืฉื”ืžืคืชื— ืฉืœ OpenRouter ืœื ืžื•ื’ื“ืจ. ื”ืžื•ื“ืœ ืžื—ื–ื™ืจ ืชืฉื•ื‘ื” ืžื™ืœื•ืœื™ืช ืื—ืช ืฉืžืื’ื“ืช ืืช ื”ื”ืงืฉืจ ืฉื ืฉืœื•ืฃ.</p>
                <h3>6. ื–ื™ื›ืจื•ืŸ ืฉื™ื—ื” ื•ืžื˜ืžื•ืŸ</h3>
                <p><strong>ืžื”:</strong> ื”ื™ืกื˜ื•ืจื™ื™ืช ืฉื™ื—ื” (Q+A) ืœึพ3 ืชื•ืจื•ืช ืื—ืจื•ื ื•ืช ื ื›ืœืœืช ื‘ืคืจื•ืžืคื˜. ืžื˜ืžื•ืŸ ืœืคื™ hash ืฉืœ ื”ืฉืืœื” โ€“ ืชืฉื•ื‘ื•ืช ื–ื”ื•ืช ื—ื•ื–ืจื•ืช ืžื”ืžื˜ืžื•ืŸ ื‘ืœื™ ืงืจื™ืื” ืœึพLLM.</p>
                <p><strong>ืœืžื”:</strong> ืžืขื ื” ืœืฉืืœื•ืช ื”ืžืฉืš ("ื•ืžื” ืœื’ื‘ื™ ื”ื‘ื˜ื™ื—ื•ืช?") ื“ื•ืจืฉ ื”ืงืฉืจ โ€“ ื‘ืœื™ ื”ื™ืกื˜ื•ืจื™ื” ื”ืžื•ื“ืœ ืœื ื™ื•ื“ืข ืขืœ ืžื” ืžื“ื‘ืจื™ื. ื”ืžื˜ืžื•ืŸ ื—ื•ืกืš ื–ืžืŸ ื•ื›ืกืฃ ืขืœ ืฉืืœื•ืช ื—ื•ื–ืจื•ืช ื•ืžืฉืคืจ ื—ื•ื•ื™ื™ืช ืžืฉืชืžืฉ.</p>
                <p class="arch-credit">ื’ืœ ืฉืœืžื” ื‘ืŸ ื“ื•ื“ ยท <a href="mailto:galbendavids@gmail.com">galbendavids@gmail.com</a></p>
                <button type="button" class="arch-popup-close" onclick="document.getElementById('arch-popup').classList.remove('show');">ืกื’ื•ืจ</button>
            </div>
        </div>
        """)

if __name__ == "__main__":
    # Enable queue + open API so tests (gradio_client) can call the same chat pipeline
    demo.queue(api_open=True).launch()