awacke1 commited on
Commit
288997f
Β·
verified Β·
1 Parent(s): 5f77bb2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -30
app.py CHANGED
@@ -7,6 +7,10 @@ import numpy as np
7
  import tensorflow as tf
8
  from tensorflow.keras import layers, models
9
  from transformers import BertTokenizer, TFBertModel
 
 
 
 
10
 
11
  # ---------------------------- Helper Function for NER Data ----------------------------
12
 
@@ -109,42 +113,122 @@ def train_model_demo():
109
  st.write(f"Final training loss: **{history.history['loss'][-1]:.4f}**, accuracy: **{history.history['accuracy'][-1]:.4f}**")
110
  st.write("Fun fact: This model can make predictions on binary outcomes like whether a cat will sleep or not. πŸ±πŸ’€")
111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  # ---------------------------- Header and Introduction ----------------------------
113
 
114
  st.set_page_config(page_title="LLMs and Tiny ML Models", page_icon="πŸ€–", layout="wide", initial_sidebar_state="expanded")
115
  st.title("πŸ€–πŸ“Š LLMs and Tiny ML Models with TensorFlow πŸ“ŠπŸ€–")
116
- st.markdown("This app demonstrates how to build a small TensorFlow model and augment text data using word subtraction and recombination strategies.")
117
  st.markdown("---")
118
 
119
- # ---------------------------- Call NER Demo ----------------------------
120
 
121
- if st.button('πŸ§ͺ Run NER Model Demo'):
122
- ner_demo()
123
- else:
124
- st.write("Click the button above to start the AI NER magic! 🎩✨")
125
 
126
- # ---------------------------- TensorFlow Demo ----------------------------
 
 
 
 
127
 
128
- if st.button('πŸš€ Build and Train a TensorFlow Model'):
129
- train_model_demo()
 
130
 
131
- st.markdown("---")
 
 
 
 
 
 
 
 
 
132
 
133
- # ---------------------------- Fun Text Augmentation ----------------------------
 
134
 
135
- st.subheader("🎲 Fun Text Augmentation with Random Strategies 🎲")
 
136
 
137
- input_text = st.text_input("Enter a sentence to see some augmentation magic! ✨", "TensorFlow is awesome!")
 
138
 
139
- if st.button("Subtract Random Words"):
140
- st.write(f"Original: **{input_text}**")
141
- st.write(f"Augmented: **{word_subtraction(input_text)}**")
142
 
143
- if st.button("Recombine Words"):
144
- st.write(f"Original: **{input_text}**")
145
- st.write(f"Augmented: **{word_recombination(input_text)}**")
146
 
147
- st.write("Try both and see how the magic works! 🎩✨")
148
  st.markdown("---")
149
 
150
  # ---------------------------- Footer and Additional Resources ----------------------------
@@ -152,15 +236,20 @@ st.markdown("---")
152
  st.subheader("πŸ“š Additional Resources")
153
  st.markdown("""
154
  - [Official Streamlit Documentation](https://docs.streamlit.io/)
155
- - [pip-audit GitHub Repository](https://github.com/pypa/pip-audit)
156
- - [Mermaid Live Editor](https://mermaid.live/) - Design and preview Mermaid diagrams.
157
- - [Azure Container Apps Documentation](https://docs.microsoft.com/en-us/azure/container-apps/)
158
- - [Cybersecurity Best Practices by CISA](https://www.cisa.gov/cybersecurity-best-practices)
159
  """)
160
 
161
- # ---------------------------- Self-Assessment ----------------------------
162
-
163
- # Score: 9.5/10
164
- # Rationale: This app integrates TensorFlow for building a small neural network and adds playful text augmentation techniques. The humorous elements, interactive outputs, and functional demonstrations create an engaging learning experience.
165
- # Points for improvement: Could include more interactive model-building features, such as allowing users to adjust model layers or input shapes.
166
-
 
 
 
 
 
 
7
  import tensorflow as tf
8
  from tensorflow.keras import layers, models
9
  from transformers import BertTokenizer, TFBertModel
10
+ import requests
11
+ import matplotlib.pyplot as plt
12
+ from io import BytesIO
13
+ import base64
14
 
15
  # ---------------------------- Helper Function for NER Data ----------------------------
16
 
 
113
  st.write(f"Final training loss: **{history.history['loss'][-1]:.4f}**, accuracy: **{history.history['accuracy'][-1]:.4f}**")
114
  st.write("Fun fact: This model can make predictions on binary outcomes like whether a cat will sleep or not. πŸ±πŸ’€")
115
 
116
+ # ---------------------------- Additional Useful Examples ----------------------------
117
+
118
+ def code_snippet_sharing():
119
+ st.header("πŸ“€ Code Snippet Sharing with Syntax Highlighting πŸ–₯️")
120
+
121
+ code = '''def hello_world():
122
+ print("Hello, world!")'''
123
+
124
+ st.code(code, language='python')
125
+
126
+ st.write("Developers often need to share code snippets. Here's how you can display code with syntax highlighting in Streamlit! 🌈")
127
+
128
+ def file_uploader_example():
129
+ st.header("πŸ“ File Uploader Example πŸ“€")
130
+
131
+ uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
132
+ if uploaded_file is not None:
133
+ data = pd.read_csv(uploaded_file)
134
+ st.write("πŸŽ‰ File uploaded successfully!")
135
+ st.dataframe(data.head())
136
+ st.write("Use file uploaders to allow users to bring their own data into your app! πŸ“Š")
137
+
138
+ def matplotlib_plot_example():
139
+ st.header("πŸ“ˆ Matplotlib Plot Example πŸ“Š")
140
+
141
+ # Generate data
142
+ x = np.linspace(0, 10, 100)
143
+ y = np.sin(x)
144
+
145
+ # Create plot
146
+ fig, ax = plt.subplots()
147
+ ax.plot(x, y)
148
+ ax.set_title('Sine Wave')
149
+ st.pyplot(fig)
150
+
151
+ st.write("You can integrate Matplotlib plots directly into your Streamlit app! 🎨")
152
+
153
+ def cache_example():
154
+ st.header("⚑ Streamlit Cache Example πŸš€")
155
+
156
+ @st.cache
157
+ def expensive_computation(a, b):
158
+ time.sleep(2)
159
+ return a * b
160
+
161
+ st.write("Let's compute something that takes time...")
162
+ result = expensive_computation(2, 21)
163
+ st.write(f"The result is {result}. But thanks to caching, it's faster the next time! ⚑")
164
+
165
+ # ---------------------------- Display Tweet ----------------------------
166
+
167
+ def display_tweet():
168
+ st.header("🐦 Tweet Spotlight: TensorFlow and Transformers 🌟")
169
+
170
+ tweet_html = '''
171
+ <blockquote class="twitter-tweet">
172
+ <p lang="en" dir="ltr">
173
+ Just tried integrating TensorFlow with Transformers for my latest LLM project! πŸš€
174
+ The synergy between them is incredible. TensorFlow's flexibility combined with Transformers' power boosts Generative AI capabilities to new heights! πŸ”₯ #TensorFlow #Transformers #AI #MachineLearning
175
+ </p>&mdash; AI Enthusiast (@ai_enthusiast) <a href="https://twitter.com/ai_enthusiast/status/1234567890">September 30, 2024</a>
176
+ </blockquote>
177
+ <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
178
+ '''
179
+
180
+ st.components.v1.html(tweet_html, height=300)
181
+
182
+ st.write("Tweets can be embedded to showcase social proof or updates. Isn't that neat? 🐀")
183
+
184
  # ---------------------------- Header and Introduction ----------------------------
185
 
186
  st.set_page_config(page_title="LLMs and Tiny ML Models", page_icon="πŸ€–", layout="wide", initial_sidebar_state="expanded")
187
  st.title("πŸ€–πŸ“Š LLMs and Tiny ML Models with TensorFlow πŸ“ŠπŸ€–")
188
+ st.markdown("This app demonstrates how to build small TensorFlow models, solve common developer problems, and augment text data using word subtraction and recombination strategies.")
189
  st.markdown("---")
190
 
191
+ # ---------------------------- Main Navigation ----------------------------
192
 
193
+ st.sidebar.title("Navigation")
194
+ options = st.sidebar.radio("Go to", ['NER Demo', 'TensorFlow Model', 'Text Augmentation', 'Code Sharing', 'File Uploader', 'Matplotlib Plot', 'Streamlit Cache', 'Tweet Spotlight'])
 
 
195
 
196
+ if options == 'NER Demo':
197
+ if st.button('πŸ§ͺ Run NER Model Demo'):
198
+ ner_demo()
199
+ else:
200
+ st.write("Click the button above to start the AI NER magic! 🎩✨")
201
 
202
+ elif options == 'TensorFlow Model':
203
+ if st.button('πŸš€ Build and Train a TensorFlow Model'):
204
+ train_model_demo()
205
 
206
+ elif options == 'Text Augmentation':
207
+ st.subheader("🎲 Fun Text Augmentation with Random Strategies 🎲")
208
+ input_text = st.text_input("Enter a sentence to see some augmentation magic! ✨", "TensorFlow is awesome!")
209
+ if st.button("Subtract Random Words"):
210
+ st.write(f"Original: **{input_text}**")
211
+ st.write(f"Augmented: **{word_subtraction(input_text)}**")
212
+ if st.button("Recombine Words"):
213
+ st.write(f"Original: **{input_text}**")
214
+ st.write(f"Augmented: **{word_recombination(input_text)}**")
215
+ st.write("Try both and see how the magic works! 🎩✨")
216
 
217
+ elif options == 'Code Sharing':
218
+ code_snippet_sharing()
219
 
220
+ elif options == 'File Uploader':
221
+ file_uploader_example()
222
 
223
+ elif options == 'Matplotlib Plot':
224
+ matplotlib_plot_example()
225
 
226
+ elif options == 'Streamlit Cache':
227
+ cache_example()
 
228
 
229
+ elif options == 'Tweet Spotlight':
230
+ display_tweet()
 
231
 
 
232
  st.markdown("---")
233
 
234
  # ---------------------------- Footer and Additional Resources ----------------------------
 
236
  st.subheader("πŸ“š Additional Resources")
237
  st.markdown("""
238
  - [Official Streamlit Documentation](https://docs.streamlit.io/)
239
+ - [TensorFlow Documentation](https://www.tensorflow.org/api_docs)
240
+ - [Transformers Documentation](https://huggingface.co/docs/transformers/index)
241
+ - [Streamlit Cheat Sheet](https://docs.streamlit.io/library/cheatsheet)
242
+ - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html)
243
  """)
244
 
245
+ # ---------------------------- requirements.txt ----------------------------
246
+ st.markdown('''
247
+ Reference Libraries:
248
+ plaintext
249
+ streamlit
250
+ pandas
251
+ numpy
252
+ tensorflow
253
+ transformers
254
+ matplotlib
255
+ ''')