sourize commited on
Commit
b7c1ede
Β·
verified Β·
1 Parent(s): 93c4241

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -48
app.py CHANGED
@@ -2,75 +2,70 @@ import os
2
  import streamlit as st
3
  from huggingface_hub import InferenceClient
4
 
5
- # ── Configuration ──────────────────────────────────────────────────────────
6
- HF_TOKEN = os.getenv("HF_TOKEN") # set this in your Space Secrets
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 lines beginning with 'User:' or 'Assistant:'.\n"
15
  "If you don't know, say \"I don't know.\"\n"
16
  )
17
 
18
- # ── Initialize Hugging Face Inference Client ─────────────────────────────────
19
- client = InferenceClient(token=HF_TOKEN)
20
 
21
- def query_hf(prompt: str) -> str:
22
- # model_id and prompt are positional args, params passed via params=
23
- response = client.text_generation(
24
- MODEL_ID,
25
- prompt,
26
- params={
27
- "max_new_tokens": MAX_NEW_TOKENS,
28
- "do_sample": True,
29
- "temperature": 0.7,
30
- "top_p": 0.9,
31
- "return_full_text": False,
32
- },
33
- )
34
- text = response.generated_text.strip()
35
- # Remove stray markers
36
- for marker in ("User:", "Assistant:"):
37
- if marker in text:
38
- text = text.split(marker)[0].strip()
39
- return text or "I don't know."
40
-
41
- # ── Streamlit UI ──────────────────────────────────────────────────────────
42
  st.set_page_config(layout="centered")
43
- st.title("🧠 DeepTalks (Inference API)")
44
- st.subheader("Your personal AI Companion")
45
 
46
  if "history" not in st.session_state:
47
- st.session_state.history = [] # list of (role, text)
48
 
49
- # Render chat history
50
- for role, text in st.session_state.history:
51
- st.chat_message("user" if role == "You" else "assistant").write(text)
52
 
53
- # User input
54
- user_input = st.chat_input("Type your message…")
55
  if user_input:
56
  st.chat_message("user").write(user_input)
57
  st.session_state.history.append(("You", user_input))
58
 
59
- # Build context from last turns
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"
64
- f"Context:\n{context}\n\n"
65
- f"User: {user_input}\nAssistant:"
66
- )
67
 
68
  with st.spinner("Thinking…"):
69
  try:
70
- answer = query_hf(prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  except Exception as e:
 
72
  st.error(f"Inference API error: {e}")
73
- answer = "I’m sorry, something went wrong."
74
 
75
- st.chat_message("assistant").write(answer)
76
- st.session_state.history.append(("Bot", answer))
 
2
  import streamlit as st
3
  from huggingface_hub import InferenceClient
4
 
5
+ # ── Config ─────────────────────────────────────────────────────────
6
+ MODEL_ID = "sourize/phi2-memory-lora"
7
+ HF_TOKEN = os.getenv("HF_TOKEN") # Set this in your Space secrets
8
+ CONTEXT_TURNS = 6
9
  MAX_NEW_TOKENS = 128
10
+ TEMPERATURE = 0.7
11
+ TOP_P = 0.9
12
 
13
  SYSTEM_PROMPT = (
14
+ "You are a helpful assistant for DeepTalks with base Phi-2\n"
15
+ "fine-tuned by Sourish for domain support.\n"
16
  "Answer **only** using the conversation context below.\n"
17
+ "Do NOT output any lines beginning with 'User:' or 'Assistant:'.\n"
18
  "If you don't know, say \"I don't know.\"\n"
19
  )
20
 
21
+ client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
 
22
 
23
+ # ── UI Setup ────────────────────────────────────────────────────────
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  st.set_page_config(layout="centered")
25
+ st.title("🧠 DeepTalks")
26
+ st.subheader("Your personal AI Companion", divider="grey")
27
 
28
  if "history" not in st.session_state:
29
+ st.session_state.history = []
30
 
31
+ # ── Show Previous Messages ─────────────────────────────────────────
32
+ for role, message in st.session_state.history:
33
+ st.chat_message("user" if role == "You" else "assistant").write(message)
34
 
35
+ # ── New User Input ─────────────────────────────────────────────────
36
+ user_input = st.chat_input("Your message…")
37
  if user_input:
38
  st.chat_message("user").write(user_input)
39
  st.session_state.history.append(("You", user_input))
40
 
41
+ # Prepare context
42
  recent = st.session_state.history[-CONTEXT_TURNS*2:]
43
+ context = "\n".join([f"{role}: {text}" for role, text in recent])
44
+ prompt = f"{SYSTEM_PROMPT}\n\nContext:\n{context}\n\nUser: {user_input}\nAssistant:"
 
 
 
 
45
 
46
  with st.spinner("Thinking…"):
47
  try:
48
+ response = client.text_generation(
49
+ prompt,
50
+ max_new_tokens=MAX_NEW_TOKENS,
51
+ temperature=TEMPERATURE,
52
+ top_p=TOP_P,
53
+ do_sample=True,
54
+ return_full_text=False,
55
+ )
56
+
57
+ # Clean up
58
+ reply = response.strip()
59
+ for marker in ["User:", "Assistant:"]:
60
+ if marker in reply:
61
+ reply = reply.split(marker)[0].strip()
62
+
63
+ if not reply:
64
+ reply = "I’m sorry, I didn’t catch that. Could you rephrase?"
65
+
66
  except Exception as e:
67
+ reply = "I’m sorry, something went wrong."
68
  st.error(f"Inference API error: {e}")
 
69
 
70
+ st.chat_message("assistant").write(reply)
71
+ st.session_state.history.append(("Bot", reply))