ogegadavis254 commited on
Commit
7986268
·
verified ·
1 Parent(s): 9892e5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -73
app.py CHANGED
@@ -3,7 +3,6 @@
3
  @email: [email protected]
4
  """
5
 
6
- import numpy as np
7
  import streamlit as st
8
  from openai import OpenAI
9
  import os
@@ -11,40 +10,14 @@ from dotenv import load_dotenv
11
 
12
  load_dotenv()
13
 
14
- # initialize the client
15
  client = OpenAI(
16
  base_url="https://api-inference.huggingface.co/v1",
17
  api_key=os.environ.get('HUGGINGFACEHUB_API_TOKEN') # Replace with your token
18
  )
19
 
20
- # Create supported model
21
- model_link = {
22
- "Mistral-7B": "mistralai/Mistral-7B-Instruct-v0.2"
23
- }
24
-
25
- # Pull info about the model to display
26
- model_info = {
27
- "Mistral-7B": {
28
- 'description': """The Mistral model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
29
- \nIt was created by the [**Mistral AI**](https://mistral.ai/news/announcing-mistral-7b/) team as has over **7 billion parameters.** \n""",
30
- 'logo': 'https://mistral.ai/images/logo_hubc88c4ece131b91c7cb753f40e9e1cc5_2589_256x0_resize_q97_h2_lanczos_3.webp'
31
- }
32
- }
33
-
34
- # Random dog images for error message
35
- random_dog = ["0f476473-2d8b-415e-b944-483768418a95.jpg",
36
- "1bd75c81-f1d7-4e55-9310-a27595fa8762.jpg",
37
- "526590d2-8817-4ff0-8c62-fdcba5306d02.jpg",
38
- "1326984c-39b0-492c-a773-f120d747a7e2.jpg",
39
- "42a98d03-5ed7-4b3b-af89-7c4876cb14c3.jpg",
40
- "8b3317ed-2083-42ac-a575-7ae45f9fdc0d.jpg",
41
- "ee17f54a-83ac-44a3-8a35-e89ff7153fb4.jpg",
42
- "027eef85-ccc1-4a66-8967-5d74f34c8bb4.jpg",
43
- "08f5398d-7f89-47da-a5cd-1ed74967dc1f.jpg",
44
- "0fd781ff-ec46-4bdc-a4e8-24f18bf07def.jpg",
45
- "0fb4aeee-f949-4c7b-a6d8-05bf0736bdd1.jpg",
46
- "6edac66e-c0de-4e69-a9d6-b2e6f6f9001b.jpg",
47
- "bfb9e165-c643-4993-9b3a-7e73571672a6.jpg"]
48
 
49
  def reset_conversation():
50
  '''
@@ -54,50 +27,26 @@ def reset_conversation():
54
  st.session_state.messages = []
55
  return None
56
 
57
- # Create model description
58
- st.sidebar.write(f"You're now chatting with **Mistral-7B**")
59
- st.sidebar.markdown(model_info["Mistral-7B"]['description'])
60
- st.sidebar.image(model_info["Mistral-7B"]['logo'])
61
- st.sidebar.markdown("*Generated content may be inaccurate or false.*")
62
- st.sidebar.markdown("\nLearn how to build this chatbot [here](https://ngebodh.github.io/projects/2024-03-05/).")
63
- st.sidebar.markdown("\nRun into issues? Try the [back-up](https://huggingface.co/spaces/ngebodh/SimpleChatbot-Backup).")
64
 
65
- #Create a temperature slider
66
- temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, (0.5))
67
-
68
- # Add reset button to clear conversation
69
- st.sidebar.button('Reset Chat', on_click=reset_conversation) # Reset button
70
-
71
- # Check if the previous option is in session state
72
- if "prev_option" not in st.session_state:
73
- st.session_state.prev_option = "Mistral-7B"
74
-
75
- # Reset conversation if the model changes
76
- if st.session_state.prev_option != "Mistral-7B":
77
- st.session_state.messages = []
78
- st.session_state.prev_option = "Mistral-7B"
79
- reset_conversation()
80
-
81
- # Pull in the model we want to use
82
- repo_id = model_link["Mistral-7B"]
83
-
84
- st.subheader('AI - Mistral-7B')
85
-
86
- # Set a default model
87
- if "Mistral-7B" not in st.session_state:
88
- st.session_state["Mistral-7B"] = model_link["Mistral-7B"]
89
 
90
  # Initialize chat history
91
  if "messages" not in st.session_state:
92
  st.session_state.messages = []
93
 
 
 
 
94
  # Display chat messages from history on app rerun
95
  for message in st.session_state.messages:
96
  with st.chat_message(message["role"]):
97
  st.markdown(message["content"])
98
 
99
  # Accept user input
100
- if prompt := st.chat_input("Hi I'm Mistral-7B, ask me a question"):
101
 
102
  # Display user message in chat message container
103
  with st.chat_message("user"):
@@ -109,12 +58,12 @@ if prompt := st.chat_input("Hi I'm Mistral-7B, ask me a question"):
109
  with st.chat_message("assistant"):
110
  try:
111
  stream = client.chat.completions.create(
112
- model=model_link["Mistral-7B"],
113
  messages=[
114
  {"role": m["role"], "content": m["content"]}
115
  for m in st.session_state.messages
116
  ],
117
- temperature=temp_values,
118
  stream=True,
119
  max_tokens=3000,
120
  )
@@ -122,16 +71,9 @@ if prompt := st.chat_input("Hi I'm Mistral-7B, ask me a question"):
122
  response = st.write_stream(stream)
123
 
124
  except Exception as e:
125
- response = "😵‍💫 Looks like someone unplugged something!\
126
- \n Either the model space is being updated or something is down.\
127
- \n\
128
- \n Try again later. \
129
- \n\
130
- \n Here's a random pic of a 🐶:"
131
  st.write(response)
132
- random_dog_pick = 'https://random.dog/' + random_dog[np.random.randint(len(random_dog))]
133
- st.image(random_dog_pick)
134
- st.write("This was the error message:")
135
  st.write(e)
136
 
137
  st.session_state.messages.append({"role": "assistant", "content": response})
 
3
  @email: [email protected]
4
  """
5
 
 
6
  import streamlit as st
7
  from openai import OpenAI
8
  import os
 
10
 
11
  load_dotenv()
12
 
13
+ # Initialize the client
14
  client = OpenAI(
15
  base_url="https://api-inference.huggingface.co/v1",
16
  api_key=os.environ.get('HUGGINGFACEHUB_API_TOKEN') # Replace with your token
17
  )
18
 
19
+ # Model link
20
+ model_link = "mistralai/Mistral-7B-Instruct-v0.2"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def reset_conversation():
23
  '''
 
27
  st.session_state.messages = []
28
  return None
29
 
30
+ # Set the temperature value directly in the code
31
+ temperature = 0.5
 
 
 
 
 
32
 
33
+ # Add a button to clear conversation
34
+ st.button('Reset Chat', on_click=reset_conversation) # Reset button
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  # Initialize chat history
37
  if "messages" not in st.session_state:
38
  st.session_state.messages = []
39
 
40
+ st.title("Mistral-7B Chatbot")
41
+ st.subheader("Ask me anything!")
42
+
43
  # Display chat messages from history on app rerun
44
  for message in st.session_state.messages:
45
  with st.chat_message(message["role"]):
46
  st.markdown(message["content"])
47
 
48
  # Accept user input
49
+ if prompt := st.chat_input("Type your message here..."):
50
 
51
  # Display user message in chat message container
52
  with st.chat_message("user"):
 
58
  with st.chat_message("assistant"):
59
  try:
60
  stream = client.chat.completions.create(
61
+ model=model_link,
62
  messages=[
63
  {"role": m["role"], "content": m["content"]}
64
  for m in st.session_state.messages
65
  ],
66
+ temperature=temperature,
67
  stream=True,
68
  max_tokens=3000,
69
  )
 
71
  response = st.write_stream(stream)
72
 
73
  except Exception as e:
74
+ response = "An error occurred. Please try again later."
 
 
 
 
 
75
  st.write(response)
76
+ st.write("Error details:")
 
 
77
  st.write(e)
78
 
79
  st.session_state.messages.append({"role": "assistant", "content": response})