Spaces:
Build error
Build error
Create streamlit_app.py
Browse files- streamlit_app.py +73 -0
streamlit_app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# streamlit_app.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
from langchain.llms import LlamaCpp
|
| 7 |
+
|
| 8 |
+
# -----------------------------
|
| 9 |
+
# MODEL DOWNLOAD / LOAD
|
| 10 |
+
# -----------------------------
|
| 11 |
+
st.sidebar.info("Initializing model... (this may take a few minutes the first time)")
|
| 12 |
+
|
| 13 |
+
os.makedirs("models", exist_ok=True)
|
| 14 |
+
model_file = "BioMistral-7B.Q4_K_M.gguf"
|
| 15 |
+
model_path = os.path.join("models", model_file)
|
| 16 |
+
|
| 17 |
+
if not os.path.exists(model_path):
|
| 18 |
+
st.sidebar.info("Downloading GGUF model from Hugging Face Hub...")
|
| 19 |
+
model_path = hf_hub_download(
|
| 20 |
+
repo_id="MaziyarPanahi/BioMistral-7B-GGUF",
|
| 21 |
+
filename=model_file,
|
| 22 |
+
cache_dir="models"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Load the model
|
| 26 |
+
llm = LlamaCpp(
|
| 27 |
+
model_path=model_path,
|
| 28 |
+
n_ctx=4096,
|
| 29 |
+
temperature=0.3,
|
| 30 |
+
max_tokens=512,
|
| 31 |
+
verbose=False
|
| 32 |
+
)
|
| 33 |
+
st.sidebar.success("Model loaded ✅")
|
| 34 |
+
|
| 35 |
+
# -----------------------------
|
| 36 |
+
# CHAT FUNCTION
|
| 37 |
+
# -----------------------------
|
| 38 |
+
def chat_fn(user_input):
|
| 39 |
+
try:
|
| 40 |
+
prompt = f"[INST] Answer as a mental health chatbot: {user_input} [/INST]"
|
| 41 |
+
response = llm.invoke(prompt)
|
| 42 |
+
|
| 43 |
+
# Extract text if the model returns a dict
|
| 44 |
+
if isinstance(response, dict) and "choices" in response:
|
| 45 |
+
return response["choices"][0]["text"]
|
| 46 |
+
return str(response)
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"Sorry, something went wrong. ({e})"
|
| 50 |
+
|
| 51 |
+
# -----------------------------
|
| 52 |
+
# STREAMLIT UI
|
| 53 |
+
# -----------------------------
|
| 54 |
+
st.title("🧠 Mental Health Companion Bot")
|
| 55 |
+
st.write("An empathetic AI assistant to support emotional well-being 💬")
|
| 56 |
+
|
| 57 |
+
if "history" not in st.session_state:
|
| 58 |
+
st.session_state.history = []
|
| 59 |
+
|
| 60 |
+
user_input = st.text_input("You:", key="input")
|
| 61 |
+
|
| 62 |
+
if st.button("Send") and user_input:
|
| 63 |
+
reply = chat_fn(user_input)
|
| 64 |
+
st.session_state.history.append(("You", user_input))
|
| 65 |
+
st.session_state.history.append(("Bot", reply))
|
| 66 |
+
st.session_state.input = "" # Clear input after sending
|
| 67 |
+
|
| 68 |
+
# Display chat history
|
| 69 |
+
for speaker, text in st.session_state.history:
|
| 70 |
+
if speaker == "You":
|
| 71 |
+
st.markdown(f"**{speaker}:** {text}")
|
| 72 |
+
else:
|
| 73 |
+
st.markdown(f"**{speaker}:** {text}")
|