Spaces:
Runtime error
Runtime error
Upload 2 files
Browse filesAdd week 3 tutorials
pages/3_301_Running_streamlit_locally.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from utils import *
|
| 3 |
+
|
| 4 |
+
if check_password():
|
| 5 |
+
st.markdown("""
|
| 6 |
+
## Set up local streamlit
|
| 7 |
+
|
| 8 |
+
#### 1. install streamlit
|
| 9 |
+
|
| 10 |
+
```python
|
| 11 |
+
pip install streamlit==1.24.0 # We use a the version of streamlit that matching the one we are using in HF spaces
|
| 12 |
+
```
|
| 13 |
+
|
| 14 |
+
#### 2. Setup local workspace
|
| 15 |
+
- Depend on the OS you are using, you will find different way of define your workspace,
|
| 16 |
+
we recommand you define your workspace as the same path of your launch of jupyter notebook,
|
| 17 |
+
so if you do not want to use vscode.dev, using jupyter notebook can also do the job
|
| 18 |
+
|
| 19 |
+
###### For MacOS:
|
| 20 |
+
```cmd
|
| 21 |
+
# at the same path of your notebook
|
| 22 |
+
mkdir webapps && cd "$_"
|
| 23 |
+
|
| 24 |
+
# You are now in your /webapps directory
|
| 25 |
+
# You can now create your specific folder for different apps, for example:
|
| 26 |
+
mkdir myfirstApp && cd "$_" # you are now in the sub-directory /webapps/myfirstApp
|
| 27 |
+
|
| 28 |
+
## now you create a app.py file by:
|
| 29 |
+
touch app.py
|
| 30 |
+
|
| 31 |
+
### After create app.py, you can use you IDE (or just notebook) to edit this file
|
| 32 |
+
```
|
| 33 |
+
- Then, open the app.py file from any IDE or just notebook editor, typing following :
|
| 34 |
+
|
| 35 |
+
```python
|
| 36 |
+
# app.py
|
| 37 |
+
import streamlit as st
|
| 38 |
+
|
| 39 |
+
st.header("My first App")
|
| 40 |
+
```
|
| 41 |
+
and save the changes to the file
|
| 42 |
+
|
| 43 |
+
- in the terminal again, typing:
|
| 44 |
+
|
| 45 |
+
```cmd
|
| 46 |
+
nohup streamlit run app.py & disown
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
now in your browser, type in
|
| 50 |
+
- http://localhost:8501/
|
| 51 |
+
your streamlit app should popup if everything goes correctly.
|
| 52 |
+
- sometimes, if you already have a streamlit apps running locally, the port might become 8502 or more
|
| 53 |
+
|
| 54 |
+
- From now on, you can keep the tab or your streamlit app openning and running, whence you change your code from
|
| 55 |
+
app.py, you can go back to your browser to click "Rerun" at the top right to check the effect of change.
|
| 56 |
+
|
| 57 |
+
###### For Windows10:
|
| 58 |
+
|
| 59 |
+
- Most of the code setting would be same, except for the following:
|
| 60 |
+
1. You cannot use "mkdir myfirstApp && cd "$_"" in one line, instead, you create your directory firstly by "mkdir myfirstApp"
|
| 61 |
+
then "cd myfirstApp";
|
| 62 |
+
2. to create a empty app.py file, you type
|
| 63 |
+
```cmd
|
| 64 |
+
echo.> app.py
|
| 65 |
+
```
|
| 66 |
+
3. you do not need nohup, just type:
|
| 67 |
+
```cmd
|
| 68 |
+
streamlit run app.py
|
| 69 |
+
```
|
| 70 |
+
#### 3. Try an input widget in streamlit
|
| 71 |
+
Now, you can try to add an input widget to your app.py
|
| 72 |
+
|
| 73 |
+
```python
|
| 74 |
+
## just below your existing code, the st.header() one, type following:
|
| 75 |
+
|
| 76 |
+
text = st.text_area("copy and paste your text here",
|
| 77 |
+
height = 20,
|
| 78 |
+
max_chars = 3000,
|
| 79 |
+
key='text_area')
|
| 80 |
+
```
|
| 81 |
+
- the code above, we defined an user input widget, text_area, give it a variable name, called 'text'
|
| 82 |
+
This allow our app user to copy and paste text to summarize here, and the pasted text will be given a name 'text'
|
| 83 |
+
- Now how can we use this input? i.e. call the data in our app? try the following code below the previous one
|
| 84 |
+
|
| 85 |
+
```python
|
| 86 |
+
if st.button('submit'):
|
| 87 |
+
st.write(text)
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
- save the app.py file by click "File" --> "Save" at the left top of the editing window of notebook
|
| 91 |
+
- Rerun the app at your browser, see what happen?
|
| 92 |
+
- Try to copy and paste a paragraph of text, then click the button, see what happen?
|
| 93 |
+
- Can you figure out the logic and process behind the code so far?
|
| 94 |
+
|
| 95 |
+
#### 4. Familiar with Python
|
| 96 |
+
|
| 97 |
+
Read the following introduction below for Python Dictionary and if statement:
|
| 98 |
+
- [Python Dictionary](https://www.w3schools.com/python/python_dictionaries.asp)
|
| 99 |
+
- [Python Condition](https://www.w3schools.com/python/python_conditions.asp)
|
| 100 |
+
|
| 101 |
+
You can copy and paste those code snippets into your jupyter notebook to play around with them.
|
| 102 |
+
|
| 103 |
+
And do some exercises if you want :smile:
|
| 104 |
+
|
| 105 |
+
- [Dictionary exercise](https://www.w3schools.com/python/python_dictionaries_exercises.asp)
|
| 106 |
+
- [Condition exercise](https://www.w3schools.com/python/exercise.asp?filename=exercise_ifelse1)
|
| 107 |
+
|
| 108 |
+
""")
|
pages/3_302_Our_First_App.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from utils import *
|
| 3 |
+
st.set_page_config(page_title="The first App", page_icon=":tangerine:")
|
| 4 |
+
|
| 5 |
+
if check_password():
|
| 6 |
+
st.markdown("## Welcome to the first app: summarizer")
|
| 7 |
+
st.write("##")
|
| 8 |
+
|
| 9 |
+
st.markdown("We have seen our summarization prompt from our notebook")
|
| 10 |
+
st.code('''
|
| 11 |
+
numberOfWords = '20'
|
| 12 |
+
topics = "marry"
|
| 13 |
+
|
| 14 |
+
prompt = f"""
|
| 15 |
+
Your task is to generate a short summary from text below, delimited by triple backticks, in at most {numberOfWords} words,\n
|
| 16 |
+
Firstly, extract relevant information and create a list of keywords without response,\n
|
| 17 |
+
Then, check if {topics} is in your list, if not, just response no relevent topics about {topics} to summarise,\n
|
| 18 |
+
if it is in your list, focusing on any aspects that mention {topics},\n
|
| 19 |
+
Review: ```{text}```
|
| 20 |
+
"""
|
| 21 |
+
''', language = "python")
|
| 22 |
+
|
| 23 |
+
st.markdown("""
|
| 24 |
+
We have 3 variables above, numberOfWords, topics, and text, which means, our app will give users
|
| 25 |
+
3 input windows or widgets,
|
| 26 |
+
- a Text input window for the text that need to be summarized, where users can copy and paste large text into it;
|
| 27 |
+
- a short text input window, that user can choose to choose the topics he/she want the app to summarize;
|
| 28 |
+
- a number input window, that user can select the length of summarization;
|
| 29 |
+
|
| 30 |
+
- Additionally, a secret window that allow users to pass their own OpenAI API Key to use ChatGPT
|
| 31 |
+
""")
|
| 32 |
+
st.write("##")
|
| 33 |
+
st.markdown("""
|
| 34 |
+
The following streamlit widgets can help us to achieve requirement above:
|
| 35 |
+
|
| 36 |
+
1. [st.text_area](https://docs.streamlit.io/library/api-reference/widgets/st.text_area)
|
| 37 |
+
|
| 38 |
+
Try to copy the following code to your app.py and rerun the local streamlit server
|
| 39 |
+
|
| 40 |
+
```python
|
| 41 |
+
txt = st.text_area('Text to summarize', "")
|
| 42 |
+
st.write(txt)
|
| 43 |
+
```
|
| 44 |
+
""")
|
| 45 |
+
|
| 46 |
+
txt = st.text_area('Text to summarize', "")
|
| 47 |
+
st.write(txt)
|
| 48 |
+
|
| 49 |
+
st.write("##")
|
| 50 |
+
st.markdown("""
|
| 51 |
+
2. [st.text_input](https://docs.streamlit.io/library/api-reference/widgets/st.text_input)
|
| 52 |
+
|
| 53 |
+
Copy the code and run it on your app.py
|
| 54 |
+
|
| 55 |
+
```python
|
| 56 |
+
topics = st.text_input('Choose topics, seperated by ,', '')
|
| 57 |
+
st.write(topics)
|
| 58 |
+
```
|
| 59 |
+
""")
|
| 60 |
+
|
| 61 |
+
topics = st.text_input('Choose topics, seperated by ,', '')
|
| 62 |
+
st.write(topics)
|
| 63 |
+
st.write("##")
|
| 64 |
+
|
| 65 |
+
st.markdown("""
|
| 66 |
+
3. [st.number_input](https://docs.streamlit.io/library/api-reference/widgets/st.number_input)
|
| 67 |
+
|
| 68 |
+
Copy the code ane rerun on your app.py
|
| 69 |
+
```python
|
| 70 |
+
number = st.number_input('words limit')
|
| 71 |
+
st.write('The current number is ', number)
|
| 72 |
+
```
|
| 73 |
+
""")
|
| 74 |
+
|
| 75 |
+
number = st.number_input('words limit')
|
| 76 |
+
st.write('The current number is ', number)
|
| 77 |
+
st.write("##")
|
| 78 |
+
st.markdown("""
|
| 79 |
+
Now, your task is to download the initial app this week:
|
| 80 |
+
|
| 81 |
+
- Read the code;
|
| 82 |
+
- figuring our where are those widgets located in the code;
|
| 83 |
+
- ask any question in the Discord;
|
| 84 |
+
- bravely change the code on your computer, do some experiment
|
| 85 |
+
|
| 86 |
+
:smile:
|
| 87 |
+
""")
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|