sourize commited on
Commit
02f80ba
Β·
verified Β·
1 Parent(s): a6691ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import os
2
  import streamlit as st
3
  import torch
@@ -27,7 +29,7 @@ embedder = get_embedder()
27
  def fetch_mems(query, k=3):
28
  vec = embedder.encode(query).astype("float32").tolist()
29
  return supabase.rpc(
30
- "match_memories",
31
  {"query_embedding": vec, "match_count": k}
32
  ).execute().data
33
 
@@ -65,8 +67,7 @@ def load_generator():
65
  device_map="auto"
66
  )
67
  else:
68
- # CPU or no CUDA: use fp16 if available, else fp32
69
- dtype = torch.float16 if torch.cuda.is_available() or torch.cuda.device_count()>0 else torch.float32
70
  base = AutoModelForCausalLM.from_pretrained(
71
  "microsoft/phi-2",
72
  trust_remote_code=True,
@@ -100,13 +101,14 @@ tokenizer, generator = load_generator()
100
 
101
  # ── System prompt to reduce hallucinations ──────────────────────────────────
102
  SYSTEM = (
103
- "You are a helpful assistant.\n"
 
104
  "Answer **only** using the information in the memory below.\n"
105
  "If the answer is not in memory, reply: \"I don't know.\"\n"
106
  )
107
 
108
  # ── Streamlit UI ──────────────────────────────────────────────────────────
109
- st.set_page_config(layout="wide")
110
  st.title("🧠 Memory-Aware Phi-2 Chat")
111
 
112
  if "history" not in st.session_state:
@@ -114,21 +116,20 @@ if "history" not in st.session_state:
114
 
115
  # Render existing chat history
116
  for role, msg in st.session_state.history:
117
- st.chat_message("user" if role=="You" else "assistant").write(msg)
118
 
119
  # Input box at the bottom
120
  user_input = st.chat_input("Type your message...")
121
 
122
  if user_input:
123
- # Append user message
 
124
  st.session_state.history.append(("You", user_input))
125
  add_mem("user", user_input)
126
 
127
- # Retrieve relevant memories
128
  mems = fetch_mems(user_input, k=3)
129
  mem_block = "\n".join(f"{m['speaker']}: {m['text']}" for m in mems)
130
-
131
- # Build prompt
132
  prompt = f"""{SYSTEM}
133
 
134
  Memory:
@@ -137,7 +138,7 @@ Memory:
137
  User: {user_input}
138
  Assistant:"""
139
 
140
- # Generate reply with spinner
141
  with st.spinner("Thinking..."):
142
  try:
143
  out = generator(prompt)[0]["generated_text"].strip()
@@ -145,6 +146,7 @@ Assistant:"""
145
  out = "Sorry, I encountered an error."
146
  st.error(f"Generation error: {e}")
147
 
148
- # Append assistant reply
 
149
  st.session_state.history.append(("Bot", out))
150
  add_mem("assistant", out)
 
1
+ # app.py
2
+
3
  import os
4
  import streamlit as st
5
  import torch
 
29
  def fetch_mems(query, k=3):
30
  vec = embedder.encode(query).astype("float32").tolist()
31
  return supabase.rpc(
32
+ "match_memories",
33
  {"query_embedding": vec, "match_count": k}
34
  ).execute().data
35
 
 
67
  device_map="auto"
68
  )
69
  else:
70
+ dtype = torch.float16 if torch.cuda.is_available() else torch.float32
 
71
  base = AutoModelForCausalLM.from_pretrained(
72
  "microsoft/phi-2",
73
  trust_remote_code=True,
 
101
 
102
  # ── System prompt to reduce hallucinations ──────────────────────────────────
103
  SYSTEM = (
104
+ "You are a helpful assistant for DeepTalks with a base model as Phi-2 and fine tuned by Sourish for my domain specific role.\n"
105
+ "My domain is assisting you within my expertise by listening to you understanding you & supporting you.\n"
106
  "Answer **only** using the information in the memory below.\n"
107
  "If the answer is not in memory, reply: \"I don't know.\"\n"
108
  )
109
 
110
  # ── Streamlit UI ──────────────────────────────────────────────────────────
111
+ st.set_page_config(layout="centered")
112
  st.title("🧠 Memory-Aware Phi-2 Chat")
113
 
114
  if "history" not in st.session_state:
 
116
 
117
  # Render existing chat history
118
  for role, msg in st.session_state.history:
119
+ st.chat_message("user" if role == "You" else "assistant").write(msg)
120
 
121
  # Input box at the bottom
122
  user_input = st.chat_input("Type your message...")
123
 
124
  if user_input:
125
+ # 1) Show and store user message
126
+ st.chat_message("user").write(user_input)
127
  st.session_state.history.append(("You", user_input))
128
  add_mem("user", user_input)
129
 
130
+ # 2) Retrieve memories & build prompt
131
  mems = fetch_mems(user_input, k=3)
132
  mem_block = "\n".join(f"{m['speaker']}: {m['text']}" for m in mems)
 
 
133
  prompt = f"""{SYSTEM}
134
 
135
  Memory:
 
138
  User: {user_input}
139
  Assistant:"""
140
 
141
+ # 3) Generate reply with spinner
142
  with st.spinner("Thinking..."):
143
  try:
144
  out = generator(prompt)[0]["generated_text"].strip()
 
146
  out = "Sorry, I encountered an error."
147
  st.error(f"Generation error: {e}")
148
 
149
+ # 4) Show and store assistant reply
150
+ st.chat_message("assistant").write(out)
151
  st.session_state.history.append(("Bot", out))
152
  add_mem("assistant", out)