Sharathhebbar24
commited on
Commit
β’
8869ff1
1
Parent(s):
192a1a0
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from langchain.llms import HuggingFaceHub
|
4 |
+
|
5 |
+
|
6 |
+
from models import return_models, return_text2text_generation_models, return_task_name, return_text_generation_models
|
7 |
+
dummy_parent = "google"
|
8 |
+
models_count = return_text2text_generation_models(dummy_parent, True) + return_text_generation_models(dummy_parent, True)
|
9 |
+
st.warning("Warning: Some models may not work and some models may require GPU to run")
|
10 |
+
st.text(f"As of now there are {models_count} model available")
|
11 |
+
st.text("Made with Langchain, StreamLit, Hugging Face and π")
|
12 |
+
st.header('π¦π One stop for Open Source Models')
|
13 |
+
|
14 |
+
API_KEY = st.sidebar.text_input(
|
15 |
+
'API Key',
|
16 |
+
type='password',
|
17 |
+
help="Type in your HuggingFace API key to use this app")
|
18 |
+
|
19 |
+
task_name = st.sidebar.selectbox(
|
20 |
+
label = "Choose the task you want to perform",
|
21 |
+
options = return_task_name(),
|
22 |
+
help="Choose your open source LLM to get started"
|
23 |
+
)
|
24 |
+
if task_name is None:
|
25 |
+
model_parent_visibility = True
|
26 |
+
else:
|
27 |
+
model_parent_visibility = False
|
28 |
+
|
29 |
+
model_parent_options = return_models(task_name)
|
30 |
+
model_parent = st.sidebar.selectbox(
|
31 |
+
label = "Choose your Source",
|
32 |
+
options = model_parent_options,
|
33 |
+
help="Choose your source of models",
|
34 |
+
disabled=model_parent_visibility
|
35 |
+
)
|
36 |
+
|
37 |
+
if model_parent is None:
|
38 |
+
model_name_visibility = True
|
39 |
+
else:
|
40 |
+
model_name_visibility = False
|
41 |
+
if task_name == "text2text-generation":
|
42 |
+
options = return_text2text_generation_models(model_parent)
|
43 |
+
else:
|
44 |
+
options = return_text_generation_models(model_parent)
|
45 |
+
model_name = st.sidebar.selectbox(
|
46 |
+
label = "Choose your Models",
|
47 |
+
options = options,
|
48 |
+
help="Choose your open source LLM to get started",
|
49 |
+
disabled=model_name_visibility
|
50 |
+
)
|
51 |
+
|
52 |
+
temperature = st.sidebar.slider(
|
53 |
+
label="Temperature",
|
54 |
+
min_value=0.1,
|
55 |
+
max_value=1.0,
|
56 |
+
step=0.1,
|
57 |
+
value=0.9,
|
58 |
+
help="Set the temperature to get accurate results"
|
59 |
+
)
|
60 |
+
|
61 |
+
max_token_length = st.sidebar.slider(
|
62 |
+
label="Token Length",
|
63 |
+
min_value=32,
|
64 |
+
max_value=1024,
|
65 |
+
step=32,
|
66 |
+
value=1024,
|
67 |
+
help="Set the max tokens to get accurate results"
|
68 |
+
)
|
69 |
+
|
70 |
+
|
71 |
+
os.environ['HUGGINGFACEHUB_API_TOKEN'] = API_KEY
|
72 |
+
def generate_response(input_text):
|
73 |
+
|
74 |
+
model_kwargs = {
|
75 |
+
"temperature": temperature,
|
76 |
+
"max_length": max_token_length
|
77 |
+
}
|
78 |
+
llm = HuggingFaceHub(
|
79 |
+
repo_id = model_name,
|
80 |
+
model_kwargs = model_kwargs
|
81 |
+
)
|
82 |
+
|
83 |
+
st.info(llm(input_text))
|
84 |
+
|
85 |
+
|
86 |
+
with st.form('my_form'):
|
87 |
+
try:
|
88 |
+
text = st.text_area('Enter Your Prompt', 'What are the three key pieces of advice for learning how to code?')
|
89 |
+
submitted = st.form_submit_button('Submit')
|
90 |
+
if not API_KEY.startswith('hf_'):
|
91 |
+
st.warning('Please enter your API key!', icon='β ')
|
92 |
+
if submitted and API_KEY.startswith('hf_'):
|
93 |
+
with st.spinner("Running...."):
|
94 |
+
generate_response(text)
|
95 |
+
except Exception as e:
|
96 |
+
st.error(e, icon="π¨")
|