Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,57 +14,46 @@ model_path = hf_hub_download(
|
|
| 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
|
| 22 |
-
# Usiamo una struttura a blocchi che i modelli Llama leggono meglio.
|
| 23 |
system_message = (
|
| 24 |
-
"### IDENTITÀ
|
| 25 |
-
"
|
| 26 |
-
"
|
| 27 |
-
"
|
| 28 |
-
"
|
| 29 |
-
"
|
| 30 |
-
"
|
| 31 |
-
"
|
| 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
|
| 45 |
-
|
| 46 |
prompt,
|
| 47 |
max_tokens=512,
|
| 48 |
-
temperature=0.1,
|
| 49 |
-
repeat_penalty=1.2,
|
| 50 |
-
|
| 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 |
-
|
| 57 |
-
|
| 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.
|
| 67 |
-
examples=["Chie ti hat creadu?", "
|
| 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__":
|