Telmoud commited on
Commit
fa335a3
·
1 Parent(s): efb4bd2

Add Py Prolog Files

Browse files
Files changed (5) hide show
  1. Dockerfile +12 -0
  2. README.md +15 -14
  3. app.py +102 -0
  4. packages.txt +1 -0
  5. requirements.txt +2 -0
Dockerfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+
3
+ # Installer SWI-Prolog
4
+ RUN apt-get update && apt-get install -y swi-prolog
5
+
6
+ # Installer dépendances
7
+ RUN pip install gradio pyswip
8
+
9
+ WORKDIR /app
10
+ COPY . /app
11
+
12
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -1,14 +1,15 @@
1
- ---
2
- title: Py Prolog
3
- emoji: 🏢
4
- colorFrom: red
5
- colorTo: green
6
- sdk: gradio
7
- sdk_version: 6.10.0
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
- short_description: Mini moteur Prolog avec interface moderne en Python.
12
- ---
13
-
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
1
+ # 🧠 Py-Prolog
2
+
3
+ Mini moteur Prolog avec interface moderne en Python.
4
+
5
+ ## Features
6
+ - Chargement de fichier `.pl`
7
+ - Requêtes Prolog interactives
8
+ - Bouton `;` pour solutions multiples
9
+ - Basé sur PySwip + SWI-Prolog
10
+
11
+ ## Exemple
12
+
13
+ ```prolog
14
+ ?- grand_pere(X,dudulle).
15
+ X = bernard ;
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from pyswip import Prolog
3
+
4
+ prolog = Prolog()
5
+
6
+ # =========================
7
+ # 🔹 STATE (solutions cache)
8
+ # =========================
9
+ current_query = {"results": [], "index": 0}
10
+
11
+ # =========================
12
+ # 🔹 Charger fichier
13
+ # =========================
14
+ def load_file(file):
15
+ try:
16
+ prolog.consult(file.name)
17
+ return "✔ Base de connaissance chargée avec succès"
18
+ except Exception as e:
19
+ return f"❌ Erreur: {str(e)}"
20
+
21
+ # =========================
22
+ # 🔹 Lancer requête
23
+ # =========================
24
+ def run_query(query):
25
+ global current_query
26
+
27
+ try:
28
+ results = list(prolog.query(query))
29
+
30
+ current_query["results"] = results
31
+ current_query["index"] = 0
32
+
33
+ if not results:
34
+ return "false."
35
+
36
+ if results == [{}]:
37
+ return "true."
38
+
39
+ return format_result(results[0])
40
+
41
+ except Exception as e:
42
+ return f"❌ Erreur: {str(e)}"
43
+
44
+ # =========================
45
+ # 🔹 Next solution (;)
46
+ # =========================
47
+ def next_solution():
48
+ global current_query
49
+
50
+ results = current_query["results"]
51
+ idx = current_query["index"] + 1
52
+
53
+ if idx >= len(results):
54
+ return "false."
55
+
56
+ current_query["index"] = idx
57
+ return format_result(results[idx])
58
+
59
+ # =========================
60
+ # 🔹 Format Prolog-like
61
+ # =========================
62
+ def format_result(res):
63
+ return " ; ".join([f"{k} = {v}" for k, v in res.items()]) + " ;"
64
+
65
+ # =========================
66
+ # 🎨 Interface moderne
67
+ # =========================
68
+ with gr.Blocks(theme=gr.themes.Soft(), title="Py-Prolog") as app:
69
+
70
+ gr.Markdown("""
71
+ # 🧠 Py-Prolog
72
+ ### Mini SWI-Prolog en Python (Powered by PySwip)
73
+
74
+ Charger un fichier `.pl`, puis exécuter des requêtes comme en Prolog.
75
+ """)
76
+
77
+ with gr.Row():
78
+ file_input = gr.File(label="📂 Charger fichier Prolog (.pl)")
79
+ load_btn = gr.Button("⚡ Charger", variant="primary")
80
+
81
+ status = gr.Textbox(label="📢 Etat", interactive=False)
82
+
83
+ load_btn.click(load_file, inputs=file_input, outputs=status)
84
+
85
+ gr.Markdown("## 💻 Terminal Prolog")
86
+
87
+ query_input = gr.Textbox(
88
+ label="Requête",
89
+ placeholder="Ex: grand_pere(X,dudulle)."
90
+ )
91
+
92
+ with gr.Row():
93
+ run_btn = gr.Button("▶ Exécuter", variant="primary")
94
+ next_btn = gr.Button("➡ Next ( ; )")
95
+
96
+ output = gr.Textbox(label="Résultat", lines=5)
97
+
98
+ run_btn.click(run_query, inputs=query_input, outputs=output)
99
+ next_btn.click(next_solution, outputs=output)
100
+
101
+ # 🔥 IMPORTANT (évite erreurs asyncio)
102
+ app.launch(server_name="0.0.0.0", server_port=7860)
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ swi-prolog
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ pyswip