Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,38 +3,52 @@ from huggingface_hub import hf_hub_download
|
|
| 3 |
from llama_cpp import Llama
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
# 1. Recupero
|
| 7 |
token = os.getenv("HF_TOKEN")
|
| 8 |
if token:
|
| 9 |
token = token.strip()
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# Scarichiamo il modello
|
| 14 |
model_path = hf_hub_download(
|
| 15 |
repo_id="FPll/Limba-1.0",
|
| 16 |
filename="Meta-Llama-3.1-8B.Q4_K_M.gguf",
|
| 17 |
token=token
|
| 18 |
)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# 2. Inizializzazione del modello (Ottimizzato per 4 thread)
|
| 23 |
-
llm = Llama(model_path=model_path, n_ctx=2048, n_threads=4)
|
| 24 |
|
| 25 |
def respond(message, history):
|
| 26 |
-
#
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
# 3. Interfaccia
|
| 33 |
demo = gr.ChatInterface(
|
| 34 |
respond,
|
| 35 |
-
title="Limba 1.0 - Progetto Margherita",
|
| 36 |
description="Su primu protòtipu de IA pro s'iscola sarda. Parla-mi in sardu o de fìsica e matemàtica!",
|
| 37 |
-
examples=["Iscrie sa fòrmula de s'energia tzinetica", "Chie est istadu Enricu Fermi?", "A ite zerbit sa derivata?"]
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
|
|
|
| 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__":
|