Update src/streamlit_app.py
Browse files- src/streamlit_app.py +56 -39
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,57 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
+
|
| 5 |
+
# Configurazione interfaccia
|
| 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. Scarica e carica il modello dal tuo profilo FPll
|
| 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="unsloth.Q4_K_M.gguf"
|
| 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 download del modello: {e}")
|
| 24 |
+
return None
|
| 25 |
+
|
| 26 |
+
with st.spinner("Sto pensando..."):
|
| 27 |
+
llm = load_model()
|
| 28 |
+
|
| 29 |
+
# 3. Gestione della chat
|
| 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:
|
| 47 |
+
response = llm(
|
| 48 |
+
full_prompt,
|
| 49 |
+
max_tokens=512,
|
| 50 |
+
stop=["###", "<|end_of_text|>", "</s>"],
|
| 51 |
+
echo=False
|
| 52 |
+
)
|
| 53 |
+
answer = response["choices"][0]["text"].strip()
|
| 54 |
+
st.markdown(answer)
|
| 55 |
+
st.session_state.messages.append({"role": "assistant", "content": answer})
|
| 56 |
+
else:
|
| 57 |
+
st.error("Modello non caricato. Controlla il nome del file GGUF.")
|