Update app.py
Browse files
app.py
CHANGED
|
@@ -2,75 +2,70 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
-
# ββ
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
CONTEXT_TURNS
|
| 9 |
MAX_NEW_TOKENS = 128
|
|
|
|
|
|
|
| 10 |
|
| 11 |
SYSTEM_PROMPT = (
|
| 12 |
-
"You are a helpful assistant for DeepTalks
|
|
|
|
| 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 |
-
|
| 19 |
-
client = InferenceClient(token=HF_TOKEN)
|
| 20 |
|
| 21 |
-
|
| 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
|
| 44 |
-
st.subheader("Your personal AI Companion")
|
| 45 |
|
| 46 |
if "history" not in st.session_state:
|
| 47 |
-
st.session_state.history = []
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
for role,
|
| 51 |
-
st.chat_message("user" if role == "You" else "assistant").write(
|
| 52 |
|
| 53 |
-
# User
|
| 54 |
-
user_input = st.chat_input("
|
| 55 |
if user_input:
|
| 56 |
st.chat_message("user").write(user_input)
|
| 57 |
st.session_state.history.append(("You", user_input))
|
| 58 |
|
| 59 |
-
#
|
| 60 |
recent = st.session_state.history[-CONTEXT_TURNS*2:]
|
| 61 |
-
context = "\n".join(
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
| 76 |
-
st.session_state.history.append(("Bot",
|
|
|
|
| 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))
|