sourize commited on
Commit
b965f65
Β·
verified Β·
1 Parent(s): 5002144

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -39
app.py CHANGED
@@ -1,70 +1,62 @@
1
  import os
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
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
- # ── 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
- # 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()
44
- return text or "I don't know."
 
 
45
 
46
  # ── Streamlit UI ──────────────────────────────────────────────────────────
47
  st.set_page_config(layout="centered")
48
- st.title("🧠 DeepTalks (Inference API)")
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:"
@@ -74,7 +66,7 @@ if user_input:
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)
 
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)