Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 117 |
st.markdown("---")
|
| 118 |
|
| 119 |
-
# ----------------------------
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
else:
|
| 124 |
-
st.write("Click the button above to start the AI NER magic! π©β¨")
|
| 125 |
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
|
|
|
| 130 |
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
-
|
|
|
|
| 134 |
|
| 135 |
-
|
|
|
|
| 136 |
|
| 137 |
-
|
|
|
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
st.write(f"Augmented: **{word_subtraction(input_text)}**")
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 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 |
-
- [
|
| 156 |
-
- [
|
| 157 |
-
- [
|
| 158 |
-
- [
|
| 159 |
""")
|
| 160 |
|
| 161 |
-
# ----------------------------
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 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>— 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 |
+
''')
|