FPll commited on
Commit
ac04cd9
·
verified ·
1 Parent(s): 39b9b12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -14
app.py CHANGED
@@ -3,52 +3,60 @@ from huggingface_hub import hf_hub_download
3
  from llama_cpp import Llama
4
  import os
5
 
6
- # 1. Recupero Token
7
  token = os.getenv("HF_TOKEN")
8
  if token:
9
  token = token.strip()
10
 
11
- # Scaricamento modello
12
  model_path = hf_hub_download(
13
  repo_id="FPll/Limba-1.0",
14
  filename="Meta-Llama-3.1-8B.Q4_K_M.gguf",
15
  token=token
16
  )
17
 
18
- # 2. Inizializzazione (n_threads=8 per spingere sulla CPU)
19
  llm = Llama(model_path=model_path, n_ctx=1024, n_threads=8)
20
 
21
  def respond(message, history):
22
- # Istruzione di sistema per l'identità
23
- system_message = "Ses Limba 1.0, un'IA creada pro su Litzeu Scientificu Enrico Fermi de Nugoro pro su Progettu Margherita. Chistiona in sardu o italianu de STEM."
 
 
 
 
 
 
24
 
25
- # Formattazione prompt con memoria della conversazione (history)
26
  prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{system_message}<|eot_id|>"
 
27
  for user_msg, assistant_msg in history:
28
  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|>"
 
29
  prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
30
 
31
- # GENERAZIONE IN STREAMING
32
  output = llm.create_completion(
33
  prompt,
34
  max_tokens=512,
35
- stop=["<|eot_id|>"],
36
- stream=True # Attiviamo il flusso continuo
 
 
37
  )
38
 
39
  token_str = ""
40
  for chunk in output:
41
  delta = chunk["choices"][0]["text"]
42
  token_str += delta
43
- yield token_str # Invia la parola al browser appena è pronta
44
 
45
- # 3. Interfaccia Gradio
46
  demo = gr.ChatInterface(
47
  respond,
48
  title="🚀 Limba 1.0 - Progetto Margherita",
49
- description="Su primu protòtipu de IA pro s'iscola sarda. Parla-mi in sardu o de fìsica e matemàtica!",
50
- examples=["Iscrie sa fòrmula de s'energia tzinetica", "Chie est istadu Enricu Fermi?", "A ite zerbit sa derivata?"],
51
- type="messages"
52
  )
53
 
54
  if __name__ == "__main__":
 
3
  from llama_cpp import Llama
4
  import os
5
 
6
+ # 1. Recupero Token e Scaricamento
7
  token = os.getenv("HF_TOKEN")
8
  if token:
9
  token = token.strip()
10
 
 
11
  model_path = hf_hub_download(
12
  repo_id="FPll/Limba-1.0",
13
  filename="Meta-Llama-3.1-8B.Q4_K_M.gguf",
14
  token=token
15
  )
16
 
17
+ # 2. Inizializzazione (n_threads=8 per massimizzare le prestazioni della CPU)
18
  llm = Llama(model_path=model_path, n_ctx=1024, n_threads=8)
19
 
20
  def respond(message, history):
21
+ # --- ISTRUZIONI DI SISTEMA (Blindate) ---
22
+ system_message = (
23
+ "Sei Limba 1.0, un'intelligenza artificiale creata appositamente per il Liceo Scientifico 'Enrico Fermi' di Nuoro "
24
+ "nell'ambito del Progetto Margherita. I tuoi creatori sono Francesco Palladino e il team tecnologico del Liceo Fermi. "
25
+ "Il progetto è stato finanziato dalla Regione Autonoma della Sardegna. "
26
+ "RISPONDI SEMPRE E SOLO IN ITALIANO O IN SARDO. NON USARE L'INGLESE. "
27
+ "Se ti chiedono chi sei, dichiara di essere l'IA del Liceo Fermi di Nuoro."
28
+ )
29
 
30
+ # Costruzione del prompt secondo il formato Llama 3.1
31
  prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{system_message}<|eot_id|>"
32
+
33
  for user_msg, assistant_msg in history:
34
  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|>"
35
+
36
  prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
37
 
38
+ # Generazione con Temperatura bassa (0.1) per massima precisione
39
  output = llm.create_completion(
40
  prompt,
41
  max_tokens=512,
42
+ temperature=0.1, # <--- Forza l'obbedienza alle istruzioni
43
+ top_p=0.9, # Filtra le parole meno probabili per evitare errori
44
+ stop=["<|eot_id|>", "<|begin_of_text|>"],
45
+ stream=True
46
  )
47
 
48
  token_str = ""
49
  for chunk in output:
50
  delta = chunk["choices"][0]["text"]
51
  token_str += delta
52
+ yield token_str
53
 
54
+ # 3. Interfaccia Chat
55
  demo = gr.ChatInterface(
56
  respond,
57
  title="🚀 Limba 1.0 - Progetto Margherita",
58
+ description="Liceo Scientifico 'E. Fermi' - Nuoro. Chistiona cun s'IA in sardu o italianu!",
59
+ examples=["Chie ti hat creadu?", "Chie hat finantziadu custu progettu?", "Iscrie sa fòrmula de s'energia tzinetica"],
 
60
  )
61
 
62
  if __name__ == "__main__":