Niansuh commited on
Commit
d9a0196
1 Parent(s): d9098a8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -0
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ Chatbot
2
+ @author: NiansuhAI
3
+ @email: niansuhtech@gmail.com
4
+ """
5
+ import numpy as np
6
+ import streamlit as st
7
+ from openai import OpenAI
8
+ import os
9
+ import sys
10
+ from dotenv import load_dotenv, dotenv_values
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('API_KEY') # Replace with your token
17
+ )
18
+
19
+ # Create supported models
20
+ model_links = {
21
+ "Mixtral-8x7B": "mistralai/Mixtral-8x7B-Instruct-v0.1",
22
+ "Meta-Llama-3-8B": "meta-llama/Meta-Llama-3-8B-Instruct",
23
+ "Mistral-7B-Instruct-v0.1": "mistralai/Mistral-7B-Instruct-v0.1",
24
+ "Mistral-7B-Instruct-v0.2": "mistralai/Mistral-7B-Instruct-v0.2",
25
+ "Mistral-7B-Instruct-v0.3": "mistralai/Mistral-7B-Instruct-v0.3",
26
+ "Gemma-7B": "google/gemma-1.1-7b-it",
27
+ "Gemma-2B": "google/gemma-1.1-2b-it",
28
+ "Zephyr-7B-β": "HuggingFaceH4/zephyr-7b-beta",
29
+ "Phi-3-mini": "microsoft/Phi-3-mini-4k-instruct",
30
+ }
31
+
32
+ # Random dog images for error message
33
+ random_dog = ["0f476473-2d8b-415e-b944-483768418a95.jpg",
34
+ "1bd75c81-f1d7-4e55-9310-a27595fa8762.jpg",
35
+ "526590d2-8817-4ff0-8c62-fdcba5306d02.jpg",
36
+ "1326984c-39b0-492c-a773-f120d747a7e2.jpg",
37
+ "42a98d03-5ed7-4b3b-af89-7c4876cb14c3.jpg",
38
+ "8b3317ed-2083-42ac-a575-7ae45f9fdc0d.jpg",
39
+ "ee17f54a-83ac-44a3-8a35-e89ff7153fb4.jpg",
40
+ "027eef85-ccc1-4a66-8967-5d74f34c8bb4.jpg",
41
+ "08f5398d-7f89-47da-a5cd-1ed74967dc1f.jpg",
42
+ "0fd781ff-ec46-4bdc-a4e8-24f18bf07def.jpg",
43
+ "0fb4aeee-f949-4c7b-a6d8-05bf0736bdd1.jpg",
44
+ "6edac66e-c0de-4e69-a9d6-b2e6f6f9001b.jpg",
45
+ "bfb9e165-c643-4993-9b3a-7e73571672a6.jpg"]
46
+
47
+ def reset_conversation():
48
+ '''
49
+ Resets Conversation
50
+ '''
51
+ st.session_state.conversation = []
52
+ st.session_state.messages = []
53
+ return None
54
+
55
+ # Define the available models
56
+ models = [key for key in model_links.keys()]
57
+
58
+ # Create the sidebar with the dropdown for model selection
59
+ selected_model = st.sidebar.selectbox("Select Model", models)
60
+
61
+ # Create a temperature slider
62
+ temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, (0.5))
63
+
64
+ # Add reset button to clear conversation
65
+ st.sidebar.button('Reset Chat', on_click=reset_conversation) # Reset button
66
+
67
+ # Create model description
68
+ st.sidebar.write(f"You're now chatting with **{selected_model}**")
69
+ st.sidebar.markdown("*Generated content may be inaccurate or false.*")
70
+
71
+ if "prev_option" not in st.session_state:
72
+ st.session_state.prev_option = selected_model
73
+
74
+ if st.session_state.prev_option != selected_model:
75
+ st.session_state.messages = []
76
+ st.session_state.prev_option = selected_model
77
+ reset_conversation()
78
+
79
+ # Pull in the model we want to use
80
+ repo_id = model_links[selected_model]
81
+
82
+ st.subheader(f'AI - {selected_model}')
83
+
84
+ # Initialize chat history
85
+ if "messages" not in st.session_state:
86
+ st.session_state.messages = []
87
+
88
+ # Display chat messages from history on app rerun
89
+ for message in st.session_state.messages:
90
+ with st.chat_message(message["role"]):
91
+ st.markdown(message["content"])
92
+
93
+ # Accept user input
94
+ if prompt := st.chat_input(f"Hi I'm {selected_model}, ask me a question"):
95
+
96
+ # Display user message in chat message container
97
+ with st.chat_message("user"):
98
+ st.markdown(prompt)
99
+ # Add user message to chat history
100
+ st.session_state.messages.append({"role": "user", "content": prompt})
101
+
102
+ # Display assistant response in chat message container
103
+ with st.chat_message("assistant"):
104
+
105
+ try:
106
+ stream = client.chat.completions.create(
107
+ model=model_links[selected_model],
108
+ messages=[
109
+ {"role": m["role"], "content": m["content"]}
110
+ for m in st.session_state.messages
111
+ ],
112
+ temperature=temp_values,
113
+ stream=True,
114
+ max_tokens=3000,
115
+ )
116
+
117
+ response = st.write_stream(stream)
118
+
119
+ except Exception as e:
120
+ response = "😵‍💫 Looks like someone unplugged something!\
121
+ \n Either the model space is being updated or something is down.\
122
+ \n\
123
+ \n Try again later. \
124
+ \n\
125
+ \n Here's a random pic of a 🐶:"
126
+ st.write(response)
127
+ random_dog_pick = 'https://random.dog/' + random_dog[np.random.randint(len(random_dog))]
128
+ st.image(random_dog_pick)
129
+ st.write("This was the error message:")
130
+ st.write(e)
131
+
132
+ st.session_state.messages.append({"role": "assistant", "content": response})