CoolFace
Apppublic

awacke1/TensorFlowForTheWin

sourceHugging Facemitupdated 2y agoView on Hugging Face
2likes
app.py255 linesDownload Raw Back to root
1import streamlit as st2import pandas as pd3import subprocess4import time5import random6import numpy as np7import tensorflow as tf8from tensorflow.keras import layers, models9from transformers import BertTokenizer, TFBertModel10import requests11import matplotlib.pyplot as plt12from io import BytesIO13import base6414 15# ---------------------------- Helper Function for NER Data ----------------------------16 17def generate_ner_data():18    # Sample NER data for different entities19    data_person = [{"text": f"Person example {i}", "entities": [{"entity": "Person", "value": f"Person {i}"}]} for i in range(1, 21)]20    data_organization = [{"text": f"Organization example {i}", "entities": [{"entity": "Organization", "value": f"Organization {i}"}]} for i in range(1, 21)]21    data_location = [{"text": f"Location example {i}", "entities": [{"entity": "Location", "value": f"Location {i}"}]} for i in range(1, 21)]22    data_date = [{"text": f"Date example {i}", "entities": [{"entity": "Date", "value": f"Date {i}"}]} for i in range(1, 21)]23    data_product = [{"text": f"Product example {i}", "entities": [{"entity": "Product", "value": f"Product {i}"}]} for i in range(1, 21)]24    25    # Create a dictionary of all NER examples26    ner_data = {27        "Person": data_person,28        "Organization": data_organization,29        "Location": data_location,30        "Date": data_date,31        "Product": data_product32    }33    34    return ner_data35 36# ---------------------------- Fun NER Data Function ----------------------------37 38def ner_demo():39    st.header("๐Ÿค– LLM NER Model Demo ๐Ÿ•ต๏ธโ€โ™€๏ธ")40    41    # Generate NER data42    ner_data = generate_ner_data()43 44    # Pick a random entity type to display45    entity_type = random.choice(list(ner_data.keys()))46    st.subheader(f"Here comes the {entity_type} entity recognition, ready to show its magic! ๐ŸŽฉโœจ")47 48    # Select a random record to display49    example = random.choice(ner_data[entity_type])50    st.write(f"Analyzing: *{example['text']}*")51    52    # Display recognized entity53    for entity in example["entities"]:54        st.success(f"๐Ÿ” Found a {entity['entity']}: **{entity['value']}**")55    56    # A bit of rhyme to lighten up the task57    st.write("There once was an AI so bright, ๐ŸŽ‡")58    st.write("It could spot any name in sight, ๐Ÿ‘๏ธ")59    st.write("With a click or a tap, it put on its cap, ๐ŸŽฉ")60    st.write("And found entities day or night! ๐ŸŒ™")61 62# ---------------------------- Helper: Text Data Augmentation ----------------------------63 64def word_subtraction(text):65    """Subtract words at random positions."""66    words = text.split()67    if len(words) > 2:68        index = random.randint(0, len(words) - 1)69        words.pop(index)70    return " ".join(words)71 72def word_recombination(text):73    """Recombine words with random shuffling."""74    words = text.split()75    random.shuffle(words)76    return " ".join(words)77 78# ---------------------------- ML Model Building ----------------------------79 80def build_small_model(input_shape):81    model = models.Sequential()82    model.add(layers.Dense(64, activation='relu', input_shape=(input_shape,)))83    model.add(layers.Dense(32, activation='relu'))84    model.add(layers.Dense(1, activation='sigmoid'))85    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])86    return model87 88# ---------------------------- TensorFlow and Keras Integration ----------------------------89 90def train_model_demo():91    st.header("๐Ÿงช Let's Build a Mini TensorFlow Model ๐ŸŽ“")92 93    # Generate random synthetic data for simplicity94    data_size = 10095    X_train = np.random.rand(data_size, 10)96    y_train = np.random.randint(0, 2, size=data_size)97    98    st.write(f"๐Ÿš€ **Data Shape**: {X_train.shape}, with binary target labels.")99    100    # Build the model101    model = build_small_model(X_train.shape[1])102    103    st.write("๐Ÿ”ง **Model Summary**:")104    st.text(model.summary())105 106    # Train the model107    st.write("๐Ÿš€ **Training the model...**")108    history = model.fit(X_train, y_train, epochs=5, batch_size=16, verbose=0)109 110    # Output training results humorously111    st.success("๐ŸŽ‰ Training completed! The model now knows its ABCs... or 1s and 0s at least! ๐Ÿ˜‚")112 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 118def 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 128def 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 138def matplotlib_plot_example():139    st.header("๐Ÿ“ˆ Matplotlib Plot Example ๐Ÿ“Š")140 141    # Generate data142    x = np.linspace(0, 10, 100)143    y = np.sin(x)144 145    # Create plot146    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 153def cache_example():154    st.header("โšก Streamlit Cache Example ๐Ÿš€")155 156    @st.cache157    def expensive_computation(a, b):158        time.sleep(2)159        return a * b160 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 167def 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 #MachineLearning175    </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 186st.set_page_config(page_title="LLMs and Tiny ML Models", page_icon="๐Ÿค–", layout="wide", initial_sidebar_state="expanded")187st.title("๐Ÿค–๐Ÿ“Š LLMs and Tiny ML Models with TensorFlow ๐Ÿ“Š๐Ÿค–")188st.markdown("This app demonstrates how to build small TensorFlow models, solve common developer problems, and augment text data using word subtraction and recombination strategies.")189st.markdown("---")190 191# ---------------------------- Main Navigation ----------------------------192 193st.sidebar.title("Navigation")194options = st.sidebar.radio("Go to", ['NER Demo', 'TensorFlow Model', 'Text Augmentation', 'Code Sharing', 'File Uploader', 'Matplotlib Plot', 'Streamlit Cache', 'Tweet Spotlight'])195 196if 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 202elif options == 'TensorFlow Model':203    if st.button('๐Ÿš€ Build and Train a TensorFlow Model'):204        train_model_demo()205 206elif 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 217elif options == 'Code Sharing':218    code_snippet_sharing()219 220elif options == 'File Uploader':221    file_uploader_example()222 223elif options == 'Matplotlib Plot':224    matplotlib_plot_example()225 226elif options == 'Streamlit Cache':227    cache_example()228 229elif options == 'Tweet Spotlight':230    display_tweet()231 232st.markdown("---")233 234# ---------------------------- Footer and Additional Resources ----------------------------235 236st.subheader("๐Ÿ“š Additional Resources")237st.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 ----------------------------246st.markdown('''247Reference Libraries:248plaintext249streamlit250pandas251numpy252tensorflow253transformers254matplotlib255''')