Download app.py from ffffre/zaza-semantic-search: direct link, hf CLI and curl.
- Browser
- Download file 2.18 kB
-
https://huggingface.co/ffffre/zaza-semantic-search/resolve/main/app.py
- Command line
-
hf download hf://ffffre/zaza-semantic-search/app.py
-
curl -L -o app.py https://huggingface.co/ffffre/zaza-semantic-search/resolve/main/app.py
2.18 kB
| 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) | |