Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,66 +2,100 @@ import streamlit as st
|
|
| 2 |
import os
|
| 3 |
from llama_cpp import Llama
|
| 4 |
|
|
|
|
| 5 |
st.set_page_config(page_title="Nova AI", page_icon="β¨", layout="wide")
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
if "auth" not in st.session_state:
|
| 8 |
st.session_state.auth = False
|
| 9 |
if "messages" not in st.session_state:
|
| 10 |
st.session_state.messages = []
|
| 11 |
|
| 12 |
-
#
|
| 13 |
if not st.session_state.auth:
|
| 14 |
st.title("π Nova Secure Entry")
|
| 15 |
with st.form("login"):
|
|
|
|
|
|
|
| 16 |
pwd = st.text_input("Access Key", type="password")
|
| 17 |
-
if st.form_submit_button("Unlock"):
|
| 18 |
-
if pwd ==
|
| 19 |
st.session_state.auth = True
|
| 20 |
st.rerun()
|
| 21 |
else:
|
| 22 |
st.error("Invalid Key")
|
| 23 |
st.stop()
|
| 24 |
|
| 25 |
-
#
|
| 26 |
@st.cache_resource
|
| 27 |
def load_nova():
|
| 28 |
return Llama.from_pretrained(
|
| 29 |
repo_id="HuggingFaceTB/SmolLM2-1.7B-Instruct-GGUF",
|
| 30 |
filename="*q4_k_m.gguf",
|
| 31 |
-
n_ctx=
|
| 32 |
-
n_threads=4,
|
| 33 |
-
n_batch=512,
|
| 34 |
verbose=False
|
| 35 |
)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
llm = load_nova()
|
| 38 |
|
| 39 |
-
#
|
| 40 |
st.title("β¨ Nova Coding Assistant")
|
| 41 |
|
|
|
|
| 42 |
for message in st.session_state.messages:
|
| 43 |
-
|
|
|
|
| 44 |
st.markdown(message["content"])
|
| 45 |
|
|
|
|
| 46 |
if prompt := st.chat_input("Ask Nova..."):
|
|
|
|
| 47 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 48 |
-
with st.chat_message("user"):
|
| 49 |
st.markdown(prompt)
|
| 50 |
|
| 51 |
-
with st.chat_message("assistant"):
|
| 52 |
-
# The
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
|
|
|
| 65 |
response_container.markdown(full_response)
|
| 66 |
-
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 67 |
-
|
|
|
|
| 2 |
import os
|
| 3 |
from llama_cpp import Llama
|
| 4 |
|
| 5 |
+
# 1. PAGE SETUP & MOBILE STYLING
|
| 6 |
st.set_page_config(page_title="Nova AI", page_icon="β¨", layout="wide")
|
| 7 |
|
| 8 |
+
st.markdown("""
|
| 9 |
+
<style>
|
| 10 |
+
div.block-container{padding-top:2rem;}
|
| 11 |
+
.stChatFloatingInputContainer{padding-bottom:1rem;}
|
| 12 |
+
</style>
|
| 13 |
+
""", unsafe_allow_html=True)
|
| 14 |
+
|
| 15 |
+
# 2. SESSION STATE (The App's Memory)
|
| 16 |
if "auth" not in st.session_state:
|
| 17 |
st.session_state.auth = False
|
| 18 |
if "messages" not in st.session_state:
|
| 19 |
st.session_state.messages = []
|
| 20 |
|
| 21 |
+
# 3. SECURE LOGIN
|
| 22 |
if not st.session_state.auth:
|
| 23 |
st.title("π Nova Secure Entry")
|
| 24 |
with st.form("login"):
|
| 25 |
+
# Access your password from Settings > Secrets
|
| 26 |
+
SECRET_PASSWORD = os.getenv("NOVA_PASSWORD", "admin")
|
| 27 |
pwd = st.text_input("Access Key", type="password")
|
| 28 |
+
if st.form_submit_button("Unlock Nova"):
|
| 29 |
+
if pwd == SECRET_PASSWORD:
|
| 30 |
st.session_state.auth = True
|
| 31 |
st.rerun()
|
| 32 |
else:
|
| 33 |
st.error("Invalid Key")
|
| 34 |
st.stop()
|
| 35 |
|
| 36 |
+
# 4. FAST MODEL SETTINGS (Optimized for 16GB RAM)
|
| 37 |
@st.cache_resource
|
| 38 |
def load_nova():
|
| 39 |
return Llama.from_pretrained(
|
| 40 |
repo_id="HuggingFaceTB/SmolLM2-1.7B-Instruct-GGUF",
|
| 41 |
filename="*q4_k_m.gguf",
|
| 42 |
+
n_ctx=2048, # Large memory for long scripts
|
| 43 |
+
n_threads=4, # Max CPU power
|
| 44 |
+
n_batch=512, # Efficient processing
|
| 45 |
verbose=False
|
| 46 |
)
|
| 47 |
|
| 48 |
+
with st.sidebar:
|
| 49 |
+
st.title("β§ Nova Cloud-Alpha")
|
| 50 |
+
st.info("Status: Online | Secure")
|
| 51 |
+
if st.button("Clear Chat History"):
|
| 52 |
+
st.session_state.messages = []
|
| 53 |
+
st.rerun()
|
| 54 |
+
|
| 55 |
llm = load_nova()
|
| 56 |
|
| 57 |
+
# 5. CHAT INTERFACE
|
| 58 |
st.title("β¨ Nova Coding Assistant")
|
| 59 |
|
| 60 |
+
# Display previous messages with custom avatars
|
| 61 |
for message in st.session_state.messages:
|
| 62 |
+
avatar_sym = "< >" if message["role"] == "user" else "β¨"
|
| 63 |
+
with st.chat_message(message["role"], avatar=avatar_sym):
|
| 64 |
st.markdown(message["content"])
|
| 65 |
|
| 66 |
+
# 6. AI GENERATION (With Status Bar & Streaming)
|
| 67 |
if prompt := st.chat_input("Ask Nova..."):
|
| 68 |
+
# Add your message to history
|
| 69 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 70 |
+
with st.chat_message("user", avatar="< >"):
|
| 71 |
st.markdown(prompt)
|
| 72 |
|
| 73 |
+
with st.chat_message("assistant", avatar="β¨"):
|
| 74 |
+
# The Status Bar
|
| 75 |
+
with st.status("β¨ Nova is processing...", expanded=True) as status:
|
| 76 |
+
st.write("Initializing neural links...")
|
| 77 |
+
|
| 78 |
+
system_instr = "You are Nova, a witty, fast, and direct coding assistant. No yapping. No 'As an AI' disclaimers. Answer concisely."
|
| 79 |
+
full_prompt = f"<|im_start|>system\n{system_instr}<|im_end|>\n<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
|
| 80 |
+
|
| 81 |
+
response_container = st.empty()
|
| 82 |
+
full_response = ""
|
| 83 |
+
|
| 84 |
+
st.write("Generating code...")
|
| 85 |
+
|
| 86 |
+
# Streaming the response word-by-word
|
| 87 |
+
for chunk in llm(
|
| 88 |
+
full_prompt,
|
| 89 |
+
max_tokens=1024, # High lung capacity for long code
|
| 90 |
+
stop=["<|im_end|>", "User:"],
|
| 91 |
+
stream=True
|
| 92 |
+
):
|
| 93 |
+
token = chunk["choices"][0]["text"]
|
| 94 |
+
full_response += token
|
| 95 |
+
response_container.markdown(full_response + "β") # Cursor effect
|
| 96 |
+
|
| 97 |
+
status.update(label="β
Nova Ready", state="complete", expanded=False)
|
| 98 |
|
| 99 |
+
# Final cleanup and save
|
| 100 |
response_container.markdown(full_response)
|
| 101 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
|
|