awacke1 commited on
Commit
89b83cd
·
0 Parent(s):

Duplicate from awacke1/Azure.Streamlit.Github.Actions.Azure.Container.Registry.Docker.AKS

Browse files
Files changed (5) hide show
  1. .gitattributes +34 -0
  2. README.md +14 -0
  3. Setup.md +237 -0
  4. app.py +43 -0
  5. requirements.txt +1 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Azure.Streamlit.Github.Actions.Azure.Container.Registry.Docker.AKS
3
+ emoji: 🌖
4
+ colorFrom: yellow
5
+ colorTo: pink
6
+ sdk: streamlit
7
+ sdk_version: 1.17.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ duplicated_from: awacke1/Azure.Streamlit.Github.Actions.Azure.Container.Registry.Docker.AKS
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
Setup.md ADDED
@@ -0,0 +1,237 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Write python streamlit application to use Terraform, Github Actions, Azure Container Registry and Github to set up a new streamlit app on azure.
2
+ To create a Streamlit application using Terraform, Github Actions, Azure Container Registry, and Github, you will need to follow these steps:
3
+
4
+ Create a new Streamlit application.
5
+ Push the application to a Github repository.
6
+ Configure the Azure Container Registry.
7
+ Set up a Github Actions workflow to build and push the Docker image to the Azure Container Registry.
8
+ Use Terraform to create an Azure Kubernetes Service (AKS) cluster and deploy the Streamlit app on it.
9
+ Here's an example implementation of these steps:
10
+
11
+ Create a new Streamlit app:
12
+ python
13
+ Copy code
14
+ # app.py
15
+ import streamlit as st
16
+
17
+ st.title("Hello, Streamlit!")
18
+ st.write("This is a simple Streamlit app deployed on Azure.")
19
+ Create a Dockerfile to containerize the app:
20
+ docker
21
+ Copy code
22
+ # Dockerfile
23
+ FROM python:3.8-slim
24
+
25
+ WORKDIR /app
26
+
27
+ COPY requirements.txt .
28
+ RUN pip install -r requirements.txt
29
+
30
+ COPY . .
31
+
32
+ CMD ["streamlit", "run", "app.py"]
33
+ Add a requirements.txt file with the necessary dependencies:
34
+ makefile
35
+ Copy code
36
+ streamlit==1.1.0
37
+ Push the application to a new Github repository.
38
+
39
+ Configure the Azure Container Registry:
40
+
41
+ Log in to the Azure Portal.
42
+ Create a new Azure Container Registry.
43
+ Copy the registry's login server name for later use.
44
+ Set up a Github Actions workflow to build and push the Docker image to the Azure Container Registry:
45
+ yaml
46
+ Copy code
47
+ # .github/workflows/main.yml
48
+ name: CI/CD
49
+
50
+ on:
51
+ push:
52
+ branches:
53
+ - main
54
+
55
+ jobs:
56
+ build-and-push:
57
+ runs-on: ubuntu-latest
58
+
59
+ steps:
60
+ - name: Checkout code
61
+ uses: actions/checkout@v2
62
+
63
+ - name: Set up QEMU
64
+ uses: docker/setup-qemu-action@v1
65
+
66
+ - name: Set up Docker Buildx
67
+ uses: docker/setup-buildx-action@v1
68
+
69
+ - name: Login to Azure Container Registry
70
+ run: echo ${{ secrets.ACR_PASSWORD }} | docker login ${{ secrets.ACR_REGISTRY }} -u ${{ secrets.ACR_USERNAME }} --password-stdin
71
+
72
+ - name: Build and push
73
+ uses: docker/build-push-action@v2
74
+ with:
75
+ context: .
76
+ push: true
77
+ tags: ${{ secrets.ACR_REGISTRY }}/streamlit-app:latest
78
+ Add the necessary secrets to your Github repository:
79
+ ACR_REGISTRY: The login server name of your Azure Container Registry.
80
+ ACR_USERNAME: The username for your Azure Container Registry.
81
+ ACR_PASSWORD: The password for your Azure Container Registry.
82
+ Create Terraform configuration files to set up the Azure Kubernetes Service (AKS) cluster and deploy the Streamlit app:
83
+ main.tf: This file will contain the main Terraform configuration.
84
+ variables.tf: This file will contain the Terraform variables.
85
+ outputs.tf: This file will contain the Terraform outputs.
86
+ hcl
87
+ Copy code
88
+ # main.tf
89
+ provider "azurerm" {
90
+ features {}
91
+ }
92
+
93
+ resource "azurerm_resource_group" "aks" {
94
+ name = "streamlit-aks-rg"
95
+ location = "East US"
96
+ }
97
+
98
+ resource "azurerm_kubernetes_cluster" "aks" {
99
+ name = "streamlit-aks"
100
+ location = azurerm_resource_group.aks.location
101
+ resource_group_name = azurerm_resource_group.aks.name
102
+ dns_prefix = "streamlit-aks"
103
+
104
+ default_node_pool {
105
+ name = "default"
106
+ node_count =
107
+
108
+ 3
109
+ vm_size = "Standard_DS2_v2"
110
+ }
111
+
112
+ identity {
113
+ type = "SystemAssigned"
114
+ }
115
+ }
116
+
117
+ resource "azurerm_kubernetes_cluster_node_pool" "streamlit" {
118
+ name = "streamlit"
119
+ kubernetes_cluster_id = azurerm_kubernetes_cluster.aks.id
120
+ vm_size = "Standard_DS2_v2"
121
+ node_count = 1
122
+ }
123
+
124
+ provider "kubernetes" {
125
+ host = azurerm_kubernetes_cluster.aks.kube_config.0.host
126
+ client_certificate = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_certificate)
127
+ client_key = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_key)
128
+ cluster_ca_certificate = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.cluster_ca_certificate)
129
+ }
130
+
131
+ resource "kubernetes_namespace" "streamlit" {
132
+ metadata {
133
+ name = "streamlit"
134
+ }
135
+ }
136
+
137
+ resource "kubernetes_deployment" "streamlit" {
138
+ metadata {
139
+ name = "streamlit"
140
+ namespace = kubernetes_namespace.streamlit.metadata.0.name
141
+ }
142
+
143
+ spec {
144
+ replicas = 1
145
+
146
+ selector {
147
+ match_labels = {
148
+ app = "streamlit"
149
+ }
150
+ }
151
+
152
+ template {
153
+ metadata {
154
+ labels = {
155
+ app = "streamlit"
156
+ }
157
+ }
158
+
159
+ spec {
160
+ container {
161
+ name = "streamlit"
162
+ image = "${var.acr_login_server}/streamlit-app:latest"
163
+
164
+ resources {
165
+ limits {
166
+ cpu = "1"
167
+ memory = "1Gi"
168
+ }
169
+
170
+ requests {
171
+ cpu = "500m"
172
+ memory = "500Mi"
173
+ }
174
+ }
175
+
176
+ port {
177
+ container_port = 8501
178
+ }
179
+ }
180
+ }
181
+ }
182
+ }
183
+ }
184
+
185
+ resource "kubernetes_service" "streamlit" {
186
+ metadata {
187
+ name = "streamlit"
188
+ namespace = kubernetes_namespace.streamlit.metadata.0.name
189
+ }
190
+
191
+ spec {
192
+ selector = {
193
+ app = "streamlit"
194
+ }
195
+
196
+ bash
197
+ Copy code
198
+ port {
199
+ port = 80
200
+ target_port = 8501
201
+ }
202
+
203
+ type = "LoadBalancer"
204
+ }
205
+ }
206
+
207
+ bash
208
+ Copy code
209
+
210
+ ```hcl
211
+ # variables.tf
212
+ variable "acr_login_server" {
213
+ description = "The Azure Container Registry login server"
214
+ type = string
215
+ }
216
+ hcl
217
+ Copy code
218
+ # outputs.tf
219
+ output "streamlit_url" {
220
+ value = "http://${kubernetes_service.streamlit.status.0.load_balancer.0.ingress.0.ip}"
221
+ description = "Streamlit app public URL"
222
+ }
223
+ Initialize Terraform and apply the configuration:
224
+ sh
225
+ Copy code
226
+ $ terraform init
227
+ $ terraform apply -var "acr_login_server=<your_acr_login_server>"
228
+ Replace <your_acr_login_server> with the login server name of your Azure Container Registry.
229
+
230
+ Access the Streamlit app using the public URL displayed in the Terraform output.
231
+ Please note that this is an example implementation and you may need to adjust the configurations according to your specific requirements and environment.
232
+
233
+
234
+
235
+
236
+
237
+
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from collections import Counter
3
+ import plotly.express as px
4
+ import numpy as np
5
+
6
+ def get_word_score(word):
7
+ # This function returns a score based on the length of the word
8
+ # Modify this function as per your requirements
9
+ score = len(word)**2
10
+ return score
11
+
12
+ def get_word_frequency(text):
13
+ # This function returns the word frequency of the given text
14
+ words = text.split()
15
+ word_frequency = Counter(words)
16
+ return word_frequency
17
+
18
+ # Load the markdown file
19
+ with open('Setup.md', 'r') as file:
20
+ text = file.read()
21
+
22
+
23
+ # Display the parsed markdown
24
+ st.markdown(text, unsafe_allow_html=True)
25
+
26
+ # Get the word frequency of the markdown text
27
+ word_frequency = get_word_frequency(text)
28
+
29
+ # Get the top words and their frequency
30
+ top_words = word_frequency.most_common(10)
31
+ top_words_dict = dict(top_words)
32
+
33
+ # Create a Plotly bar chart to display the top words and their frequency
34
+ fig = px.bar(x=list(top_words_dict.keys()), y=list(top_words_dict.values()), labels={'x':'Word', 'y':'Frequency'})
35
+ st.plotly_chart(fig)
36
+
37
+ # Calculate the scores for each word based on their length
38
+ word_scores = {word:get_word_score(word) for word in word_frequency}
39
+ top_word_scores = dict(sorted(word_scores.items(), key=lambda item: item[1], reverse=True)[:10])
40
+
41
+ # Create a Plotly bar chart to display the top words and their scores
42
+ fig = px.bar(x=list(top_word_scores.keys()), y=list(top_word_scores.values()), labels={'x':'Word', 'y':'Score'})
43
+ st.plotly_chart(fig)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ plotly