Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,8 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
import requests
|
| 4 |
|
| 5 |
-
# ββ
|
| 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 |
-
|
|
|
|
| 36 |
for m in ("User:", "Assistant:"):
|
| 37 |
-
if m in
|
| 38 |
-
|
| 39 |
-
return
|
| 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 |
-
|
| 50 |
-
|
|
|
|
| 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(
|
| 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"
|