Spaces:
Sleeping
Sleeping
Streamit PoC for SML 3B models with HG api-inference
Browse files
Meta.png
ADDED
![]() |
Qwen.png
ADDED
![]() |
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
title: SmallZOO ChatBot 3B
|
3 |
-
emoji:
|
4 |
colorFrom: yellow
|
5 |
colorTo: red
|
6 |
sdk: streamlit
|
@@ -8,7 +8,7 @@ sdk_version: 1.40.2
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: cc-by-sa-4.0
|
11 |
-
short_description: Let's explore my ZOO
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
title: SmallZOO ChatBot 3B
|
3 |
+
emoji: 𐑟
|
4 |
colorFrom: yellow
|
5 |
colorTo: red
|
6 |
sdk: streamlit
|
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: cc-by-sa-4.0
|
11 |
+
short_description: Let's explore my ZOO of Small Language Models (~3b)
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
|
6 |
+
st.title("SmallZOO-ChatBot-3B")
|
7 |
+
|
8 |
+
base_url="https://api-inference.huggingface.co/models/"
|
9 |
+
API_KEY = os.environ.get('HUGGINGFACE_API_KEY')
|
10 |
+
|
11 |
+
model_links ={
|
12 |
+
"Llama-3.2 [3B]":base_url+"meta-llama/Llama-3.2-3B-Instruct",
|
13 |
+
"Qwen2.5 [3B]":base_url+"Qwen/Qwen2.5-3B-Instruct",
|
14 |
+
"Phi-3.5 [3.82B]":base_url+"microsoft/Phi-3.5-mini-instruct"
|
15 |
+
}
|
16 |
+
|
17 |
+
model_info ={
|
18 |
+
"Llama-3.2 [3B]":
|
19 |
+
{'description':"""The Llama-3.2 3B Instruct model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
20 |
+
\nA SLM (Large Language Model) is best for applications requiring fast response times, low resource consumption, and specific, narrow tasks. \n""",
|
21 |
+
'logo':'./Meta.png'},
|
22 |
+
|
23 |
+
"Qwen2.5 [3B]":
|
24 |
+
{'description':"""The Qwen2.5 3B Instruct model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
25 |
+
\nA SLM (Large Language Model) is best for applications requiring fast response times, low resource consumption, and specific, narrow tasks. \\n""",
|
26 |
+
'logo':'./Qwen.png'},
|
27 |
+
|
28 |
+
|
29 |
+
"Phi-3.5 [3.82B]":
|
30 |
+
{'description':"""The Phi-3.5 mini instruct model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
31 |
+
\nA SLM (Large Language Model) is best for applications requiring fast response times, low resource consumption, and specific, narrow tasks. \ \n""",
|
32 |
+
'logo':'./ms.png'},
|
33 |
+
|
34 |
+
}
|
35 |
+
|
36 |
+
def format_promt(message, custom_instructions=None):
|
37 |
+
prompt = ""
|
38 |
+
if custom_instructions:
|
39 |
+
prompt += f"[INST] {custom_instructions} [/INST]"
|
40 |
+
prompt += f"[INST] {message} [/INST]"
|
41 |
+
return prompt
|
42 |
+
|
43 |
+
def reset_conversation():
|
44 |
+
'''
|
45 |
+
Resets Conversation
|
46 |
+
'''
|
47 |
+
st.session_state.conversation = []
|
48 |
+
st.session_state.messages = []
|
49 |
+
return None
|
50 |
+
|
51 |
+
models =[key for key in model_links.keys()]
|
52 |
+
|
53 |
+
selected_model = st.sidebar.selectbox("Select Model", models)
|
54 |
+
|
55 |
+
temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, (0.5))
|
56 |
+
|
57 |
+
st.sidebar.button('Reset Chat', on_click=reset_conversation)
|
58 |
+
|
59 |
+
|
60 |
+
st.sidebar.write(f"You're now chatting with **{selected_model}**")
|
61 |
+
st.sidebar.markdown(model_info[selected_model]['description'])
|
62 |
+
st.sidebar.image(model_info[selected_model]['logo'])
|
63 |
+
st.sidebar.markdown("*Generated content can be inaccurate, offensive or non-factual!!!*")
|
64 |
+
|
65 |
+
if "prev_option" not in st.session_state:
|
66 |
+
st.session_state.prev_option = selected_model
|
67 |
+
|
68 |
+
if st.session_state.prev_option != selected_model:
|
69 |
+
st.session_state.messages = []
|
70 |
+
# st.write(f"Changed to {selected_model}")
|
71 |
+
st.session_state.prev_option = selected_model
|
72 |
+
reset_conversation()
|
73 |
+
|
74 |
+
|
75 |
+
repo_id = model_links[selected_model]
|
76 |
+
|
77 |
+
st.subheader(f'{selected_model}')
|
78 |
+
# st.title(f'ChatBot Using {selected_model}')
|
79 |
+
|
80 |
+
|
81 |
+
if "messages" not in st.session_state:
|
82 |
+
st.session_state.messages = []
|
83 |
+
|
84 |
+
|
85 |
+
for message in st.session_state.messages:
|
86 |
+
with st.chat_message(message["role"]):
|
87 |
+
st.markdown(message["content"])
|
88 |
+
|
89 |
+
if prompt := st.chat_input(f"Hi I'm {selected_model}, How can I help you today?"):
|
90 |
+
|
91 |
+
custom_instruction = "Act like a Human in conversation, you are helpfull assistant"
|
92 |
+
|
93 |
+
with st.chat_message("user"):
|
94 |
+
st.markdown(prompt)
|
95 |
+
|
96 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
97 |
+
|
98 |
+
formated_text = format_promt(prompt, custom_instruction)
|
99 |
+
|
100 |
+
|
101 |
+
with st.chat_message("assistant"):
|
102 |
+
client = InferenceClient(
|
103 |
+
model=model_links[selected_model],)
|
104 |
+
|
105 |
+
output = client.text_generation(
|
106 |
+
formated_text,
|
107 |
+
temperature=temp_values,#0.5
|
108 |
+
max_new_tokens=3000,
|
109 |
+
stream=True
|
110 |
+
)
|
111 |
+
|
112 |
+
response = st.write_stream(output)
|
113 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
ms.png
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
huggingface_hub
|
2 |
+
streamlit
|