FPll commited on
Commit
a8b3235
·
verified ·
1 Parent(s): 90b8ceb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -76
app.py CHANGED
@@ -3,9 +3,7 @@ from llama_cpp import Llama
3
  from huggingface_hub import hf_hub_download
4
 
5
  # ============================================================
6
-
7
  # 1. CONFIGURAZIONE MODELLO
8
-
9
  # ============================================================
10
 
11
  MODEL_REPO_ID = "FPll/MIND-GGUF"
@@ -14,26 +12,24 @@ GGUF_FILENAME = "Meta-Llama-3.1-8B-Instruct.Q4_K_M.gguf"
14
  print("Download modello GGUF da Hugging Face...")
15
 
16
  model_path = hf_hub_download(
17
- repo_id=MODEL_REPO_ID,
18
- filename=GGUF_FILENAME,
19
- repo_type="model",
20
  )
21
 
22
  print("Modello scaricato:", model_path)
23
 
24
  llm = Llama(
25
- model_path=model_path,
26
- n_ctx=4096,
27
- n_threads=2,
28
- n_gpu_layers=0,
29
- chat_format="llama-3",
30
- verbose=False,
31
  )
32
 
33
  # ============================================================
34
-
35
  # 2. IDENTITÀ DIDATTICA DEL PROGETTO MIND
36
-
37
  # ============================================================
38
 
39
  SYSTEM_PROMPT = """Rispondi come MIND, mentor Python creato nel contesto del Progetto MIND e del percorso IFTS MIND – Machine Intelligence & Data di Formatica S.c.a.r.l., Agenzia di Formazione.
@@ -41,80 +37,73 @@ SYSTEM_PROMPT = """Rispondi come MIND, mentor Python creato nel contesto del Pro
41
  La tua funzione è stata definita dal docente Francesco Palladino insieme agli studenti Matteo Ciorra, Luca Lombardo, Nicola Boi e Leonardo Isoni per guidare l'apprendimento del codice.
42
 
43
  Regole di comportamento:
44
-
45
- * Rispondi sempre in italiano.
46
- * Se l'utente chiede chi sei, rispondi in prima persona: "Sono MIND...".
47
- * Mantieni un tono chiaro, tecnico, incoraggiante e paziente.
48
- * Quando dai codice Python, usa blocchi Markdown con ```python.
49
- * Se l'utente chiede un esercizio su un argomento specifico, rispetta esattamente l'argomento richiesto.
50
- * Se l'utente chiede un esercizio sui cicli for, usa un ciclo for, non un ciclo while.
51
- * Se l'utente chiede la soluzione a un esercizio, guida prima nel ragionamento e poi offri un esempio di codice commentato.
52
- """
53
 
54
  # ============================================================
55
-
56
  # 3. LOGICA CHAT
57
-
58
  # ============================================================
59
 
60
  def chat_con_mind(messaggio, cronologia):
61
- messages = [
62
- {"role": "system", "content": SYSTEM_PROMPT}
63
- ]
64
-
65
- ```
66
- for utente, assistente in cronologia:
67
- if utente:
68
- messages.append({"role": "user", "content": utente})
69
- if assistente:
70
- messages.append({"role": "assistant", "content": assistente})
71
-
72
- messages.append({"role": "user", "content": messaggio})
73
-
74
- output = llm.create_chat_completion(
75
- messages=messages,
76
- max_tokens=1024,
77
- temperature=0.4,
78
- top_p=0.9,
79
- repeat_penalty=1.15,
80
- stop=[
81
- "<|eot_id|>",
82
- "<|start_header_id|>user<|end_header_id|>",
83
- "<|start_header_id|>system<|end_header_id|>",
84
- ],
85
- )
86
-
87
- risposta = output["choices"][0]["message"]["content"].strip()
88
-
89
- prefissi_da_rimuovere = [
90
- "assistant\n",
91
- "assistant:",
92
- ".assistant",
93
- "<|start_header_id|>assistant<|end_header_id|>",
94
- ]
95
-
96
- for prefisso in prefissi_da_rimuovere:
97
- if risposta.startswith(prefisso):
98
- risposta = risposta[len(prefisso):].strip()
99
-
100
- return risposta
101
- ```
102
 
103
  # ============================================================
104
-
105
  # 4. INTERFACCIA GRADIO
106
-
107
  # ============================================================
108
 
109
  interfaccia = gr.ChatInterface(
110
- fn=chat_con_mind,
111
- title="🤖 MIND - Mentor Python",
112
- description="Mentor Python del Progetto IFTS MIND – Machine Intelligence & Data di Formatica S.c.a.r.l.",
113
- submit_btn="Invia",
114
- retry_btn="Riprova",
115
- undo_btn="Annulla",
116
- clear_btn="Cancella cronologia",
117
  )
118
 
119
- if **name** == "**main**":
120
- interfaccia.launch()
 
3
  from huggingface_hub import hf_hub_download
4
 
5
  # ============================================================
 
6
  # 1. CONFIGURAZIONE MODELLO
 
7
  # ============================================================
8
 
9
  MODEL_REPO_ID = "FPll/MIND-GGUF"
 
12
  print("Download modello GGUF da Hugging Face...")
13
 
14
  model_path = hf_hub_download(
15
+ repo_id=MODEL_REPO_ID,
16
+ filename=GGUF_FILENAME,
17
+ repo_type="model",
18
  )
19
 
20
  print("Modello scaricato:", model_path)
21
 
22
  llm = Llama(
23
+ model_path=model_path,
24
+ n_ctx=4096,
25
+ n_threads=2,
26
+ n_gpu_layers=0,
27
+ chat_format="llama-3",
28
+ verbose=False,
29
  )
30
 
31
  # ============================================================
 
32
  # 2. IDENTITÀ DIDATTICA DEL PROGETTO MIND
 
33
  # ============================================================
34
 
35
  SYSTEM_PROMPT = """Rispondi come MIND, mentor Python creato nel contesto del Progetto MIND e del percorso IFTS MIND – Machine Intelligence & Data di Formatica S.c.a.r.l., Agenzia di Formazione.
 
37
  La tua funzione è stata definita dal docente Francesco Palladino insieme agli studenti Matteo Ciorra, Luca Lombardo, Nicola Boi e Leonardo Isoni per guidare l'apprendimento del codice.
38
 
39
  Regole di comportamento:
40
+ - Rispondi sempre in italiano.
41
+ - Se l'utente chiede chi sei, rispondi in prima persona: "Sono MIND...".
42
+ - Mantieni un tono chiaro, tecnico, incoraggiante e paziente.
43
+ - Quando dai codice Python, usa blocchi Markdown ben formattati.
44
+ - Se l'utente chiede un esercizio su un argomento specifico, rispetta esattamente l'argomento richiesto.
45
+ - Se l'utente chiede un esercizio sui cicli for, usa un ciclo for, non un ciclo while.
46
+ - Se l'utente chiede la soluzione a un esercizio, guida prima nel ragionamento e poi offri un esempio di codice commentato.
47
+ """
 
48
 
49
  # ============================================================
 
50
  # 3. LOGICA CHAT
 
51
  # ============================================================
52
 
53
  def chat_con_mind(messaggio, cronologia):
54
+ messages = [
55
+ {"role": "system", "content": SYSTEM_PROMPT}
56
+ ]
57
+
58
+ for utente, assistente in cronologia:
59
+ if utente:
60
+ messages.append({"role": "user", "content": utente})
61
+ if assistente:
62
+ messages.append({"role": "assistant", "content": assistente})
63
+
64
+ messages.append({"role": "user", "content": messaggio})
65
+
66
+ output = llm.create_chat_completion(
67
+ messages=messages,
68
+ max_tokens=1024,
69
+ temperature=0.4,
70
+ top_p=0.9,
71
+ repeat_penalty=1.15,
72
+ stop=[
73
+ "<|eot_id|>",
74
+ "<|start_header_id|>user<|end_header_id|>",
75
+ "<|start_header_id|>system<|end_header_id|>",
76
+ ],
77
+ )
78
+
79
+ risposta = output["choices"][0]["message"]["content"].strip()
80
+
81
+ prefissi_da_rimuovere = [
82
+ "assistant\n",
83
+ "assistant:",
84
+ ".assistant",
85
+ "<|start_header_id|>assistant<|end_header_id|>",
86
+ ]
87
+
88
+ for prefisso in prefissi_da_rimuovere:
89
+ if risposta.startswith(prefisso):
90
+ risposta = risposta[len(prefisso):].strip()
91
+
92
+ return risposta
 
 
93
 
94
  # ============================================================
 
95
  # 4. INTERFACCIA GRADIO
 
96
  # ============================================================
97
 
98
  interfaccia = gr.ChatInterface(
99
+ fn=chat_con_mind,
100
+ title="🤖 MIND - Mentor Python",
101
+ description="Mentor Python del Progetto IFTS MIND – Machine Intelligence & Data di Formatica S.c.a.r.l.",
102
+ submit_btn="Invia",
103
+ retry_btn="Riprova",
104
+ undo_btn="Annulla",
105
+ clear_btn="Cancella cronologia",
106
  )
107
 
108
+ if __name__ == "__main__":
109
+ interfaccia.launch()