sourize commited on
Commit
4f73f4a
Β·
verified Β·
1 Parent(s): b7c1ede

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -39
app.py CHANGED
@@ -1,14 +1,14 @@
1
  import os
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"
@@ -18,54 +18,73 @@ SYSTEM_PROMPT = (
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))
 
1
  import os
2
  import streamlit as st
3
+ from huggingface_hub import InferenceApi
4
 
5
+ # ── Configuration ──────────────────────────────────────────────────────────
6
+ MODEL_ID = "sourize/phi2-memory-lora"
7
+ HF_TOKEN = os.getenv("HF_TOKEN") # set 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"
 
18
  "If you don't know, say \"I don't know.\"\n"
19
  )
20
 
21
+ # ── Initialize the low-level Inference API ─────────────────────────────────
22
+ inference = InferenceApi(
23
+ repo_id=MODEL_ID,
24
+ token=HF_TOKEN,
25
+ task="text-generation"
26
+ )
27
+
28
+ def query_hf(prompt: str) -> str:
29
+ """
30
+ Call the text-generation endpoint via InferenceApi.
31
+ """
32
+ outputs = inference(
33
+ inputs=prompt,
34
+ parameters={
35
+ "max_new_tokens": MAX_NEW_TOKENS,
36
+ "temperature": TEMPERATURE,
37
+ "top_p": TOP_P,
38
+ "do_sample": True,
39
+ "return_full_text": False
40
+ }
41
+ )
42
+ # outputs is a list of dicts: [{"generated_text": "..."}]
43
+ text = outputs[0]["generated_text"].strip()
44
+ # strip any stray markers
45
+ for marker in ("User:", "Assistant:"):
46
+ if marker in text:
47
+ text = text.split(marker)[0].strip()
48
+ return text or "I don't know."
49
 
50
+ # ── Streamlit UI ──────────────────────────────────────────────────────────
51
  st.set_page_config(layout="centered")
52
+ st.title("🧠 DeepTalks (Inference API)")
53
+ st.subheader("Your personal AI Companion")
54
 
55
  if "history" not in st.session_state:
56
+ st.session_state.history = [] # list of (role, text)
57
 
58
+ # Render previous messages
59
+ for role, msg in st.session_state.history:
60
+ st.chat_message("user" if role=="You" else "assistant").write(msg)
61
 
62
+ # New user input
63
+ user_input = st.chat_input("Type your message…")
64
  if user_input:
65
+ # Display and record user
66
  st.chat_message("user").write(user_input)
67
  st.session_state.history.append(("You", user_input))
68
 
69
+ # Build context of last 2*CONTEXT_TURNS entries
70
  recent = st.session_state.history[-CONTEXT_TURNS*2:]
71
+ context = "\n".join(text for _, text in recent)
 
72
 
73
+ prompt = (
74
+ f"{SYSTEM_PROMPT}\n\n"
75
+ f"Context:\n{context}\n\n"
76
+ f"User: {user_input}\n"
77
+ f"Assistant:"
78
+ )
79
+
80
+ # Call Hugging Face Inference API
81
  with st.spinner("Thinking…"):
82
  try:
83
+ reply = query_hf(prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  except Exception as e:
 
85
  st.error(f"Inference API error: {e}")
86
+ reply = "I’m sorry, something went wrong."
87
 
88
+ # Display and record assistant
89
  st.chat_message("assistant").write(reply)
90
  st.session_state.history.append(("Bot", reply))