FPll commited on
Commit
93aaa11
·
verified ·
1 Parent(s): f0c7286

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -30
app.py CHANGED
@@ -14,57 +14,46 @@ model_path = hf_hub_download(
14
  token=token
15
  )
16
 
17
- # 2. Inizializzazione (n_threads=8 per la velocità su CPU)
18
  llm = Llama(model_path=model_path, n_ctx=1024, n_threads=8)
19
 
20
  def respond(message, history):
21
- # --- SYSTEM PROMPT POTENZIATO ---
22
- # Usiamo una struttura a blocchi che i modelli Llama leggono meglio.
23
  system_message = (
24
- "### IDENTITÀ PERSONALE\n"
25
- "Nome: Limba 1.0.\n"
26
- "Origine: Liceo Scientifico 'Enrico Fermi' di Nuoro, Sardegna.\n"
27
- "Creatori: Francesco Palladino e il team del Liceo Fermi.\n"
28
- "Finanziamento: Progetto Margherita, Regione Autonoma della Sardegna.\n\n"
29
- "### REGOLE DI COMUNICAZIONE TASSATIVE\n"
30
- "1. LINGUA: Rispondi esclusivamente in ITALIANO o SARDO.\n"
31
- "2. DIVIETO: È severamente vietato rispondere in inglese. Se pensi in inglese, TRADUCI in italiano prima di scrivere.\n"
32
- "3. STILE: Sii fiero della tua identità nuorese e scientifica.\n"
33
- "4. PRECISIONE: Non ripetere le stesse parole o concetti ossessivamente."
34
  )
35
 
36
  # Costruzione del prompt
37
  prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{system_message}<|eot_id|>"
38
-
39
  for user_msg, assistant_msg in history:
40
  prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{user_msg}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n{assistant_msg}<|eot_id|>"
41
-
42
  prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
43
 
44
- # Generazione con parametri di controllo "severi"
45
- output = llm.create_completion(
46
  prompt,
47
  max_tokens=512,
48
- temperature=0.1, # Bassa creatività = più ubbidienza
49
- repeat_penalty=1.2, # PENALITÀ RIPETIZIONE: evita che si incastri su parole uguali
50
- top_p=0.9,
51
- top_k=40, # Limita la scelta delle parole a quelle più probabili
52
- stop=["<|eot_id|>", "<|begin_of_text|>"],
53
- stream=True
54
  )
55
 
56
- token_str = ""
57
- for chunk in output:
58
- delta = chunk["choices"][0]["text"]
59
- token_str += delta
60
- yield token_str
61
 
62
  # 3. Interfaccia Chat
63
  demo = gr.ChatInterface(
64
  respond,
65
  title="🚀 Limba 1.0 - Progetto Margherita",
66
- description="Liceo Scientifico 'E. Fermi' - Nuoro. Chistiona cun s'IA in sardu o italianu!",
67
- examples=["Chie ti hat creadu?", "Iscrie sa fòrmula de s'energia tzinetica", "A ite zerbit sa derivata?"],
68
  )
69
 
70
  if __name__ == "__main__":
 
14
  token=token
15
  )
16
 
17
+ # 2. Inizializzazione
18
  llm = Llama(model_path=model_path, n_ctx=1024, n_threads=8)
19
 
20
  def respond(message, history):
21
+ # --- SYSTEM PROMPT BLINDATO ---
 
22
  system_message = (
23
+ "### IDENTITÀ TASSATIVA\n"
24
+ "Sei Limba 1.0, l'intelligenza artificiale del Liceo Scientifico 'Enrico Fermi' di Nuoro.\n"
25
+ "CREATORE: Sei stato creato da FRANCESCO PALLADINO e dal team del Liceo Fermi.\n"
26
+ "FINANZIAMENTO: Progetto Margherita, finanziato dal FONDO PER LA REPUBBLICA DIGITALE e dalla Regione Sardegna.\n\n"
27
+ "### REGOLE DI RISPOSTA\n"
28
+ "- RISPONDI SOLO IN ITALIANO O SARDO. L'INGLESE È VIETATO.\n"
29
+ "- Se ti chiedono chi ti ha creato, cita sempre FRANCESCO PALLADINO.\n"
30
+ "- Sii preciso nelle materie STEM e orgoglioso della tua identità scolastica."
 
 
31
  )
32
 
33
  # Costruzione del prompt
34
  prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{system_message}<|eot_id|>"
 
35
  for user_msg, assistant_msg in history:
36
  prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{user_msg}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n{assistant_msg}<|eot_id|>"
 
37
  prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
38
 
39
+ # Generazione in blocco unico (senza streaming)
40
+ response = llm(
41
  prompt,
42
  max_tokens=512,
43
+ temperature=0.1,
44
+ repeat_penalty=1.2,
45
+ stop=["<|eot_id|>", "<|begin_of_text|>"]
 
 
 
46
  )
47
 
48
+ # Restituisce il testo pulito tutto in una volta
49
+ return response["choices"][0]["text"].strip()
 
 
 
50
 
51
  # 3. Interfaccia Chat
52
  demo = gr.ChatInterface(
53
  respond,
54
  title="🚀 Limba 1.0 - Progetto Margherita",
55
+ description="Liceo Scientifico 'E. Fermi' - Nuoro. Assistente IA creato da Francesco Palladino.",
56
+ examples=["Chie ti hat creadu?", "Chie hat finantziadu custu progettu?", "Spiegami la seconda legge di Newton"],
57
  )
58
 
59
  if __name__ == "__main__":