Update app.py
Browse files
app.py
CHANGED
|
@@ -3,25 +3,25 @@ import streamlit as st
|
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
# ββ Configuration ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 6 |
-
HF_TOKEN
|
| 7 |
-
MODEL_ID
|
| 8 |
-
CONTEXT_TURNS
|
| 9 |
-
MAX_NEW_TOKENS
|
| 10 |
-
|
| 11 |
-
|
| 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 |
-
|
| 23 |
model=MODEL_ID,
|
| 24 |
-
|
| 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 =
|
| 34 |
-
# strip any
|
| 35 |
-
for marker in
|
| 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")
|
| 44 |
|
| 45 |
if "history" not in st.session_state:
|
| 46 |
-
st.session_state.history = [] #
|
| 47 |
|
| 48 |
-
# render
|
| 49 |
-
for role,
|
| 50 |
-
st.chat_message("user" if role=="You" else "assistant").write(
|
| 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 |
-
|
| 61 |
-
prompt
|
| 62 |
f"{SYSTEM_PROMPT}\n\n"
|
| 63 |
-
f"Context:\n{
|
| 64 |
f"User: {user_input}\nAssistant:"
|
| 65 |
)
|
| 66 |
|
| 67 |
-
# call HF Inference API
|
| 68 |
with st.spinner("Thinkingβ¦"):
|
| 69 |
try:
|
| 70 |
-
|
| 71 |
except Exception as e:
|
| 72 |
-
|
| 73 |
-
|
| 74 |
|
| 75 |
-
|
| 76 |
-
st.
|
|
|
|
|
|
| 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))
|