Update app.py
Browse files
app.py
CHANGED
|
@@ -5,10 +5,7 @@ import sys
|
|
| 5 |
musl_path = "/home/user/app/libc.musl-x86_64.so.1"
|
| 6 |
if not os.path.exists(musl_path):
|
| 7 |
print("Configurazione libreria musl in corso...")
|
| 8 |
-
# Crea un collegamento alla libreria di sistema appena scaricata
|
| 9 |
os.system(f"ln -s /usr/lib/x86_64-linux-musl/libc.so {musl_path}")
|
| 10 |
-
|
| 11 |
-
# Riavvia istantaneamente l'app forzando la lettura della nuova cartella
|
| 12 |
env = os.environ.copy()
|
| 13 |
env["LD_LIBRARY_PATH"] = "/home/user/app:" + env.get("LD_LIBRARY_PATH", "")
|
| 14 |
os.execve(sys.executable, [sys.executable] + sys.argv, env)
|
|
@@ -18,54 +15,102 @@ import gradio as gr
|
|
| 18 |
from huggingface_hub import hf_hub_download
|
| 19 |
from llama_cpp import Llama
|
| 20 |
|
| 21 |
-
|
| 22 |
-
REPO_ID = "FPll/limba-mentor-llama3-gguf"
|
| 23 |
FILENAME = "Meta-Llama-3.1-8B.Q4_K_M.gguf"
|
| 24 |
|
| 25 |
# Scarica il file GGUF nella cache della Space
|
| 26 |
-
model_path = hf_hub_download(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
# Impostiamo i thread a 2 per sfruttare al meglio le 2 vCPU del Free Tier
|
| 30 |
llm = Llama(
|
| 31 |
model_path=model_path,
|
| 32 |
-
n_ctx=2048,
|
| 33 |
n_threads=2,
|
| 34 |
verbose=False
|
| 35 |
)
|
| 36 |
|
| 37 |
-
# 3. Definisci la funzione che gestisce la chat e lo streaming
|
| 38 |
def respond(message, history):
|
| 39 |
-
#
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
# 4.
|
| 62 |
-
# IMPORTANTE: Cambia "logo.png" con il nome esatto del file che hai caricato
|
| 63 |
demo = gr.ChatInterface(
|
| 64 |
respond,
|
| 65 |
-
title="Limba
|
| 66 |
-
description="
|
|
|
|
| 67 |
chatbot=gr.Chatbot(
|
| 68 |
-
avatar_images=[None, "logo.png"],
|
| 69 |
height=500
|
| 70 |
)
|
| 71 |
)
|
|
|
|
| 5 |
musl_path = "/home/user/app/libc.musl-x86_64.so.1"
|
| 6 |
if not os.path.exists(musl_path):
|
| 7 |
print("Configurazione libreria musl in corso...")
|
|
|
|
| 8 |
os.system(f"ln -s /usr/lib/x86_64-linux-musl/libc.so {musl_path}")
|
|
|
|
|
|
|
| 9 |
env = os.environ.copy()
|
| 10 |
env["LD_LIBRARY_PATH"] = "/home/user/app:" + env.get("LD_LIBRARY_PATH", "")
|
| 11 |
os.execve(sys.executable, [sys.executable] + sys.argv, env)
|
|
|
|
| 15 |
from huggingface_hub import hf_hub_download
|
| 16 |
from llama_cpp import Llama
|
| 17 |
|
| 18 |
+
REPO_ID = "FPII/limba-mentor-llama3-gguf"
|
|
|
|
| 19 |
FILENAME = "Meta-Llama-3.1-8B.Q4_K_M.gguf"
|
| 20 |
|
| 21 |
# Scarica il file GGUF nella cache della Space
|
| 22 |
+
model_path = hf_hub_download(
|
| 23 |
+
repo_id=REPO_ID,
|
| 24 |
+
filename=FILENAME,
|
| 25 |
+
token=os.environ.get("HF_TOKEN")
|
| 26 |
+
)
|
| 27 |
|
| 28 |
+
# Inizializza Llama (su CPU Free Tier)
|
|
|
|
| 29 |
llm = Llama(
|
| 30 |
model_path=model_path,
|
| 31 |
+
n_ctx=2048,
|
| 32 |
n_threads=2,
|
| 33 |
verbose=False
|
| 34 |
)
|
| 35 |
|
|
|
|
| 36 |
def respond(message, history):
|
| 37 |
+
# 1. IL SALVAVITA: Emette un segnale vuoto per impedire a Gradio di andare in crash (StopAsyncIteration)
|
| 38 |
+
partial_message = ""
|
| 39 |
+
yield partial_message
|
| 40 |
|
| 41 |
+
try:
|
| 42 |
+
# Estrazione sicura del testo nel caso Gradio invii un dizionario
|
| 43 |
+
msg_text = message.get("text", "") if isinstance(message, dict) else str(message)
|
| 44 |
|
| 45 |
+
system_instruction = (
|
| 46 |
+
"Sei Limba 2.0, creato da Francesco Palladino. Sei un modello di linguaggio che parla sardo (LSC) e italiano.\n"
|
| 47 |
+
"REGOLE DA RISPETTARE:\n"
|
| 48 |
+
"1. Se l'utente chiede una traduzione in sardo o in italiano, esegui la traduzione senza aggiungere altro.\n"
|
| 49 |
+
"2. Se l'utente chiede una formula o se nella risposta scrivi formule, usa il formato LaTeX racchiuso tra doppi simboli di dollaro ($$) su una riga separata, esempio: $$E=mc^2$$.\n"
|
| 50 |
+
"3. Se l'utente ti saluta (es. 'Ciao', 'Buongiorno'), rispondi esclusivamente con: 'Salude! So Limba 2.0, comente ti potzo agiudare?' e fermati.\n"
|
| 51 |
+
"4. Se l'utente fa una richiesta su un argomento specifico, rispondi alla richiesta in modo completo e accurato.\n"
|
| 52 |
+
)
|
| 53 |
|
| 54 |
+
few_shot = (
|
| 55 |
+
"<|start_header_id|>user<|end_header_id|>\n\nDimmi tre colori in sardo.<|eot_id|>"
|
| 56 |
+
"<|start_header_id|>assistant<|end_header_id|>\n\nEcco sos colores:\n\n"
|
| 57 |
+
"- Ruju\n"
|
| 58 |
+
"- Birde\n"
|
| 59 |
+
"- Blu<|eot_id|>"
|
| 60 |
+
"<|start_header_id|>user<|end_header_id|>\n\nConfronta oro e argento.<|eot_id|>"
|
| 61 |
+
"<|start_header_id|>assistant<|end_header_id|>\n\nEcco sa tabella:\n\n"
|
| 62 |
+
"| Metallu | Simbolu | n. Atòmicu |\n"
|
| 63 |
+
"| :--- | :--- | :--- |\n"
|
| 64 |
+
"| Oro | Au | 79 |\n"
|
| 65 |
+
"| Prata | Ag | 47 |<|eot_id|>"
|
| 66 |
+
)
|
| 67 |
|
| 68 |
+
# 2. GESTIONE UNIVERSALE HISTORY (Funziona con tutte le versioni di Gradio)
|
| 69 |
+
history_text = ""
|
| 70 |
+
for item in history:
|
| 71 |
+
if isinstance(item, dict):
|
| 72 |
+
# Formato nuovo di Gradio
|
| 73 |
+
role = item.get("role", "user")
|
| 74 |
+
content = item.get("content", "")
|
| 75 |
+
history_text += f"<|start_header_id|>{role}<|end_header_id|>\n\n{content}<|eot_id|>"
|
| 76 |
+
else:
|
| 77 |
+
# Formato classico a tuple
|
| 78 |
+
user_msg, bot_msg = item
|
| 79 |
+
history_text += f"<|start_header_id|>user<|end_header_id|>\n\n{user_msg}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n{bot_msg}<|eot_id|>"
|
| 80 |
+
|
| 81 |
+
# Assemblaggio del Prompt
|
| 82 |
+
prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{system_instruction}<|eot_id|>{few_shot}{history_text}<|start_header_id|>user<|end_header_id|>\n\n{msg_text}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
|
| 83 |
+
|
| 84 |
+
# 3. GENERAZIONE CON PARAMETRI OTTIMIZZATI PER GLI ELENCHI
|
| 85 |
+
response = llm(
|
| 86 |
+
prompt,
|
| 87 |
+
max_tokens=512,
|
| 88 |
+
stop=["<|eot_id|>", "<|start_header_id|>", "<|end_header_id|>"],
|
| 89 |
+
stream=True,
|
| 90 |
+
temperature=0.1,
|
| 91 |
+
top_p=0.9,
|
| 92 |
+
repeat_penalty=1.15
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
for chunk in response:
|
| 96 |
+
if "choices" in chunk and len(chunk["choices"]) > 0:
|
| 97 |
+
text = chunk["choices"][0].get("text", "")
|
| 98 |
+
if text:
|
| 99 |
+
partial_message += text
|
| 100 |
+
yield partial_message
|
| 101 |
+
|
| 102 |
+
# Scudo Anti-Crash: se c'è un errore te lo stampa in chat invece di far crollare la pagina
|
| 103 |
+
except Exception as e:
|
| 104 |
+
yield f"🚨 Errore Python interno: {str(e)}"
|
| 105 |
|
| 106 |
+
# 4. INTERFACCIA VISIVA
|
|
|
|
| 107 |
demo = gr.ChatInterface(
|
| 108 |
respond,
|
| 109 |
+
title="Limba 2.0 - S'assistente IA tuo chi faeddat sardu",
|
| 110 |
+
description="Ajò! So Limba 2.0, s'assistente tuo pro s'informatica, sa matematica e s'iscentzia. Faeddo sardu e italianu.",
|
| 111 |
+
examples=["Iscrie unu script in Python pro lèghere unu file CSV.", "Ispiegami su teorema de Pitagora.", "Traduci in sardo: L'algoritmo di machine learning sta analizzando i dati."],
|
| 112 |
chatbot=gr.Chatbot(
|
| 113 |
+
avatar_images=[None, "logo.png"],
|
| 114 |
height=500
|
| 115 |
)
|
| 116 |
)
|