Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,14 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
-
from huggingface_hub import
|
| 4 |
|
| 5 |
-
# ββ
|
| 6 |
-
MODEL_ID
|
| 7 |
-
HF_TOKEN
|
| 8 |
-
CONTEXT_TURNS
|
| 9 |
MAX_NEW_TOKENS = 128
|
| 10 |
-
TEMPERATURE
|
| 11 |
-
TOP_P
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
# ββ UI
|
| 24 |
st.set_page_config(layout="centered")
|
| 25 |
-
st.title("π§ DeepTalks")
|
| 26 |
-
st.subheader("Your personal AI Companion"
|
| 27 |
|
| 28 |
if "history" not in st.session_state:
|
| 29 |
-
st.session_state.history = []
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
for role,
|
| 33 |
-
st.chat_message("user" if role
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
user_input = st.chat_input("
|
| 37 |
if user_input:
|
|
|
|
| 38 |
st.chat_message("user").write(user_input)
|
| 39 |
st.session_state.history.append(("You", user_input))
|
| 40 |
|
| 41 |
-
#
|
| 42 |
recent = st.session_state.history[-CONTEXT_TURNS*2:]
|
| 43 |
-
context = "\n".join(
|
| 44 |
-
prompt = f"{SYSTEM_PROMPT}\n\nContext:\n{context}\n\nUser: {user_input}\nAssistant:"
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
with st.spinner("Thinkingβ¦"):
|
| 47 |
try:
|
| 48 |
-
|
| 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))
|