Upload ./app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Use the remote API if available, otherwise local
|
| 7 |
+
API_URL = os.environ.get("API_URL", "")
|
| 8 |
+
if not API_URL:
|
| 9 |
+
# Try to use the engine directly
|
| 10 |
+
API_URL = "http://localhost:8000"
|
| 11 |
+
|
| 12 |
+
def search(query, top_n):
|
| 13 |
+
if not query.strip():
|
| 14 |
+
return "Entrez une requête pour lancer la recherche."
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
if API_URL and API_URL != "http://localhost:8000":
|
| 18 |
+
import requests
|
| 19 |
+
resp = requests.get(f"{API_URL}/search-semantic", params={"query": query, "top": str(top_n)})
|
| 20 |
+
results = resp.json()
|
| 21 |
+
else:
|
| 22 |
+
# Use local API
|
| 23 |
+
import requests
|
| 24 |
+
resp = requests.get("http://localhost:8000/search-semantic", params={"query": query, "top": str(top_n)})
|
| 25 |
+
results = resp.json()
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return f"Erreur de connexion: {e}\n\nLe moteur doit être lancé avec `zaza server` d'abord."
|
| 28 |
+
|
| 29 |
+
if not results:
|
| 30 |
+
return "Aucun résultat trouvé."
|
| 31 |
+
|
| 32 |
+
output = []
|
| 33 |
+
for i, r in enumerate(results, 1):
|
| 34 |
+
score_pct = int(r.get("score", 0) * 100)
|
| 35 |
+
output.append(f"**#{i}** — {r.get('filename', '?')} (score: {score_pct}%)")
|
| 36 |
+
output.append(f"{r.get('excerpt', '')}")
|
| 37 |
+
output.append(f"Type: {r.get('filetype', 'txt')} • Chunk {r.get('chunk_index', 0)}/{r.get('total_chunks', 1)}")
|
| 38 |
+
output.append("")
|
| 39 |
+
|
| 40 |
+
return "\n".join(output)
|
| 41 |
+
|
| 42 |
+
# Gradio interface
|
| 43 |
+
with gr.Blocks(title="Zaza Semantic Search") as demo:
|
| 44 |
+
gr.Markdown("# 🔍 Zaza Semantic Search\nRecherche sémantique dans vos documents ingérés")
|
| 45 |
+
|
| 46 |
+
with gr.Row():
|
| 47 |
+
query_input = gr.Textbox(placeholder="Ex: comment gérer un projet IA local...", lines=2)
|
| 48 |
+
top_slider = gr.Slider(1, 20, value=5, step=1, label="Top résultats")
|
| 49 |
+
|
| 50 |
+
search_btn = gr.Button("Rechercher", variant="primary")
|
| 51 |
+
output = gr.Markdown("Entrez votre requête ci-dessus et cliquez Rechercher.")
|
| 52 |
+
|
| 53 |
+
search_btn.click(search, [query_input, top_slider], output)
|
| 54 |
+
query_input.submit(search, [query_input, top_slider], output)
|
| 55 |
+
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
demo.launch(server_name="0.0.0.0", share=False)
|