liuyuqi-dellpc 1 year ago
parent
commit
c251736e04
4 changed files with 619 additions and 1 deletions
  1. 325 1
      _c1/attention.md
  2. 180 0
      _c1/auth20.md
  3. 82 0
      _c1/docker_deploy.md
  4. 32 0
      _c1/streamlit-authenticator.md

+ 325 - 1
_c1/attention.md

@@ -1,11 +1,335 @@
 ---
-title: 第三章:注意力机制
+title: 第一章:入门介绍
 author: SHENG XU
 date: 2022-05-04
 category: nlp
 layout: post
 ---
 
+# streamlit 介绍
+
+streamlit 是一个用于构建数据应用的开源 Python 库。它提供了一个简单的 API,可以让你在几行代码中构建数据应用。streamlit 会自动处理数据的加载、交互和可视化,让你可以专注于数据分析和应用的构建。
+
+无需写html, css,js等前端代码。
+
+```
+poetry new streamlit
+poetry add streamlit
+
+sreamlit hello
+```
+
+demo.py
+
+```
+import streamlit as st
+import pandas as pd
+import numpy as np
+import matplotlib.pyplot as plt
+
+st.markdown("demo")
+st.title("demo")
+
+st.header("1、标题")
+st.text("文本")
+st.write("文本")
+st.code("print('hello world')",language="python")
+
+st.header("2、表格")
+st.subheader("2.1、表格")
+
+
+df = pd.DataFrame(
+    np.random.randn(10, 5),
+    columns=('第 %d 列' % (i+1) for i in range(5))
+)
+
+st.table(df)
+# 高级图表:排序,高亮
+st.dataframe(df.style.highlight_max(axis=0))
+
+
+#监控面板
+col1, col2, col3=st.columns(3)
+col1.metric("指标1", 100, 200)
+col2.metric("指标2", 200, 100)
+col3.metric("指标3", 300, 300)
+
+# 折线图,生成3列随机数
+chart_data=pd.DataFrame(
+    np.random.randn(20,3),
+    columns=['a','b','c']
+)
+st.line_chart(chart_data)
+
+# 柱状图
+st.bar_chart(chart_data)
+
+# 面积图
+st.area_chart(chart_data)
+
+# 交互式图表
+
+# 地图
+map_data=pd.DataFrame(
+    np.random.randn(1000,2)/[50,50]+[37.76,-122.4],
+    columns=['lat','lon']
+)
+st.map(map_data)
+
+
+# 散点图
+
+def scatter_plot(data, color):
+    fig, ax=plt.subplots()
+    ax.scatter(data['lat'], data['lon'], color=color)
+    ax.set_xlabel('lat')
+    ax.set_ylabel('lon')
+    ax.set_title('scatter plot')
+    return fig
+
+fig = scatter_plot(map_data, 'red')
+st.pyplot(fig)
+```
+
+运行:
+```
+
+if __name__=='__main__'
+    streamlit_runner.run(run, 'demo.py')
+
+streamlit run demo.py
+```
+
+
+Streamlit 的一些原生图表组件,虽然做到了傻瓜式,但仅能输入数据、高度和宽度,如果你想更漂亮的图表,就像 matplotlib.pyplot、Altair、vega-lite、Plotly、Bokeh、PyDeck、Graphviz 那样,streamlit 也提供了支持
+
+
+# 交互,表单
+
+-   button:按钮
+-   download_button:文件下载
+-   file_uploader:文件上传
+-   checkbox:复选框
+-   radio:单选框
+-   selectbox:下拉单选框
+-   multiselect:下拉多选框
+-   slider:滑动条
+-   select_slider:选择条
+-   text_input:文本输入框
+-   text_area:文本展示框
+-   number_input:数字输入框,支持加减按钮
+-   date_input:日期选择框
+-   time_input:时间选择框
+-   color_picker:颜色选择器
+
+```
+import streamlit as st
+
+@st.cache_data
+def expensive_computation(a, b):
+    # 模拟一些很复杂的计算
+    return a * b
+
+input_a = st.number_input("输入a", value=1)
+input_b = st.number_input("输入b", value=1)
+
+output = expensive_computation(input_a, input_b)
+st.write("计算结果(:", output)
+
+
+name = st.text_input("请输入您的名字", value="John Doe")
+occupation = st.selectbox("请选择您的职业", ("学生", "教师", "工程师"))
+age = st.slider("请选择您的年龄", min_value=0, max_value=100, value=30, step=1)
+
+st.write(f"您好,{name}!您是一位{occupation},今年{age}岁。")
+
+
+```
+
+## 多媒体组件
+
+-   image:图片 st.image
+-   audio:音频 st.audio
+-   video:视频 st.video
+
+## 状态进度条
+
+-   progress:进度条,如游戏加载进度
+-   spinner:等待提示
+-   balloons:页面底部飘气球,表示祝贺
+-   error:显示错误信息
+-   warning:显示报警信息
+-   info:显示常规信息
+-   success:显示成功信息
+-   exception:显示异常信息(代码错误栈)
+
+## 页面,侧边栏,布局
+
+-   st.sidebar:侧边栏
+-   st.empty:空白占位
+-   st.markdown:Markdown
+-   st.columns:多列布局
+-   st.expander:展开收起
+-   st.beta_expander:展开收起
+-   st.container:容器
+
+
+```
+color = st.sidebar.color_picker('Pick A Color', '#00f900')
+```
+
+### 多页面交互,数据传递
+
+```
+
+main.py
+----pages
+---------home.py
+---------about.py
+
+import streamlit as st
+session_state=st.session_state
+session_state['page]='Home'
+
+sidebar_page=st.sidebar.radio('Page', ['Home', 'About'])
+
+if page =='Home':
+    st.title('Home')
+elif page =='About':
+    st.title('About')
+
+```
+
+## 
+
+st.stop
+st.form
+st.form_submit_button
+st.echo
+
+# 缓存
+st.cache
+
+```
+read_and_cache_csv=st.cache(pd.read_csv)
+df=read_and_cache_csv("data.csv", nrows=1000)
+desired_label=st.selectbox("Label", df.columns)
+st.write(df[desired_label].value_counts())
+
+```
+
+
+## streamlit 与其他库的结合
+
+### streamlit + matplotlib
+
+
+### steamlit + plotly
+
+
+### streamlit + FastAPI + mysql
+
+streamlit 登录等功能较弱,为此可以结合fastapi框架,实现登录验证等功能。
+
+```python
+import streamlit as st
+import requests
+
+```
+
+
+### 登录验证 + firebase
+
+每次操作互动组件时(比如输入新问题),都会触发重新运行(rerun)。rerun(重新运行)是streamlit的一个特色,指的是将应用代码从头到尾重新运行一遍。
+
+初始化firebase:
+```python
+#配置firebaseConfig
+firebaseConfig = {...}
+#然后进行初始化
+firebase = pyrebase.initialize_app(firebaseConfig)
+auth = firebase.auth()
+# Database
+db = firebase.database()
+storage = firebase.storage()
+
+```
+
+登录按钮:
+```
+if st.session_state['logged_in']:
+    st.sidebar.write(f"Hello, {st.session_state['username']}!")
+    if st.sidebar.button('Logout'):
+        st.session_state['logged_in'] = False
+        st.session_state['username'] = ''
+        st.session_state['history'] = []
+        st.session_state['generated'] = [] 
+        st.session_state['past'] = []  
+        st.experimental_rerun()
+else:
+    auth_option = st.sidebar.radio("Choose an option", ['Login', 'Register'])
+    email = st.sidebar.text_input("Email")
+    password = st.sidebar.text_input("Password", type='password')
+
+    if auth_option == 'Login':
+        if st.sidebar.button('Login'):
+            try:
+                user = auth.sign_in_with_email_and_password(email, password)
+                st.session_state['user'] = user
+                st.session_state['logged_in'] = True
+                st.session_state['username'] = email
+                st.session_state['email'] = email
+                st.success("Logged in successfully")
+            except:
+                error_message = "Invalid email or password"
+                st.error(error_message)  # Display the error message
+            st.experimental_rerun()
+
+    elif auth_option == 'Register':
+        if st.sidebar.button('Register'):
+            try:
+                user = auth.create_user_with_email_and_password(email, password)
+                st.session_state['logged_in'] = True
+                st.session_state['username'] = email
+                st.success("Registered and logged in successfully")
+                #st.experimental_rerun()
+            except:
+                error_message = "Failed to create account"
+                st.error(error_message)  # Display the error message
+```
+
+
+```
+if 'total_tokens_used' not in st.session_state:
+    st.session_state.total_tokens_used = 0
+
+
+with get_openai_callback() as cb:
+                output = conversational_chat(user_input)
+                token_cost = cb.total_tokens
+                st.session_state.total_tokens_used += token_cost
+                tokens_remaining -= token_cost
+```
+
+
+### streamlit + Flask
+
+## deploy
+
+默认部署到 https://share.streamlit.io/ ,也可以部署到自己的服务器上。
+
+Resources per app 1 GB
+Private apps 1 app
+Public apps Unlimited
+
+
+docker 部署, heroku.com 
+
+
+
+
+
 
 
 

+ 180 - 0
_c1/auth20.md

@@ -0,0 +1,180 @@
+
+---
+title: 第一章:google,github auth2.0 权鉴登录
+author: liuyuqi
+date: 2024-03-27
+category: nlp
+layout: post
+---
+
+### google,github auth2.0 权鉴登录
+
+```
+# utils/authenticate.py
+import streamlit as st
+import requests
+import json
+
+from config import FE_HOST, CAS_LOGIN_URL, ALLOWED_USERS
+
+
+# ------------------------------------
+# Initialise Streamlit state variables
+# ------------------------------------
+def initialise_st_state_vars():
+    if "auth_code" not in st.session_state:
+        st.session_state["auth_code"] = ""
+    if "authenticated" not in st.session_state:
+        st.session_state["authenticated"] = False
+    if "user_cognito_groups" not in st.session_state:
+        st.session_state["user_cognito_groups"] = []
+
+
+# ----------------------------------
+# Get authorization code after login
+# ----------------------------------
+def get_auth_code():
+    auth_query_params = st.experimental_get_query_params()
+    try:
+        auth_code = dict(auth_query_params)["sid"][0]
+    except (KeyError, TypeError):
+        auth_code = ""
+
+    return auth_code
+
+# -----------------------------
+# Set Streamlit state variables
+# -----------------------------
+def set_st_state_vars():
+    initialise_st_state_vars()
+    auth_code = get_auth_code()
+    user_info = get_auth_user(sid=auth_code) or {}
+
+    if user_info.get("user"):
+        st.session_state["auth_code"] = auth_code
+        st.session_state["authenticated"] = True
+        st.session_state["user"] = user_info.get("user")
+        st.session_state["email"] = user_info.get("mail")
+        st.session_state["display"] = user_info.get("display")
+
+
+# -----------------------------
+# Login/ Logout HTML components
+# -----------------------------
+login_link = f"{CAS_LOGIN_URL}?ref={FE_HOST}"
+
+
+html_css_login = """
+<style>
+.button-login {
+  background-color: skyblue;
+  color: white !important;
+  padding: 1em 1.5em;
+  text-decoration: none;
+  text-transform: uppercase;
+}
+
+.button-login:hover {
+  background-color: #555;
+  text-decoration: none;
+}
+
+.button-login:active {
+  background-color: black;
+}
+
+</style>
+"""
+
+html_button_login = (
+    html_css_login
+    + f"<a href='{login_link}' class='button-login' target='_self'>Log In</a>"
+)
+
+
+def button_login():
+    """
+
+    Returns:
+        Html of the login button.
+    """
+    _, col, _ = st.columns(3)
+    return col.markdown(f"{html_button_login}", unsafe_allow_html=True)
+
+
+def button_logout():
+    """
+
+    Returns:
+        Html of the logout button.
+    """
+    def logout_click():
+        st.session_state["authenticated"] = False
+    st.sidebar.button("Logout", on_click=logout_click)
+    print(st.session_state)
+
+
+def get_auth_user(sid, ref=FE_HOST):
+    cas_url = f"{CAS_LOGIN_URL}?sid=%s&ref=%s" % (sid, ref)
+    if not sid or not ref:
+        return
+
+    user_info = requests.get(cas_url ).text
+    try:
+        user_dict = json.loads(user_info)
+    except json.decoder.JSONDecodeError:
+        return
+    else:
+        return user_dict
+
+
+def is_allowed_user():
+    if st.session_state["email"] in ALLOWED_USERS:
+        return True
+    return False
+```
+
+在页面使用 authenticate,所有页面都需要添加权鉴:
+
+```
+import streamlit as st
+
+from utils import authenticate
+from utils.authenticate import is_allowed_user
+
+
+st.set_page_config(
+    page_title="Welcome",
+    page_icon="👋",
+)
+
+# 初始化鉴权变量
+authenticate.set_st_state_vars()
+
+# Add login/logout buttons,点击可跳转
+if not st.session_state.get("authenticated"):
+    st.warning("Please login!")
+    authenticate.button_login()
+else:
+    authenticate.button_logout()
+    if not is_allowed_user():
+        st.error("You do not have access. Please contact the administrator.")
+    else:
+        # else,页面展示代码位于通过鉴权后
+        st.title("欢迎使用XX仪表盘 👋")
+
+        st.markdown(
+            """
+           
+            该项目为streamlit跳转登陆测试项目\n
+            **👈 请从侧边栏进入功能页**
+            ### 官方参考文档
+            - Streamlit: [Streamlit](https://docs.streamlit.io/)
+            - 表单登陆: [streamlit-authenticator](https://blog.streamlit.io/streamlit-authenticator-part-1-adding-an-authentication-component-to-your-app/)
+        
+            ### 实现跳转登陆参考文档
+            - [参考文档](https://levelup.gitconnected.com/building-a-multi-page-app-with-streamlit-and-restricting-user-access-to-pages-using-aws-cognito-89a1fb5364a3)
+    
+        """
+        )
+```

+ 82 - 0
_c1/docker_deploy.md

@@ -0,0 +1,82 @@
+
+---
+title: 第一章:google,github auth2.0 权鉴登录
+author: liuyuqi
+date: 2024-03-27
+category: nlp
+layout: post
+---
+
+# docker 部署
+
+新建dockerfile
+
+Dockerfile
+
+```
+FROM python:3.9.4
+
+# Create the user that will run the app
+RUN adduser --disabled-password --gecos '' ml-api-user
+
+WORKDIR /opt/car_evaluation_api
+
+ARG PIP_EXTRA_INDEX_URL
+
+# Install requirements, including from Gemfury
+ADD ./car_evaluation_api /opt/car_evaluation_api
+RUN pip install --upgrade pip
+RUN pip install -r /opt/car_evaluation_api/requirements.txt
+
+RUN chmod +x /opt/car_evaluation_api/run.sh
+RUN chown -R ml-api-user:ml-api-user ./
+
+USER ml-api-user
+
+EXPOSE 8001
+
+CMD ["bash", "./run.sh"]
+```
+
+vim docker-compose.yml
+```
+version: '3'
+
+services:
+  car_evaluation_streamlit:
+    build:
+        dockerfile: car_evaluation_streamlit\Dockerfile
+    ports:
+      - 8501:8501
+    depends_on:
+      - car_evaluation_api
+
+  car_evaluation_api:
+    build:
+        dockerfile: car_evaluation_api\Dockerfile
+    ports:
+      - 8001:8001
+```
+
+运行:
+```
+docker-compose -f docker-compse.yml up -d --build
+```
+
+
+heroku login
+heroku create
+git push heroku master
+heroku ps:scale web=1
+
+heroku logs --tail
+
+heroku domains:add hivecnstats.iswbm.com
+
+
+
+
+
+
+
+

+ 32 - 0
_c1/streamlit-authenticator.md

@@ -0,0 +1,32 @@
+
+---
+title: 第一章:google,github auth2.0 权鉴登录
+author: liuyuqi
+date: 2024-03-27
+category: nlp
+layout: post
+---
+
+### streamlit-Authenticator身份认证
+
+```
+poetry add streamlit-authenticator
+```
+
+```python
+import streamlit as st
+import streamlit_authenticator as stauth
+
+passwords=["123456","1234567"]
+hashed_passwords = stauth.Hasher(passwords).generate()
+
+authenticator =stauth.Authenticator(names, usernames, hashed_passwords,cookie_name="authenticator",cookie_expire_days =30)
+
+if stauth.login(authenticator):
+    st.write("You are logged in")
+else:
+    st.write("You are not logged in")
+```
+
+
+