yousifalishah commited on
Commit
050589b
Β·
verified Β·
1 Parent(s): 9fc9714

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -36
app.py CHANGED
@@ -1,20 +1,16 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
- from huggingface_hub import login, whoami
4
 
5
- # Load API key securely from Streamlit secrets
6
- HF_TOKEN = st.secrets["huggingface"]["api_token"]
7
-
8
- # Verify token before logging in
9
  try:
10
- user_info = whoami(HF_TOKEN)
11
- st.write(f"βœ… Logged in as: {user_info['name']}")
12
- login(HF_TOKEN)
13
- except Exception as e:
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("πŸ€– Open-Source Chatbot")
26
- st.write("A simple chatbot powered by Mistral-7B!")
27
-
28
- # Initialize chat history
29
- if "messages" not in st.session_state:
30
- st.session_state["messages"] = []
31
 
32
- # Display chat history
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
- st.session_state["messages"].append({"role": "user", "content": user_input})
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)