py-rus commited on
Commit
e8007e6
Β·
verified Β·
1 Parent(s): 826603e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -25
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
- # --- 1. LOGIN ---
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 == os.getenv("NOVA_PASSWORD", "admin"):
19
  st.session_state.auth = True
20
  st.rerun()
21
  else:
22
  st.error("Invalid Key")
23
  st.stop()
24
 
25
- # --- 2. FAST MODEL SETTINGS ---
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=512, # Small context = fast CPU speed
32
- n_threads=4, # Uses all available CPU power
33
- n_batch=512,
34
  verbose=False
35
  )
36
 
 
 
 
 
 
 
 
37
  llm = load_nova()
38
 
39
- # --- 3. UI ---
40
  st.title("✨ Nova Coding Assistant")
41
 
 
42
  for message in st.session_state.messages:
43
- with st.chat_message(message["role"]):
 
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 System prompt tells her to STOP the "As an AI" speeches
53
- full_prompt = f"<|im_start|>system\nYou are Nova, a witty, fast, and direct coding assistant. No yapping. No 'As an AI' disclaimers. Just answer the user directly.<|im_end|>\n<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
54
-
55
- # This part makes the words "stream" in real-time
56
- response_container = st.empty()
57
- full_response = ""
58
-
59
- # We loop through the tokens to show them one by one
60
- for chunk in llm(full_prompt, max_tokens=256, stop=["<|im_end|>"], stream=True):
61
- token = chunk["choices"][0]["text"]
62
- full_response += token
63
- response_container.markdown(full_response + "β–Œ")
 
 
 
 
 
 
 
 
 
 
 
 
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})