Update src/streamlit_app.py
Browse files- src/streamlit_app.py +16 -10
src/streamlit_app.py
CHANGED
|
@@ -2,45 +2,51 @@ import streamlit as st
|
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
from llama_cpp import Llama
|
| 4 |
|
| 5 |
-
# Configurazione
|
| 6 |
st.set_page_config(page_title="Limba Mentor", page_icon="🏴")
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
st.title("Limba Mentor 🏴")
|
| 9 |
st.markdown("### Su mentore tuo in limba sarda")
|
| 10 |
|
| 11 |
-
# 2.
|
| 12 |
@st.cache_resource
|
| 13 |
def load_model():
|
| 14 |
-
# IMPORTANTE: controlla se il file nel tuo repo si chiama davvero così
|
| 15 |
-
# Se no, cambia 'unsloth.Q4_K_M.gguf' con il nome esatto che vedi su HF
|
| 16 |
try:
|
|
|
|
| 17 |
model_path = hf_hub_download(
|
| 18 |
repo_id="FPll/limba-mentor-llama3-gguf",
|
| 19 |
-
filename="
|
| 20 |
)
|
|
|
|
| 21 |
return Llama(model_path=model_path, n_ctx=2048, n_threads=2)
|
| 22 |
except Exception as e:
|
| 23 |
-
st.error(f"Errore nel
|
| 24 |
return None
|
| 25 |
|
| 26 |
-
with st.spinner("Sto
|
| 27 |
llm = load_model()
|
| 28 |
|
| 29 |
-
# 3. Gestione della
|
| 30 |
if "messages" not in st.session_state:
|
| 31 |
st.session_state.messages = []
|
| 32 |
|
|
|
|
| 33 |
for message in st.session_state.messages:
|
| 34 |
with st.chat_message(message["role"]):
|
| 35 |
st.markdown(message["content"])
|
| 36 |
|
|
|
|
| 37 |
if prompt := st.chat_input("Iscrie inoghe..."):
|
| 38 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 39 |
with st.chat_message("user"):
|
| 40 |
st.markdown(prompt)
|
| 41 |
|
|
|
|
| 42 |
with st.chat_message("assistant"):
|
| 43 |
-
# Formattazione per Llama 3
|
| 44 |
full_prompt = f"### Istruzione:\n{prompt}\n\n### Risposta:\n"
|
| 45 |
|
| 46 |
if llm:
|
|
@@ -54,4 +60,4 @@ if prompt := st.chat_input("Iscrie inoghe..."):
|
|
| 54 |
st.markdown(answer)
|
| 55 |
st.session_state.messages.append({"role": "assistant", "content": answer})
|
| 56 |
else:
|
| 57 |
-
st.error("
|
|
|
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
from llama_cpp import Llama
|
| 4 |
|
| 5 |
+
# 1. Configurazione Interfaccia
|
| 6 |
st.set_page_config(page_title="Limba Mentor", page_icon="🏴")
|
| 7 |
|
| 8 |
+
# Se hai caricato un file chiamato 'logo.png' nella stessa cartella, togli il '#' qui sotto
|
| 9 |
+
# st.image("src/logo.png", width=120)
|
| 10 |
+
|
| 11 |
st.title("Limba Mentor 🏴")
|
| 12 |
st.markdown("### Su mentore tuo in limba sarda")
|
| 13 |
|
| 14 |
+
# 2. Caricamento del Modello GGUF
|
| 15 |
@st.cache_resource
|
| 16 |
def load_model():
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
+
# Scarica il file specifico dal tuo profilo
|
| 19 |
model_path = hf_hub_download(
|
| 20 |
repo_id="FPll/limba-mentor-llama3-gguf",
|
| 21 |
+
filename="Meta-Llama-3.1-8B.Q4_K_M.gguf"
|
| 22 |
)
|
| 23 |
+
# n_threads=2 è il limite ottimale per l'hardware gratuito di HF
|
| 24 |
return Llama(model_path=model_path, n_ctx=2048, n_threads=2)
|
| 25 |
except Exception as e:
|
| 26 |
+
st.error(f"Errore nel caricamento del modello: {e}")
|
| 27 |
return None
|
| 28 |
|
| 29 |
+
with st.spinner("Sto caricando il cervello di Limba..."):
|
| 30 |
llm = load_model()
|
| 31 |
|
| 32 |
+
# 3. Gestione della Chat
|
| 33 |
if "messages" not in st.session_state:
|
| 34 |
st.session_state.messages = []
|
| 35 |
|
| 36 |
+
# Mostra i messaggi della conversazione corrente
|
| 37 |
for message in st.session_state.messages:
|
| 38 |
with st.chat_message(message["role"]):
|
| 39 |
st.markdown(message["content"])
|
| 40 |
|
| 41 |
+
# Input dell'utente
|
| 42 |
if prompt := st.chat_input("Iscrie inoghe..."):
|
| 43 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 44 |
with st.chat_message("user"):
|
| 45 |
st.markdown(prompt)
|
| 46 |
|
| 47 |
+
# Generazione della risposta dell'AI
|
| 48 |
with st.chat_message("assistant"):
|
| 49 |
+
# Formattazione prompt per Llama 3.1
|
| 50 |
full_prompt = f"### Istruzione:\n{prompt}\n\n### Risposta:\n"
|
| 51 |
|
| 52 |
if llm:
|
|
|
|
| 60 |
st.markdown(answer)
|
| 61 |
st.session_state.messages.append({"role": "assistant", "content": answer})
|
| 62 |
else:
|
| 63 |
+
st.error("Il modello non è stato caricato correttamente.")
|