Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,62 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
-
|
| 4 |
|
| 5 |
# ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
CONTEXT_TURNS
|
| 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 lines beginning with 'User:' or 'Assistant:'.\n"
|
| 18 |
"If you don't know, say \"I don't know.\"\n"
|
| 19 |
)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
repo_id=MODEL_ID,
|
| 24 |
-
token=HF_TOKEN,
|
| 25 |
-
task="text-generation"
|
| 26 |
-
)
|
| 27 |
|
| 28 |
def query_hf(prompt: str) -> str:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# ββ Streamlit UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 47 |
st.set_page_config(layout="centered")
|
| 48 |
-
st.title("π§ DeepTalks (
|
| 49 |
st.subheader("Your personal AI Companion")
|
| 50 |
|
| 51 |
if "history" not in st.session_state:
|
| 52 |
st.session_state.history = []
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 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(
|
| 67 |
-
prompt
|
| 68 |
f"{SYSTEM_PROMPT}\n\n"
|
| 69 |
f"Context:\n{context}\n\n"
|
| 70 |
f"User: {user_input}\nAssistant:"
|
|
@@ -74,7 +66,7 @@ if user_input:
|
|
| 74 |
try:
|
| 75 |
reply = query_hf(prompt)
|
| 76 |
except Exception as e:
|
| 77 |
-
st.error(f"
|
| 78 |
reply = "Iβm sorry, something went wrong."
|
| 79 |
|
| 80 |
st.chat_message("assistant").write(reply)
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
+
import requests
|
| 4 |
|
| 5 |
# ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 6 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 7 |
+
MODEL_ID = "sourize/phi2-memory-lora"
|
| 8 |
+
CONTEXT_TURNS = 6
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
SYSTEM_PROMPT = (
|
| 11 |
"You are a helpful assistant for DeepTalks with base Phi-2\n"
|
| 12 |
"fine-tuned by Sourish for domain support.\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 |
+
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
| 19 |
+
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def query_hf(prompt: str) -> str:
|
| 22 |
+
payload = {
|
| 23 |
+
"inputs": prompt,
|
| 24 |
+
"parameters": {
|
| 25 |
+
"max_new_tokens": 128,
|
| 26 |
+
"do_sample": True,
|
| 27 |
+
"temperature": 0.7,
|
| 28 |
+
"top_p": 0.9,
|
| 29 |
+
"return_full_text": False
|
| 30 |
+
},
|
| 31 |
+
"options": {"use_cache": False}
|
| 32 |
+
}
|
| 33 |
+
r = requests.post(API_URL, headers=HEADERS, json=payload, timeout=60)
|
| 34 |
+
r.raise_for_status()
|
| 35 |
+
out = r.json()[0]["generated_text"].strip()
|
| 36 |
+
for m in ("User:", "Assistant:"):
|
| 37 |
+
if m in out:
|
| 38 |
+
out = out.split(m)[0].strip()
|
| 39 |
+
return out or "I don't know."
|
| 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 |
+
for role, txt in st.session_state.history:
|
| 50 |
+
st.chat_message("user" if role=="You" else "assistant").write(txt)
|
|
|
|
| 51 |
|
|
|
|
| 52 |
user_input = st.chat_input("Type your messageβ¦")
|
| 53 |
if user_input:
|
| 54 |
st.chat_message("user").write(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(msg for _, msg in recent)
|
| 59 |
+
prompt = (
|
| 60 |
f"{SYSTEM_PROMPT}\n\n"
|
| 61 |
f"Context:\n{context}\n\n"
|
| 62 |
f"User: {user_input}\nAssistant:"
|
|
|
|
| 66 |
try:
|
| 67 |
reply = query_hf(prompt)
|
| 68 |
except Exception as e:
|
| 69 |
+
st.error(f"API error: {e}")
|
| 70 |
reply = "Iβm sorry, something went wrong."
|
| 71 |
|
| 72 |
st.chat_message("assistant").write(reply)
|