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|
+
from utils import *
|
| 3 |
+
|
| 4 |
+
if check_password():
|
| 5 |
+
st.markdown("""
|
| 6 |
+
## With ChatGPT
|
| 7 |
+
|
| 8 |
+
#### 1. Set up OpenAI config in your notebook with code snippet below:
|
| 9 |
+
|
| 10 |
+
```python
|
| 11 |
+
import openai ## you need to install this with "pip install openai"
|
| 12 |
+
import os
|
| 13 |
+
|
| 14 |
+
from dotenv import load_dotenv
|
| 15 |
+
load_dotenv() # read local .env file
|
| 16 |
+
|
| 17 |
+
openai.api_key = os.getenv('<Your OpenAI API key name>') # this assign the key to a variable named "openai.api_key"
|
| 18 |
+
```
|
| 19 |
+
|
| 20 |
+
- you might want to check on your own notebook if you did correctly by "print(openai.api_key)"
|
| 21 |
+
|
| 22 |
+
#### 2. Define a python function to make our life easier later on
|
| 23 |
+
|
| 24 |
+
```python
|
| 25 |
+
def get_completion(prompt, model="gpt-3.5-turbo"):
|
| 26 |
+
messages = [{"role": "user", "content": prompt}]
|
| 27 |
+
response = openai.ChatCompletion.create(model=model, messages=messages,
|
| 28 |
+
temperature=0, # this is the degree of randomness of the model's output
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
content = response.choices[0].message["content"] # This is the real reply from the ChatGPT
|
| 32 |
+
|
| 33 |
+
## This is the data we can grab from the API, how many tokens we have used in this conversation
|
| 34 |
+
token_dict = {
|
| 35 |
+
'prompt_tokens':response['usage']['prompt_tokens'],
|
| 36 |
+
'completion_tokens':response['usage']['completion_tokens'],
|
| 37 |
+
'total_tokens':response['usage']['total_tokens'],
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
## This is another important data we will use later on
|
| 41 |
+
moderation_output = openai.Moderation.create(input=prompt)["results"][0]
|
| 42 |
+
return content, token_dict, moderation_output
|
| 43 |
+
```
|
| 44 |
+
- The above function have two input,
|
| 45 |
+
- prompt is a string input that will pass to ChatGPT
|
| 46 |
+
- model is another input for us to sellet which ChatGPT model we wish to talk on
|
| 47 |
+
- The function return three peaces of information, AI's reply, the usage info, and the moderation.
|
| 48 |
+
- we only focus on the 'content' this week,
|
| 49 |
+
- we will use the other two in the coming weeks
|
| 50 |
+
|
| 51 |
+
#### 3. Our example text to summarise, a product review in this case
|
| 52 |
+
""")
|
| 53 |
+
|
| 54 |
+
st.code(
|
| 55 |
+
'''
|
| 56 |
+
prod_review =
|
| 57 |
+
"""
|
| 58 |
+
Got this panda plush toy for my daughter's birthday, who loves it and takes it everywhere. \n
|
| 59 |
+
It's soft and super cute, and its face has a friendly look. It's a bit small for what I paid though. \n
|
| 60 |
+
I think there might be other options that are bigger for the same price. \n
|
| 61 |
+
It arrived a day earlier than expected, so I got to play with it myself before I gave it to her.
|
| 62 |
+
"""
|
| 63 |
+
'''
|
| 64 |
+
, language = 'python')
|
| 65 |
+
|
| 66 |
+
st.write("##")
|
| 67 |
+
|
| 68 |
+
st.markdown("""
|
| 69 |
+
#### 4. Now, we add our system requirement to the bot, asking it to follow our instruction specifically
|
| 70 |
+
""")
|
| 71 |
+
st.code('''
|
| 72 |
+
numberOfWords = '20' # Here we specify the max number of words we want it to reply
|
| 73 |
+
topics = "weather" # Here we want it to focus on specific topic
|
| 74 |
+
|
| 75 |
+
## Here is our system instruction, check how we pass our specific requirements to the system one
|
| 76 |
+
prompt = f"""
|
| 77 |
+
Your task is to generate a short summary of a given text \n
|
| 78 |
+
Summarize the text below, delimited by triple backticks, \n
|
| 79 |
+
in at most {numberOfWords} words, \n
|
| 80 |
+
and focusing on any aspects that mention {topics}.
|
| 81 |
+
|
| 82 |
+
Review: ```{prod_review}```
|
| 83 |
+
"""
|
| 84 |
+
''', language = 'python')
|
| 85 |
+
|
| 86 |
+
st.markdown("""
|
| 87 |
+
Now if you run code below, you will get the response, if everything were correct so far...
|
| 88 |
+
|
| 89 |
+
```python
|
| 90 |
+
response, _, _ = get_completion(prompt) # we use '_' to bypass other outputs
|
| 91 |
+
print(response)
|
| 92 |
+
```
|
| 93 |
+
""")
|
| 94 |
+
|
| 95 |
+
st.markdown("""
|
| 96 |
+
#### 5. More specific prompt
|
| 97 |
+
""")
|
| 98 |
+
st.code('''
|
| 99 |
+
numberOfWords = '20'
|
| 100 |
+
topics = "marry"
|
| 101 |
+
|
| 102 |
+
prompt = f"""
|
| 103 |
+
Your task is to generate a short summary from text below, delimited by triple backticks, in at most {numberOfWords} words.\n
|
| 104 |
+
Firstly, extract relevant information and create a list of keywords without response,\n
|
| 105 |
+
Then, check if {topics} is in your list, if not, just response no relevent topics about {topics} to summarise,\
|
| 106 |
+
if it is in your list, focusing on any aspects that mention {topics}, \n
|
| 107 |
+
Review: ```{prod_review}```
|
| 108 |
+
"""
|
| 109 |
+
''', language = 'python')
|
| 110 |
+
|
| 111 |
+
st.markdown("""
|
| 112 |
+
- The prompt above is a template for us to build our first app next week.
|
| 113 |
+
- We hope to create an app that can only pass the user input into those pre-defined variables
|
| 114 |
+
- Therefore, user only need very easy interface to get the desired output.
|
| 115 |
+
""")
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
st.markdown("""
|
| 119 |
+
## With Hugging Chat
|
| 120 |
+
""")
|
| 121 |
+
st.code('''
|
| 122 |
+
# we need to install the hugchat library fistly
|
| 123 |
+
## in your notebook, copy and execute the following code
|
| 124 |
+
!pip install hugchat
|
| 125 |
+
''')
|
| 126 |
+
st.markdown(" - Now we can call the Hugging chat API and let it do the same job as ChatGPT")
|
| 127 |
+
st.code('''
|
| 128 |
+
from hugchat.login import Login
|
| 129 |
+
from hugchat import hugchat
|
| 130 |
+
sign = Login(email, passwd) ## You need to here are your sign up email and password for Hugging Face
|
| 131 |
+
|
| 132 |
+
## Just copy these two lines and execute it, don't worry about it too much
|
| 133 |
+
cookies = sign.login()
|
| 134 |
+
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
|
| 135 |
+
|
| 136 |
+
## get the response from the Hugging Chat
|
| 137 |
+
res = chatbot.chat(prompt)
|
| 138 |
+
print(res)
|
| 139 |
+
''', language = 'python')
|
| 140 |
+
|
| 141 |
+
st.markdown("""
|
| 142 |
+
- As you might see, Hugging chat can do the job of summarization, \n
|
| 143 |
+
- But in terms of satisfy specific requirement from us, it has limitation compare to ChatGPT, \n
|
| 144 |
+
- ChatGPT is more advanced and it can follow human instruction clearly.\n
|
| 145 |
+
- The advantage of Hugging Chat is that it is free, so you might do as many experiment as you wish, \n
|
| 146 |
+
without cost you money.
|
| 147 |
+
- In our app, hugging chat would be a good alternative to ChatGPT for some case.
|
| 148 |
+
""")
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
|
pages/2_202_Access_to_HuggingFace_with_Notebook.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from utils import *
|
| 3 |
+
|
| 4 |
+
def check_password():
|
| 5 |
+
"""Returns `True` if the user had the correct password."""
|
| 6 |
+
|
| 7 |
+
def password_entered():
|
| 8 |
+
"""Checks whether a password entered by the user is correct."""
|
| 9 |
+
if st.session_state["password"] == st.secrets["password"]:
|
| 10 |
+
st.session_state["password_correct"] = True
|
| 11 |
+
del st.session_state["password"] # don't store password
|
| 12 |
+
else:
|
| 13 |
+
st.session_state["password_correct"] = False
|
| 14 |
+
|
| 15 |
+
if "password_correct" not in st.session_state:
|
| 16 |
+
# First run, show input for password.
|
| 17 |
+
st.text_input(
|
| 18 |
+
"Password", type="password", on_change=password_entered, key="password"
|
| 19 |
+
)
|
| 20 |
+
return False
|
| 21 |
+
elif not st.session_state["password_correct"]:
|
| 22 |
+
# Password not correct, show input + error.
|
| 23 |
+
st.text_input(
|
| 24 |
+
"Password", type="password", on_change=password_entered, key="password"
|
| 25 |
+
)
|
| 26 |
+
st.error("😕 Password incorrect")
|
| 27 |
+
return False
|
| 28 |
+
else:
|
| 29 |
+
# Password correct.
|
| 30 |
+
return True
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
if check_password():
|
| 34 |
+
st.markdown("""
|
| 35 |
+
## Notebook to Hugging Face Space
|
| 36 |
+
|
| 37 |
+
##### 1. Install huggingface_hub
|
| 38 |
+
|
| 39 |
+
```python
|
| 40 |
+
!pip install huggingface_hub==0.15.0
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
##### 2. Login to Huggingface with notebook
|
| 44 |
+
|
| 45 |
+
```python
|
| 46 |
+
from huggingface_hub import login
|
| 47 |
+
import os
|
| 48 |
+
from dotenv import load_dotenv
|
| 49 |
+
load_dotenv()
|
| 50 |
+
|
| 51 |
+
HF_API_KEY = os.getenv('HF_API_KEY') ## This should be set up last week :)
|
| 52 |
+
login(token = HF_API_KEY) # You might need to execute this cell twice sometimes
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
The code above have two parts:
|
| 56 |
+
|
| 57 |
+
1. Import the method, called "login" from the library we just installed; this is the most common way in Python to import library or methods from library.
|
| 58 |
+
2. Call the method by passing a parameter "token = ". This token is the **Personal Token** when you sign up Hugging Face.
|
| 59 |
+
|
| 60 |
+
##### 3. Download a repo from our code space
|
| 61 |
+
|
| 62 |
+
```python
|
| 63 |
+
from huggingface_hub import snapshot_download # This time we use another method
|
| 64 |
+
# Call the method and passing three parameters into it
|
| 65 |
+
# The 1st parameter is repo_id, which is formed by "Organization Name / Space name", in our case of the tutorial folder,
|
| 66 |
+
# it is "TGSAI/Tutorials"
|
| 67 |
+
# The 2nd parameter is location(path) on your computer you want the folder to be saved
|
| 68 |
+
# The 3rd parameter is repo type, in our case, it is 'space'
|
| 69 |
+
snapshot_download(repo_id="TGSAI/Tutorials",
|
| 70 |
+
local_dir = "./Tutorials",
|
| 71 |
+
repo_type='space',
|
| 72 |
+
revision = 'week2') ## We will create branch per week
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
##### 4. Editing our loacl codes
|
| 76 |
+
|
| 77 |
+
After you download repo from our space, you can edit and testing those files on you own computer, the location/path of the folder is show as the output above.
|
| 78 |
+
- if it is a jupyter notebook file, i.e. with ".ipynb" as file extension, you can open that file in the Jupyter Notebook directly
|
| 79 |
+
- it it is other form, such as ".py" as extension, you can use [vscode.dev](https://vscode.dev/) as the IDE to edit the file on your machine, or for the sake of simplicity, you can edit them directly in the notebook server by click the file.
|
| 80 |
+
- More detailed instruction of vscode.dev can be checked [here](https://google.com)
|
| 81 |
+
- For this week, you might only need to edit the app.py file just inside the /Tutorial folder you have just downloaded
|
| 82 |
+
- find the section "Our member profile"
|
| 83 |
+
- replace the "coming soon ..." with your name and some intro about you
|
| 84 |
+
- you should always download the latest version in each branch before you edit code
|
| 85 |
+
|
| 86 |
+
To check existing branch, using the code snippet below:
|
| 87 |
+
|
| 88 |
+
```python
|
| 89 |
+
from huggingface_hub import HfApi
|
| 90 |
+
api = HfApi()
|
| 91 |
+
|
| 92 |
+
api.list_repo_refs(repo_id = "TGSAI/Tutorials", repo_type = "space")
|
| 93 |
+
```
|
| 94 |
+
|
| 95 |
+
##### 5. Update your changes to the specific branch
|
| 96 |
+
|
| 97 |
+
1. If you have edited the app.py file with your profile, the change already happened in your local code folder;
|
| 98 |
+
2. For simplicity, you will upload the updated folder "Tutorials" to the "week2" branch;
|
| 99 |
+
3. We will normally merge the two branches, "main" and weekly branch at the end of week, then the "main" file will have the change we made.
|
| 100 |
+
|
| 101 |
+
Using the code snippet below to update your changes,
|
| 102 |
+
- You will need to do detailed editing and check before you submit it, otherwise, your pull request would not be accepted
|
| 103 |
+
|
| 104 |
+
```python
|
| 105 |
+
api.upload_folder(folder_path="./Tutorials", # the folder path at the local machine
|
| 106 |
+
repo_id="TGSAI/Tutorials",
|
| 107 |
+
repo_type="space",
|
| 108 |
+
revision = 'week2', # make sure upload to the weekly branch
|
| 109 |
+
commit_message = 'https://app.clickup.com/t/860r79ppd', # add comment, mostly link to Clickup ticket url
|
| 110 |
+
commit_description = '', # any discription, or omitted
|
| 111 |
+
create_pr = True # Important! You must create Pull Request, then the maintainer will need to check it.
|
| 112 |
+
)
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
""")
|