Spaces:
Sleeping
Sleeping
Commit
·
4efd2d6
1
Parent(s):
1efb174
Partial bot response on user's stop generation
Browse files
app.py
CHANGED
|
@@ -5,15 +5,24 @@ from streamlit import session_state as ss
|
|
| 5 |
import os
|
| 6 |
import login # Importa il file login.py che hai creato
|
| 7 |
|
|
|
|
| 8 |
st.markdown("""
|
| 9 |
<style>
|
| 10 |
-
/* Cambia il colore del testo all'interno del chat input */
|
| 11 |
section[data-testid="stTextInput"] input {
|
| 12 |
-
color: black !important;
|
| 13 |
-
background-color: #F0F2F6 !important;
|
| 14 |
-
font-size: 16px;
|
| 15 |
-
border-radius: 10px;
|
| 16 |
-
padding: 10px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
}
|
| 18 |
</style>
|
| 19 |
""", unsafe_allow_html=True)
|
|
@@ -24,14 +33,13 @@ if "is_logged_in" not in st.session_state:
|
|
| 24 |
|
| 25 |
# Mostra la pagina di login solo se l'utente non è loggato
|
| 26 |
if not st.session_state["is_logged_in"]:
|
| 27 |
-
login.login_page()
|
| 28 |
st.stop()
|
| 29 |
|
| 30 |
# Recupera le secrets da Hugging Face
|
| 31 |
-
model_repo = st.secrets["MODEL_REPO"]
|
| 32 |
-
hf_token = st.secrets["HF_TOKEN"]
|
| 33 |
|
| 34 |
-
# Carica il modello di base con caching
|
| 35 |
@st.cache_resource
|
| 36 |
def load_model():
|
| 37 |
tokenizer = AutoTokenizer.from_pretrained(model_repo, use_auth_token=hf_token)
|
|
@@ -45,9 +53,9 @@ def generate_llama_response_stream(user_input, tokenizer, model, max_length=512)
|
|
| 45 |
input_ids = tokenizer.encode(user_input + eos_token, return_tensors="pt")
|
| 46 |
response_text = ""
|
| 47 |
|
| 48 |
-
response_placeholder = st.empty()
|
| 49 |
|
| 50 |
-
# Genera un token alla volta e aggiorna il
|
| 51 |
for i in range(max_length):
|
| 52 |
if ss.get("stop_generation", False):
|
| 53 |
break # Interrompe il ciclo se l'utente ha premuto "stop"
|
|
@@ -57,7 +65,10 @@ def generate_llama_response_stream(user_input, tokenizer, model, max_length=512)
|
|
| 57 |
new_token = tokenizer.decode([new_token_id], skip_special_tokens=True)
|
| 58 |
|
| 59 |
response_text += new_token
|
| 60 |
-
response_placeholder.markdown(f"
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
# Aggiungi il nuovo token alla sequenza di input
|
| 63 |
input_ids = torch.cat([input_ids, output[:, -1:]], dim=-1)
|
|
@@ -86,39 +97,14 @@ if 'stop_generation' not in ss:
|
|
| 86 |
# Carica il modello e tokenizer
|
| 87 |
tokenizer, model = load_model()
|
| 88 |
|
| 89 |
-
# Aggiungi il CSS per personalizzare il colore del testo e lo sfondo del chat input
|
| 90 |
-
st.markdown("""
|
| 91 |
-
<style>
|
| 92 |
-
/* Sfondo e colore del testo del chat input */
|
| 93 |
-
section[data-testid="stTextInput"] input {
|
| 94 |
-
color: black !important;
|
| 95 |
-
background-color: #F0F2F6 !important;
|
| 96 |
-
border-radius: 10px !important;
|
| 97 |
-
}
|
| 98 |
-
|
| 99 |
-
/* Sfondo scuro per l'intera pagina */
|
| 100 |
-
.main {
|
| 101 |
-
background-color: #0A0A1A;
|
| 102 |
-
color: #FFFFFF;
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
-
/* Modifica sfondo e colore dei messaggi di chat */
|
| 106 |
-
.stChatMessage div[data-baseweb="block"] {
|
| 107 |
-
background-color: rgba(255, 255, 255, 0.1) !important;
|
| 108 |
-
color: #FFFFFF !important;
|
| 109 |
-
border-radius: 10px !important;
|
| 110 |
-
}
|
| 111 |
-
</style>
|
| 112 |
-
""", unsafe_allow_html=True)
|
| 113 |
-
|
| 114 |
# Mostra la cronologia dei messaggi con le label personalizzate
|
| 115 |
for message in ss.msg:
|
| 116 |
if message["role"] == "user":
|
| 117 |
with st.chat_message("user"):
|
| 118 |
-
st.markdown(f"
|
| 119 |
elif message["role"] == "RacoGPT":
|
| 120 |
with st.chat_message("RacoGPT"):
|
| 121 |
-
st.markdown(f"
|
| 122 |
|
| 123 |
# Contenitore per gestire la mutua esclusione tra input e pulsante di stop
|
| 124 |
input_container = st.empty()
|
|
@@ -129,11 +115,10 @@ if not ss.is_chat_input_disabled:
|
|
| 129 |
prompt = st.chat_input("Scrivi il tuo messaggio...")
|
| 130 |
|
| 131 |
if prompt:
|
| 132 |
-
# Salva il messaggio dell'utente e disabilita l'input
|
| 133 |
ss.msg.append({"role": "user", "content": prompt})
|
| 134 |
with st.chat_message("user"):
|
| 135 |
ss.is_chat_input_disabled = True
|
| 136 |
-
st.markdown(f"
|
| 137 |
st.rerun()
|
| 138 |
else:
|
| 139 |
# Mostra il pulsante di "Stop Generazione" al posto della barra di input
|
|
@@ -145,10 +130,16 @@ else:
|
|
| 145 |
with st.spinner("RacoGPT sta generando una risposta..."):
|
| 146 |
response = generate_llama_response_stream(ss.msg[-1]['content'], tokenizer, model)
|
| 147 |
|
| 148 |
-
#
|
| 149 |
-
ss.
|
|
|
|
|
|
|
|
|
|
| 150 |
with st.chat_message("RacoGPT"):
|
| 151 |
-
st.markdown(f"
|
|
|
|
|
|
|
|
|
|
| 152 |
ss.is_chat_input_disabled = False
|
| 153 |
|
| 154 |
# Rerun per aggiornare l'interfaccia
|
|
|
|
| 5 |
import os
|
| 6 |
import login # Importa il file login.py che hai creato
|
| 7 |
|
| 8 |
+
# Stile del chat input e sfondo della pagina
|
| 9 |
st.markdown("""
|
| 10 |
<style>
|
|
|
|
| 11 |
section[data-testid="stTextInput"] input {
|
| 12 |
+
color: black !important;
|
| 13 |
+
background-color: #F0F2F6 !important;
|
| 14 |
+
font-size: 16px;
|
| 15 |
+
border-radius: 10px;
|
| 16 |
+
padding: 10px;
|
| 17 |
+
}
|
| 18 |
+
.main {
|
| 19 |
+
background-color: #0A0A1A;
|
| 20 |
+
color: #FFFFFF;
|
| 21 |
+
}
|
| 22 |
+
.stChatMessage div[data-baseweb="block"] {
|
| 23 |
+
background-color: rgba(255, 255, 255, 0.1) !important;
|
| 24 |
+
color: #FFFFFF !important;
|
| 25 |
+
border-radius: 10px !important;
|
| 26 |
}
|
| 27 |
</style>
|
| 28 |
""", unsafe_allow_html=True)
|
|
|
|
| 33 |
|
| 34 |
# Mostra la pagina di login solo se l'utente non è loggato
|
| 35 |
if not st.session_state["is_logged_in"]:
|
| 36 |
+
login.login_page()
|
| 37 |
st.stop()
|
| 38 |
|
| 39 |
# Recupera le secrets da Hugging Face
|
| 40 |
+
model_repo = st.secrets["MODEL_REPO"]
|
| 41 |
+
hf_token = st.secrets["HF_TOKEN"]
|
| 42 |
|
|
|
|
| 43 |
@st.cache_resource
|
| 44 |
def load_model():
|
| 45 |
tokenizer = AutoTokenizer.from_pretrained(model_repo, use_auth_token=hf_token)
|
|
|
|
| 53 |
input_ids = tokenizer.encode(user_input + eos_token, return_tensors="pt")
|
| 54 |
response_text = ""
|
| 55 |
|
| 56 |
+
response_placeholder = st.empty()
|
| 57 |
|
| 58 |
+
# Genera un token alla volta e aggiorna il testo in response_text
|
| 59 |
for i in range(max_length):
|
| 60 |
if ss.get("stop_generation", False):
|
| 61 |
break # Interrompe il ciclo se l'utente ha premuto "stop"
|
|
|
|
| 65 |
new_token = tokenizer.decode([new_token_id], skip_special_tokens=True)
|
| 66 |
|
| 67 |
response_text += new_token
|
| 68 |
+
response_placeholder.markdown(f"RacoGPT: {response_text}", unsafe_allow_html=True)
|
| 69 |
+
|
| 70 |
+
# Salva il testo parziale in session_state per preservarlo in caso di interruzione
|
| 71 |
+
ss["response_text_partial"] = response_text
|
| 72 |
|
| 73 |
# Aggiungi il nuovo token alla sequenza di input
|
| 74 |
input_ids = torch.cat([input_ids, output[:, -1:]], dim=-1)
|
|
|
|
| 97 |
# Carica il modello e tokenizer
|
| 98 |
tokenizer, model = load_model()
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
# Mostra la cronologia dei messaggi con le label personalizzate
|
| 101 |
for message in ss.msg:
|
| 102 |
if message["role"] == "user":
|
| 103 |
with st.chat_message("user"):
|
| 104 |
+
st.markdown(f"Tu: {message['content']}")
|
| 105 |
elif message["role"] == "RacoGPT":
|
| 106 |
with st.chat_message("RacoGPT"):
|
| 107 |
+
st.markdown(f"RacoGPT: {message['content']}")
|
| 108 |
|
| 109 |
# Contenitore per gestire la mutua esclusione tra input e pulsante di stop
|
| 110 |
input_container = st.empty()
|
|
|
|
| 115 |
prompt = st.chat_input("Scrivi il tuo messaggio...")
|
| 116 |
|
| 117 |
if prompt:
|
|
|
|
| 118 |
ss.msg.append({"role": "user", "content": prompt})
|
| 119 |
with st.chat_message("user"):
|
| 120 |
ss.is_chat_input_disabled = True
|
| 121 |
+
st.markdown(f"Tu: {prompt}")
|
| 122 |
st.rerun()
|
| 123 |
else:
|
| 124 |
# Mostra il pulsante di "Stop Generazione" al posto della barra di input
|
|
|
|
| 130 |
with st.spinner("RacoGPT sta generando una risposta..."):
|
| 131 |
response = generate_llama_response_stream(ss.msg[-1]['content'], tokenizer, model)
|
| 132 |
|
| 133 |
+
# Usa il testo parziale se presente
|
| 134 |
+
final_response = response or ss.get("response_text_partial", "")
|
| 135 |
+
|
| 136 |
+
# Aggiungi la risposta finale nella cronologia dei messaggi
|
| 137 |
+
ss.msg.append({"role": "RacoGPT", "content": final_response})
|
| 138 |
with st.chat_message("RacoGPT"):
|
| 139 |
+
st.markdown(f"RacoGPT: {final_response}")
|
| 140 |
+
|
| 141 |
+
# Pulisce il testo parziale dalla sessione e riabilita l'input
|
| 142 |
+
ss.pop("response_text_partial", None)
|
| 143 |
ss.is_chat_input_disabled = False
|
| 144 |
|
| 145 |
# Rerun per aggiornare l'interfaccia
|