Download app.py from Telmoud/Py-Prolog: direct link, hf CLI and curl.
- Browser
- Download file 2.53 kB
-
https://huggingface.co/spaces/Telmoud/Py-Prolog/resolve/main/app.py
- Command line
-
hf download hf://spaces/Telmoud/Py-Prolog/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Telmoud/Py-Prolog/resolve/main/app.py
2.53 kB
| import gradio as gr | |
| from pyswip import Prolog | |
| prolog = Prolog() | |
| current_query = {"results": [], "index": 0} | |
| def load_file(file): | |
| try: | |
| prolog.consult(file.name) | |
| return "✔ Base de connaissance chargée avec succès" | |
| except Exception as e: | |
| return f"❌ Erreur: {str(e)}" | |
| def run_query(query): | |
| global current_query | |
| try: | |
| results = list(prolog.query(query)) | |
| current_query["results"] = results | |
| current_query["index"] = 0 | |
| if not results: | |
| return "false." | |
| if results == [{}]: | |
| return "true." | |
| return format_result(results[0]) | |
| except Exception as e: | |
| return f"❌ Erreur: {str(e)}" | |
| def next_solution(): | |
| global current_query | |
| results = current_query["results"] | |
| idx = current_query["index"] + 1 | |
| if idx >= len(results): | |
| return "false." | |
| current_query["index"] = idx | |
| return format_result(results[idx]) | |
| def format_result(res): | |
| return " ; ".join([f"{k} = {v}" for k, v in res.items()]) + " ;" | |
| with gr.Blocks(title="Py-Prolog") as app: | |
| gr.Markdown( | |
| """ | |
| <div style="text-align:center; margin-top:20px; color:gray;"> | |
| 🧠 Py-Prolog | |
| Mini SWI-Prolog en Python (Powered by PySwip) | |
| Charger un fichier `.pl`, puis exécuter des requêtes comme en Prolog. | |
| </div> | |
| """, | |
| elem_id="top-markdown" | |
| ) | |
| # ===== File loader ===== | |
| with gr.Row(): | |
| file_input = gr.File(label="📂 Charger fichier Prolog (.pl)") | |
| load_btn = gr.Button("⚡ Charger", variant="primary") | |
| status = gr.Textbox(label="📢 Etat", interactive=False) | |
| load_btn.click(load_file, inputs=file_input, outputs=status) | |
| gr.Markdown("💻 Terminal Prolog") | |
| query_input = gr.Textbox( | |
| label="Requête", | |
| placeholder="Ex: grand_pere(X,dudulle)." | |
| ) | |
| with gr.Row(): | |
| run_btn = gr.Button("▶ Exécuter", variant="primary") | |
| next_btn = gr.Button("➡ Next ( ; )") | |
| output = gr.Textbox(label="Résultat", lines=5) | |
| run_btn.click(run_query, inputs=query_input, outputs=output) | |
| next_btn.click(next_solution, outputs=output) | |
| gr.Markdown( | |
| """ | |
| <div style="text-align:center; margin-top:20px; color:gray;"> | |
| Developed by Cheikh Ahmed Telmoud | |
| </div> | |
| """, | |
| elem_id="footer" | |
| ) | |
| app.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| theme=gr.themes.Soft(), | |
| ssr_mode=False, | |
| debug=True | |
| ) | |