FPll commited on
Commit
2f61e1e
·
verified ·
1 Parent(s): 9209ce9

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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.")