tttonight commited on
Commit
fc8039b
·
verified ·
1 Parent(s): 17d9ed6

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from dotenv import load_dotenv
3
+ import os
4
+ from langchain_community.llms import HuggingFaceEndpoint
5
+
6
+ # try:
7
+ # load_dotenv('C:\\Users\\H587077\\Desktop\\study\\temp\\api-keys.txt')
8
+ # except:
9
+ # print("Environment file not found !! MUST find the env var HUGGINGFACEHUB_API_TOKEN to work.")
10
+
11
+ # Title
12
+ st.title('HuggingFace LLM playground')
13
+
14
+ models = [
15
+ 'mistralai/Mistral-7B-Instruct-v0.2',
16
+ 'google/flan-t5-xxl',
17
+ 'tiiuae/falcon-40b-instruct',
18
+ ]
19
+
20
+ # Selected model in model_id
21
+ model_id = st.sidebar.selectbox(
22
+ 'Select model',
23
+ options=tuple(models)
24
+ )
25
+
26
+ if 'model-response' not in st.session_state:
27
+ st.session_state['model-response'] = '<provide query & click on invoke>'
28
+
29
+ # draw the box for model response
30
+ st.text_area('Response', value = st.session_state['model-response'], height=400)
31
+
32
+ # draw the box for query
33
+ query = st.text_area('Query', placeholder='provide query & invoke', value='who was the president of the USA in 2023?')
34
+
35
+ # Temperature
36
+ temperature = st.sidebar.slider(
37
+ label='Temperature',
38
+ min_value=0.01,
39
+ max_value=1.0
40
+ )
41
+
42
+ # Top p
43
+ top_p = st.sidebar.slider(
44
+ label='Top p',
45
+ min_value=0.01,
46
+ max_value=1.0,
47
+ value=0.01
48
+ )
49
+
50
+ # Top k
51
+ top_k = st.sidebar.slider(
52
+ label='Top k',
53
+ min_value=1,
54
+ max_value=50,
55
+ value=10
56
+ )
57
+
58
+ repetition_penalty = st.sidebar.slider(
59
+ label='Repeatition penalty',
60
+ min_value=0.0,
61
+ max_value=5.0,
62
+ value=1.0
63
+ )
64
+
65
+ # Maximum token
66
+ max_tokens = st.sidebar.number_input(
67
+ label='Max tokens',
68
+ value=50
69
+ )
70
+
71
+ # Function to create the LLM
72
+ def get_llm(model_id):
73
+ return HuggingFaceEndpoint(
74
+ repo_id=model_id,
75
+ temperature=temperature,
76
+ top_k = top_k,
77
+ top_p = top_p,
78
+ repetition_penalty = repetition_penalty,
79
+ max_new_tokens=max_tokens
80
+ )
81
+
82
+ # Function for invoking the LLM
83
+ def invoke():
84
+ llm_hf = get_llm(model_id)
85
+
86
+ # Show spinner, while we are waiting for the response
87
+ with st.spinner('Invoking LLM ... '):
88
+ st.session_state['model-response'] = llm_hf.invoke(query)
89
+
90
+ st.button("Invoke", on_click=invoke)