sourize commited on
Commit
ba2f041
Β·
verified Β·
1 Parent(s): bd7ea55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -2,8 +2,8 @@ import os
2
  import streamlit as st
3
  import requests
4
 
5
- # ── Config ────────────────────────────────────────────────────────────────
6
- HF_TOKEN = os.getenv("HF_TOKEN")
7
  MODEL_ID = "sourize/phi2-memory-lora"
8
  CONTEXT_TURNS = 6
9
 
@@ -32,22 +32,24 @@ def query_hf(prompt: str) -> str:
32
  }
33
  r = requests.post(API_URL, headers=HEADERS, json=payload, timeout=60)
34
  r.raise_for_status()
35
- out = r.json()[0]["generated_text"].strip()
 
36
  for m in ("User:", "Assistant:"):
37
- if m in out:
38
- out = out.split(m)[0].strip()
39
- return out or "I don't know."
40
 
41
  # ── Streamlit UI ──────────────────────────────────────────────────────────
42
  st.set_page_config(layout="centered")
43
- st.title("🧠 DeepTalks (API)")
44
  st.subheader("Your personal AI Companion")
45
 
46
  if "history" not in st.session_state:
47
  st.session_state.history = []
48
 
49
- for role, txt in st.session_state.history:
50
- st.chat_message("user" if role=="You" else "assistant").write(txt)
 
51
 
52
  user_input = st.chat_input("Type your message…")
53
  if user_input:
@@ -55,7 +57,7 @@ if user_input:
55
  st.session_state.history.append(("You", user_input))
56
 
57
  recent = st.session_state.history[-CONTEXT_TURNS*2:]
58
- context = "\n".join(msg for _, msg in recent)
59
  prompt = (
60
  f"{SYSTEM_PROMPT}\n\n"
61
  f"Context:\n{context}\n\n"
 
2
  import streamlit as st
3
  import requests
4
 
5
+ # ── Configuration ──────────────────────────────────────────────────────────
6
+ HF_TOKEN = os.getenv("HF_TOKEN") # read‐only token in Space secrets
7
  MODEL_ID = "sourize/phi2-memory-lora"
8
  CONTEXT_TURNS = 6
9
 
 
32
  }
33
  r = requests.post(API_URL, headers=HEADERS, json=payload, timeout=60)
34
  r.raise_for_status()
35
+ data = r.json()
36
+ text = data[0]["generated_text"].strip()
37
  for m in ("User:", "Assistant:"):
38
+ if m in text:
39
+ text = text.split(m)[0].strip()
40
+ return text or "I don't know."
41
 
42
  # ── Streamlit UI ──────────────────────────────────────────────────────────
43
  st.set_page_config(layout="centered")
44
+ st.title("🧠 DeepTalks (Inference API)")
45
  st.subheader("Your personal AI Companion")
46
 
47
  if "history" not in st.session_state:
48
  st.session_state.history = []
49
 
50
+ # Render chat history
51
+ for role, msg in st.session_state.history:
52
+ st.chat_message("user" if role=="You" else "assistant").write(msg)
53
 
54
  user_input = st.chat_input("Type your message…")
55
  if user_input:
 
57
  st.session_state.history.append(("You", user_input))
58
 
59
  recent = st.session_state.history[-CONTEXT_TURNS*2:]
60
+ context = "\n".join(t for _, t in recent)
61
  prompt = (
62
  f"{SYSTEM_PROMPT}\n\n"
63
  f"Context:\n{context}\n\n"