sourize commited on
Commit
433d28b
Β·
verified Β·
1 Parent(s): df4e3a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -29
app.py CHANGED
@@ -3,25 +3,25 @@ import streamlit as st
3
  from huggingface_hub import InferenceClient
4
 
5
  # ── Configuration ──────────────────────────────────────────────────────────
6
- HF_TOKEN = os.getenv("HF_TOKEN") # store your token in Space Secrets
7
- MODEL_ID = "sourize/phi2-memory-lora"
8
- CONTEXT_TURNS = 7
9
- MAX_NEW_TOKENS = 128
10
- SYSTEM_PROMPT = (
11
- "You are a helpful assistant for DeepTalks with base Phi-2 "
12
- "fine-tuned by Sourish.\n"
13
  "Answer **only** using the conversation context below.\n"
14
  "Do NOT output any lines beginning with 'User:' or 'Assistant:'.\n"
15
  "If you don't know, say \"I don't know.\"\n"
16
  )
17
 
18
- # ── HF Inference client ─────────────────────────────────────────────────────
19
  client = InferenceClient(token=HF_TOKEN)
20
 
21
  def query_hf(prompt: str) -> str:
22
- out = client.text_generation(
23
  model=MODEL_ID,
24
- inputs=prompt,
25
  parameters={
26
  "max_new_tokens": MAX_NEW_TOKENS,
27
  "do_sample": True,
@@ -30,47 +30,49 @@ def query_hf(prompt: str) -> str:
30
  "return_full_text": False
31
  },
32
  )
33
- text = out.generated_text.strip()
34
- # strip any stray markers
35
- for marker in ["User:", "Assistant:"]:
36
  if marker in text:
37
  text = text.split(marker)[0].strip()
38
  return text or "I don't know."
39
 
40
  # ── Streamlit UI ──────────────────────────────────────────────────────────
41
  st.set_page_config(layout="centered")
42
- st.title("🧠 DeepTalks (Inference API)")
43
- st.subheader("Your personal AI Companion")
44
 
45
  if "history" not in st.session_state:
46
- st.session_state.history = [] # tuples of (role, text)
47
 
48
- # render history
49
- for role, text in st.session_state.history:
50
- st.chat_message("user" if role=="You" else "assistant").write(text)
51
 
52
- # new input
53
  user_input = st.chat_input("Type your message…")
54
  if user_input:
 
55
  st.chat_message("user").write(user_input)
56
  st.session_state.history.append(("You", user_input))
57
 
58
  # build context
59
  recent = st.session_state.history[-CONTEXT_TURNS*2:]
60
- ctx = "\n".join(text for _, text in recent)
61
- prompt = (
62
  f"{SYSTEM_PROMPT}\n\n"
63
- f"Context:\n{ctx}\n\n"
64
  f"User: {user_input}\nAssistant:"
65
  )
66
 
67
- # call HF Inference API
68
  with st.spinner("Thinking…"):
69
  try:
70
- reply = query_hf(prompt)
71
  except Exception as e:
72
- reply = "Error generating response."
73
- st.error(e)
74
 
75
- st.chat_message("assistant").write(reply)
76
- st.session_state.history.append(("Bot", reply))
 
 
3
  from huggingface_hub import InferenceClient
4
 
5
  # ── Configuration ──────────────────────────────────────────────────────────
6
+ HF_TOKEN = os.getenv("HF_TOKEN")
7
+ MODEL_ID = "sourize/phi2-memory-lora"
8
+ CONTEXT_TURNS = 6
9
+ MAX_NEW_TOKENS = 128
10
+
11
+ SYSTEM_PROMPT = (
12
+ "You are a helpful assistant for DeepTalks, based on Phi-2 fine-tuned by Sourish.\n"
13
  "Answer **only** using the conversation context below.\n"
14
  "Do NOT output any lines beginning with 'User:' or 'Assistant:'.\n"
15
  "If you don't know, say \"I don't know.\"\n"
16
  )
17
 
18
+ # ── Initialize the HF Inference client ─────────────────────────────────────
19
  client = InferenceClient(token=HF_TOKEN)
20
 
21
  def query_hf(prompt: str) -> str:
22
+ resp = client.text_generation(
23
  model=MODEL_ID,
24
+ prompt=prompt,
25
  parameters={
26
  "max_new_tokens": MAX_NEW_TOKENS,
27
  "do_sample": True,
 
30
  "return_full_text": False
31
  },
32
  )
33
+ text = resp.generated_text.strip()
34
+ # strip any accidental echoes
35
+ for marker in ("User:", "Assistant:"):
36
  if marker in text:
37
  text = text.split(marker)[0].strip()
38
  return text or "I don't know."
39
 
40
  # ── Streamlit UI ──────────────────────────────────────────────────────────
41
  st.set_page_config(layout="centered")
42
+ st.title("🧠 DeepTalks")
43
+ st.subheader("Your personal AI Companion", divider='grey')
44
 
45
  if "history" not in st.session_state:
46
+ st.session_state.history = [] # list of (role, text)
47
 
48
+ # render past messages
49
+ for role, txt in st.session_state.history:
50
+ st.chat_message("user" if role == "You" else "assistant").write(txt)
51
 
52
+ # new user input
53
  user_input = st.chat_input("Type your message…")
54
  if user_input:
55
+ # display & store user
56
  st.chat_message("user").write(user_input)
57
  st.session_state.history.append(("You", user_input))
58
 
59
  # build context
60
  recent = st.session_state.history[-CONTEXT_TURNS*2:]
61
+ context = "\n".join(msg for _, msg in recent)
62
+ prompt = (
63
  f"{SYSTEM_PROMPT}\n\n"
64
+ f"Context:\n{context}\n\n"
65
  f"User: {user_input}\nAssistant:"
66
  )
67
 
68
+ # call the HF Inference API
69
  with st.spinner("Thinking…"):
70
  try:
71
+ answer = query_hf(prompt)
72
  except Exception as e:
73
+ st.error(f"Inference API error: {e}")
74
+ answer = "I’m sorry, something went wrong."
75
 
76
+ # display & store assistant
77
+ st.chat_message("assistant").write(answer)
78
+ st.session_state.history.append(("Bot", answer))