File size: 2,180 Bytes
4e2c8e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import subprocess
import json
import os

# Use the remote API if available, otherwise local
API_URL = os.environ.get("API_URL", "")
if not API_URL:
    # Try to use the engine directly
    API_URL = "http://localhost:8000"

def search(query, top_n):
    if not query.strip():
        return "Entrez une requête pour lancer la recherche."
    
    try:
        if API_URL and API_URL != "http://localhost:8000":
            import requests
            resp = requests.get(f"{API_URL}/search-semantic", params={"query": query, "top": str(top_n)})
            results = resp.json()
        else:
            # Use local API
            import requests
            resp = requests.get("http://localhost:8000/search-semantic", params={"query": query, "top": str(top_n)})
            results = resp.json()
    except Exception as e:
        return f"Erreur de connexion: {e}\n\nLe moteur doit être lancé avec `zaza server` d'abord."
    
    if not results:
        return "Aucun résultat trouvé."
    
    output = []
    for i, r in enumerate(results, 1):
        score_pct = int(r.get("score", 0) * 100)
        output.append(f"**#{i}** — {r.get('filename', '?')} (score: {score_pct}%)")
        output.append(f"{r.get('excerpt', '')}")
        output.append(f"Type: {r.get('filetype', 'txt')} • Chunk {r.get('chunk_index', 0)}/{r.get('total_chunks', 1)}")
        output.append("")
    
    return "\n".join(output)

# Gradio interface
with gr.Blocks(title="Zaza Semantic Search") as demo:
    gr.Markdown("# 🔍 Zaza Semantic Search\nRecherche sémantique dans vos documents ingérés")
    
    with gr.Row():
        query_input = gr.Textbox(placeholder="Ex: comment gérer un projet IA local...", lines=2)
        top_slider = gr.Slider(1, 20, value=5, step=1, label="Top résultats")
    
    search_btn = gr.Button("Rechercher", variant="primary")
    output = gr.Markdown("Entrez votre requête ci-dessus et cliquez Rechercher.")
    
    search_btn.click(search, [query_input, top_slider], output)
    query_input.submit(search, [query_input, top_slider], output)

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", share=False)