acloudfan commited on
Commit
6b666e9
1 Parent(s): 83328c0

first commit

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Demonstrates the basic usage
2
+ # 1. Select model
3
+ # 2. Provide a query
4
+ # 3. Invoke the model
5
+
6
+ import streamlit as st
7
+ from dotenv import load_dotenv
8
+ import os
9
+ import time
10
+
11
+ from langchain_community.llms import HuggingFaceHub
12
+ from langchain_community.llms import HuggingFaceEndpoint
13
+
14
+
15
+ # Load the API keys, if running locally
16
+ # CHANGE the path to the env file
17
+
18
+ # If HF space is used then set the env var HUGGINGFACEHUB_API_TOKEN in the settings
19
+ try:
20
+ load_dotenv('C:\\Users\\raj\\.jupyter\\.env')
21
+ except:
22
+ print("Environment file not found !! MUST find the env var HUGGINGFACEHUB_API_TOKEN to work.")
23
+
24
+
25
+
26
+ # Title
27
+ st.title('Try out the model')
28
+
29
+ # Models select box
30
+ models = [
31
+ 'mistralai/Mistral-7B-Instruct-v0.2',
32
+ 'google/flan-t5-xxl',
33
+ 'tiiuae/falcon-40b-instruct',
34
+ 'microsoft/phi-2'
35
+ ]
36
+
37
+ model_id = st.sidebar.selectbox(
38
+ 'Select model',
39
+ options=tuple(models)
40
+ )
41
+
42
+ # Read the API key from environment - switch key for different providers
43
+ api_token = os.environ.get('HUGGINGFACEHUB_API_TOKEN')
44
+
45
+ if 'model-response' not in st.session_state:
46
+ st.session_state['model-response'] = '<provide query & click on invoke>'
47
+
48
+ # draw the box for model response
49
+ st.text_area('Response', value = st.session_state['model-response'], height=400)
50
+
51
+ # draw the box for query
52
+ query = st.text_area('Query', placeholder='provide query & invoke', value='who was the president of the USA in 2023?')
53
+
54
+ # Model parameter controls
55
+ # https://api.python.langchain.com/en/latest/llms/langchain_community.llms.huggingface_endpoint.HuggingFaceEndpoint.html
56
+
57
+ # Temperature
58
+ temperature = st.sidebar.slider(
59
+ label='Temperature',
60
+ min_value=0.01,
61
+ max_value=1.0
62
+ )
63
+
64
+ # Top p
65
+ top_p = st.sidebar.slider(
66
+ label='Top p',
67
+ min_value=0.01,
68
+ max_value=1.0,
69
+ value=0.01
70
+ )
71
+
72
+ # Top k
73
+ top_k = st.sidebar.slider(
74
+ label='Top k',
75
+ min_value=1,
76
+ max_value=50,
77
+ value=10
78
+ )
79
+
80
+ repetition_penalty = st.sidebar.slider(
81
+ label='Repeatition penalty',
82
+ min_value=0.0,
83
+ max_value=5.0,
84
+ value=1.0
85
+ )
86
+
87
+ # Maximum token
88
+ max_tokens = st.sidebar.number_input(
89
+ label='Max tokens',
90
+ value=50
91
+ )
92
+
93
+ # invoke the LLM
94
+ model_kwargs={ "temperature": "0.1" }
95
+ def invoke():
96
+ llm_hf = HuggingFaceEndpoint(
97
+ repo_id=model_id,
98
+ temperature=temperature,
99
+ top_k = top_k,
100
+ top_p = top_p,
101
+ repetition_penalty = repetition_penalty,
102
+ max_new_tokens=max_tokens
103
+ )
104
+
105
+ # Show spinner, while we are waiting for the response
106
+ with st.spinner('Invoking LLM ... '):
107
+ time.sleep(5)
108
+ st.session_state['model-response'] = llm_hf.invoke(query)
109
+ print(query)
110
+
111
+ st.button("Invoke", on_click=invoke)