Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,16 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
-
from huggingface_hub import login
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
HF_TOKEN = st.secrets["huggingface"]["api_token"]
|
| 7 |
-
|
| 8 |
-
# Verify token before logging in
|
| 9 |
try:
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
st.error(f"β Invalid Hugging Face Token: {e}")
|
| 15 |
st.stop()
|
| 16 |
|
| 17 |
-
# Load the model
|
| 18 |
@st.cache_resource
|
| 19 |
def load_model():
|
| 20 |
return pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1", token=HF_TOKEN)
|
|
@@ -22,31 +18,11 @@ def load_model():
|
|
| 22 |
model = load_model()
|
| 23 |
|
| 24 |
# Streamlit UI
|
| 25 |
-
st.title("π€
|
| 26 |
-
st.write("A
|
| 27 |
-
|
| 28 |
-
# Initialize chat history
|
| 29 |
-
if "messages" not in st.session_state:
|
| 30 |
-
st.session_state["messages"] = []
|
| 31 |
|
| 32 |
-
|
| 33 |
-
for msg in st.session_state["messages"]:
|
| 34 |
-
st.chat_message(msg["role"]).write(msg["content"])
|
| 35 |
-
|
| 36 |
-
# User input
|
| 37 |
-
user_input = st.text_input("You:", key="user_input")
|
| 38 |
if st.button("Send"):
|
| 39 |
if user_input:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
# Display user message
|
| 43 |
-
st.chat_message("user").write(user_input)
|
| 44 |
-
|
| 45 |
-
# Get response from model
|
| 46 |
-
response_container = st.empty()
|
| 47 |
-
bot_response = model(user_input, max_length=100, do_sample=True)[0]['generated_text']
|
| 48 |
-
|
| 49 |
-
response_container.write(bot_response)
|
| 50 |
-
|
| 51 |
-
# Append bot response to chat history
|
| 52 |
-
st.session_state["messages"].append({"role": "assistant", "content": bot_response})
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from huggingface_hub import login
|
| 4 |
|
| 5 |
+
# β
Retrieve API token securely
|
|
|
|
|
|
|
|
|
|
| 6 |
try:
|
| 7 |
+
HF_TOKEN = st.secrets["huggingface"]["api_token"]
|
| 8 |
+
login(HF_TOKEN) # Log in to Hugging Face Hub
|
| 9 |
+
except Exception:
|
| 10 |
+
st.error("β Missing or invalid Hugging Face API token. Please check secrets.toml.")
|
|
|
|
| 11 |
st.stop()
|
| 12 |
|
| 13 |
+
# β
Load the model
|
| 14 |
@st.cache_resource
|
| 15 |
def load_model():
|
| 16 |
return pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1", token=HF_TOKEN)
|
|
|
|
| 18 |
model = load_model()
|
| 19 |
|
| 20 |
# Streamlit UI
|
| 21 |
+
st.title("π€ AI Developer Assistant")
|
| 22 |
+
st.write("A chatbot powered by Mistral-7B!")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
user_input = st.text_input("You:")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
if st.button("Send"):
|
| 26 |
if user_input:
|
| 27 |
+
response = model(user_input, max_length=100, do_sample=True)[0]['generated_text']
|
| 28 |
+
st.write(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|