| import streamlit as st |
| from huggingface_hub import hf_hub_download |
| from llama_cpp import Llama |
|
|
| |
| st.set_page_config( |
| page_title="Limba 2.0", |
| page_icon="https://upload.wikimedia.org/wikipedia/commons/6/65/Flag_of_Sardinia.svg" |
| ) |
|
|
| |
| |
| try: |
| st.image("logo.png", width=150) |
| except: |
| pass |
|
|
| |
| st.title("Limba 2.0") |
| st.markdown("### Su mentore tuo in limba sarda") |
|
|
| |
| @st.cache_resource |
| def load_model(): |
| try: |
| |
| model_path = hf_hub_download( |
| repo_id="FPll/limba-mentor-llama3-gguf", |
| filename="Meta-Llama-3.1-8B.Q4_K_M.gguf" |
| ) |
| |
| return Llama(model_path=model_path, n_ctx=2048, n_threads=2) |
| except Exception as e: |
| st.error(f"Errore nel caricamento del modello: {e}") |
| return None |
|
|
| with st.spinner("Sto caricando Limba 2.0..."): |
| llm = load_model() |
|
|
| |
| if "messages" not in st.session_state: |
| st.session_state.messages = [] |
|
|
| |
| for message in st.session_state.messages: |
| with st.chat_message(message["role"]): |
| st.markdown(message["content"]) |
|
|
| |
| if prompt := st.chat_input("Iscrie inoghe..."): |
| st.session_state.messages.append({"role": "user", "content": prompt}) |
| with st.chat_message("user"): |
| st.markdown(prompt) |
|
|
| |
| with st.chat_message("assistant"): |
| |
| full_prompt = f"### Istruzione:\n{prompt}\n\n### Risposta:\n" |
| |
| if llm: |
| response = llm( |
| full_prompt, |
| max_tokens=512, |
| stop=["###", "<|end_of_text|>", "</s>"], |
| echo=False |
| ) |
| answer = response["choices"][0]["text"].strip() |
| st.markdown(answer) |
| st.session_state.messages.append({"role": "assistant", "content": answer}) |
| else: |
| st.error("Il modello non è stato caricato correttamente.") |