Update app.py
Browse files
app.py
CHANGED
|
@@ -2,9 +2,9 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
from huggingface_hub import InferenceApi
|
| 4 |
|
| 5 |
-
# ββ
|
| 6 |
MODEL_ID = "sourize/phi2-memory-lora"
|
| 7 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
CONTEXT_TURNS = 6
|
| 9 |
MAX_NEW_TOKENS = 128
|
| 10 |
TEMPERATURE = 0.7
|
|
@@ -14,11 +14,11 @@ 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
|
| 18 |
"If you don't know, say \"I don't know.\"\n"
|
| 19 |
)
|
| 20 |
|
| 21 |
-
# ββ
|
| 22 |
inference = InferenceApi(
|
| 23 |
repo_id=MODEL_ID,
|
| 24 |
token=HF_TOKEN,
|
|
@@ -26,22 +26,18 @@ inference = InferenceApi(
|
|
| 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 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"return_full_text": False
|
| 40 |
-
}
|
| 41 |
)
|
| 42 |
-
#
|
| 43 |
text = outputs[0]["generated_text"].strip()
|
| 44 |
-
# strip
|
| 45 |
for marker in ("User:", "Assistant:"):
|
| 46 |
if marker in text:
|
| 47 |
text = text.split(marker)[0].strip()
|
|
@@ -53,38 +49,33 @@ 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 = []
|
| 57 |
|
| 58 |
-
#
|
| 59 |
for role, msg in st.session_state.history:
|
| 60 |
st.chat_message("user" if role=="You" else "assistant").write(msg)
|
| 61 |
|
| 62 |
-
#
|
| 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 |
-
#
|
| 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}\
|
| 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
|
| 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))
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from huggingface_hub import InferenceApi
|
| 4 |
|
| 5 |
+
# ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 6 |
MODEL_ID = "sourize/phi2-memory-lora"
|
| 7 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
CONTEXT_TURNS = 6
|
| 9 |
MAX_NEW_TOKENS = 128
|
| 10 |
TEMPERATURE = 0.7
|
|
|
|
| 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 lines beginning with 'User:' or 'Assistant:'.\n"
|
| 18 |
"If you don't know, say \"I don't know.\"\n"
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# ββ Inference API βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 22 |
inference = InferenceApi(
|
| 23 |
repo_id=MODEL_ID,
|
| 24 |
token=HF_TOKEN,
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
def query_hf(prompt: str) -> str:
|
| 29 |
+
# All generation kwargs go at top level
|
|
|
|
|
|
|
| 30 |
outputs = inference(
|
| 31 |
inputs=prompt,
|
| 32 |
+
max_new_tokens=MAX_NEW_TOKENS,
|
| 33 |
+
do_sample=True,
|
| 34 |
+
temperature=TEMPERATURE,
|
| 35 |
+
top_p=TOP_P,
|
| 36 |
+
return_full_text=False,
|
|
|
|
|
|
|
| 37 |
)
|
| 38 |
+
# returns a list of dicts: [{"generated_text": "..."}]
|
| 39 |
text = outputs[0]["generated_text"].strip()
|
| 40 |
+
# strip stray speaker markers
|
| 41 |
for marker in ("User:", "Assistant:"):
|
| 42 |
if marker in text:
|
| 43 |
text = text.split(marker)[0].strip()
|
|
|
|
| 49 |
st.subheader("Your personal AI Companion")
|
| 50 |
|
| 51 |
if "history" not in st.session_state:
|
| 52 |
+
st.session_state.history = []
|
| 53 |
|
| 54 |
+
# render past chat
|
| 55 |
for role, msg in st.session_state.history:
|
| 56 |
st.chat_message("user" if role=="You" else "assistant").write(msg)
|
| 57 |
|
| 58 |
+
# user input
|
| 59 |
user_input = st.chat_input("Type your messageβ¦")
|
| 60 |
if user_input:
|
|
|
|
| 61 |
st.chat_message("user").write(user_input)
|
| 62 |
st.session_state.history.append(("You", user_input))
|
| 63 |
|
| 64 |
+
# build context
|
| 65 |
recent = st.session_state.history[-CONTEXT_TURNS*2:]
|
| 66 |
context = "\n".join(text for _, text in recent)
|
| 67 |
+
prompt = (
|
|
|
|
| 68 |
f"{SYSTEM_PROMPT}\n\n"
|
| 69 |
f"Context:\n{context}\n\n"
|
| 70 |
+
f"User: {user_input}\nAssistant:"
|
|
|
|
| 71 |
)
|
| 72 |
|
|
|
|
| 73 |
with st.spinner("Thinkingβ¦"):
|
| 74 |
try:
|
| 75 |
reply = query_hf(prompt)
|
| 76 |
except Exception as e:
|
| 77 |
+
st.error(f"Inference error: {e}")
|
| 78 |
reply = "Iβm sorry, something went wrong."
|
| 79 |
|
|
|
|
| 80 |
st.chat_message("assistant").write(reply)
|
| 81 |
st.session_state.history.append(("Bot", reply))
|