File size: 10,031 Bytes
7c520c0 e1ddba2 7c520c0 e1ddba2 7c520c0 e1ddba2 7c520c0 e1ddba2 7c520c0 e1ddba2 7c520c0 e1ddba2 7c520c0 e1ddba2 7c520c0 e1ddba2 7c520c0 e1ddba2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
import streamlit as st
from transformers import pipeline, AutoModel, AutoTokenizer
from diffusers import StableDiffusionPipeline, DiffusionPipeline, DDIMScheduler
from datasets import load_dataset, Dataset
from peft import PeftModel, PeftConfig, get_peft_model
from accelerate import Accelerator
from optimum.onnxruntime import ORTModelForSequenceClassification
import torch
from PIL import Image
import pandas as pd
# Cache resource-intensive models for performance
@st.cache_resource
def load_diffuser():
return StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
# Sidebar navigation
st.sidebar.title("π€ Hugging Face Libraries Demo")
library = st.sidebar.selectbox("Choose a library", ["Transformers", "Diffusers", "Datasets", "PEFT", "Accelerate", "Optimum"])
# Transformers Section
if library == "Transformers":
st.header("π Transformers: Text Processing Powerhouse")
st.write("Analyze or transform text with pre-trained NLP models. Try it out below! π")
# pipeline: Easy inference for NLP tasks
# π - Sentiment analysis with pipeline. Ex: 'I love this!' β Positive | Emotions in a snap! π
# βοΈ - Text generation with pipeline. Ex: 'Once upon a time' β Story | Spin tales fast! π
# β - Question answering with pipeline. Ex: 'Who won?' β Answer | Quiz master ready! π§
task = st.selectbox("Pick a Task", ["Sentiment Analysis ππ’", "Text Generation βοΈ", "Fill Mask π€"])
text_input = st.text_area("Enter text here βοΈ", "", placeholder="Type your text...")
if st.button("Process Text π", disabled=not text_input):
with st.spinner("Processing... β³"):
if "Sentiment" in task:
classifier = pipeline("sentiment-analysis")
result = classifier(text_input)
st.success(f"**Sentiment Result:** {result[0]['label']} (Confidence: {result[0]['score']:.2f}) β
")
st.write("π - Sentiment analysis with pipeline. Ex: 'I love this!' β Positive | Emotions in a snap! π")
elif "Text Generation" in task:
generator = pipeline("text-generation")
result = generator(text_input, max_length=50)[0]['generated_text']
st.success(f"**Generated Text:** {result} β
")
st.write("βοΈ - Text generation with pipeline. Ex: 'Once upon a time' β Story | Spin tales fast! π")
elif "Fill Mask" in task:
fill_mask = pipeline("fill-mask")
results = fill_mask(text_input.replace("[MASK]", fill_mask.tokenizer.mask_token))
for res in results[:3]:
st.write(f"Prediction: {res['sequence']} (Score: {res['score']:.2f})")
# AutoModel & AutoTokenizer: Load models and tokenizers
# π€ - Mask filling with AutoModel. Ex: 'The [MASK] is blue' β sky | Guess master! β¨
# π - Tokenizing with AutoTokenizer. Ex: 'Hello world' β tokens | Words to numbers! π’
# βοΈ - Custom inference with AutoModel. Ex: 'BERT for sentiment' β logits | Model magic! πͺ
st.write("π€ - Mask filling with pipeline. Ex: 'The [MASK] is blue' β sky | Guess master! β¨")
# Diffusers Section
elif library == "Diffusers":
st.header("πΌοΈ Diffusers: Image Generation Magic")
st.write("Turn text into stunning images with diffusion models! Let's create something cool. π¨")
# StableDiffusionPipeline: Text-to-image generation
# πΌοΈ - Text-to-image with StableDiffusionPipeline. Ex: 'Cat in space' β Image | Art from words! π
# π¨ - Style transfer with StableDiffusionPipeline. Ex: 'Van Gogh cat' β Image | Paint like pros! ποΈ
# π - Creative prompts with StableDiffusionPipeline. Ex: 'Dream city' β Image | Imagine it, see it! β¨
prompt = st.text_input("Enter a Creative Prompt π¨", "", placeholder="e.g., 'A cat in space'")
if st.button("Generate Image π", disabled=not prompt):
with st.spinner("Generating image... β³"):
pipe = load_diffuser()
image = pipe(prompt).images[0]
st.image(image, caption="Generated Image", use_column_width=True)
st.write("πΌοΈ - Text-to-image with StableDiffusionPipeline. Ex: 'Cat in space' β Image | Art from words! π")
# DiffusionPipeline & DDIMScheduler: Advanced diffusion control
# 𧩠- Inpainting with DiffusionPipeline. Ex: 'Fix broken image' β Restored | Puzzle solver! π οΈ
# π§ - Scheduling with DDIMScheduler. Ex: 'Fast diffusion' β Image | Speedy art! β‘
# Datasets Section
elif library == "Datasets":
st.header("π Datasets: Ready-to-Use Data")
st.write("Explore datasets for training or analysis. Pick one and see a sample! π")
# load_dataset: Access public datasets
# π - Load reviews with load_dataset. Ex: 'imdb' β Reviews | Movie buffs rejoice! π¬
# β - Load QA with load_dataset. Ex: 'squad' β Q&A | Trivia time! π§
# π£οΈ - Load audio with load_dataset. Ex: 'common_voice' β Audio | Hear the data! ποΈ
dataset_name = st.selectbox("Choose a Dataset", ["imdb π¬", "squad β"])
if st.button("Load Dataset π₯"):
with st.spinner("Fetching dataset... β³"):
dataset = load_dataset(dataset_name.split()[0], split="train[:5]")
st.success(f"**Dataset Loaded:** {dataset_name} (showing first 5 samples) β
")
st.write(dataset)
st.write("π - Load reviews with load_dataset. Ex: 'imdb' β Reviews | Movie buffs rejoice! π¬")
# Dataset.from_pandas: Convert DataFrames to datasets
# π - CSV to dataset with Dataset.from_pandas. Ex: 'Tweets CSV' β Dataset | Your data shines! π¦
# π - Analyze data with Dataset.from_pandas. Ex: 'Sales data' β Dataset | Numbers talk! π°
# π - Preprocess with Dataset.from_pandas. Ex: 'Raw text' β Dataset | Clean it up! π§Ή
st.subheader("Create Dataset from CSV")
uploaded_file = st.file_uploader("Upload a CSV file", type="csv")
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
dataset = Dataset.from_pandas(df)
st.write(dataset)
st.write("π - CSV to dataset with Dataset.from_pandas. Ex: 'Tweets CSV' β Dataset | Your data shines! π¦")
# PEFT Section (Simplified)
elif library == "PEFT":
st.header("βοΈ PEFT: Efficient Fine-Tuning")
st.write("Learn about parameter-efficient fine-tuning (simplified demo).")
# PeftModel: Load fine-tuned models
# βοΈ - Inference with PeftModel. Ex: 'Adapted BERT' β Output | Slim yet mighty! πͺ
# π€ - Custom tasks with PeftModel. Ex: 'NER tuning' β Tags | Precision tweak! π―
# π - Low resource with PeftModel. Ex: 'Small GPU' β Model | Efficiency wins! π
# PeftConfig: Configure PEFT settings
# π οΈ - LoRA setup with PeftConfig. Ex: 'Rank 8' β Config | Tune light! π
# π§ - Adapter config with PeftConfig. Ex: 'Task-specific' β Config | Fit like a glove! π§€
# β‘ - Fast tuning with PeftConfig. Ex: 'Quick fine-tune' β Config | Speedy prep! π
# get_peft_model: Wrap models with PEFT
# π - Enhance with get_peft_model. Ex: 'BERT + LoRA' β Model | Power boost! π
# π - Task adapt with get_peft_model. Ex: 'Sentiment' β Model | Tailored fit! βοΈ
# π - Multi-task with get_peft_model. Ex: 'QA + NER' β Model | Versatility rules! π
text = st.text_area("Enter Text to Classify βοΈ", "", placeholder="Type something...")
if st.button("Classify π", disabled=not text):
with st.spinner("Processing... β³"):
st.success("**Result:** Placeholder (PEFT reduces parameters efficiently!) β
")
st.write("βοΈ - Inference with PeftModel. Ex: 'Adapted BERT' β Output | Slim yet mighty! πͺ")
# Accelerate Section (Simplified)
elif library == "Accelerate":
st.header("π Accelerate: Device Optimization")
st.write("Optimize inference across devices with Accelerate.")
# Accelerator: Manage device placement
# π - GPU use with Accelerator. Ex: 'Auto GPU' β Device | Speed unleashed! β‘
# π - Multi-device with Accelerator. Ex: '2 GPUs' β Setup | Teamwork rocks! π€
# π οΈ - Easy setup with Accelerator. Ex: 'Model prep' β Ready | Smooth start! π
text = st.text_area("Enter Text for Sentiment Analysis βοΈ", "", placeholder="Type something...")
if st.button("Analyze π", disabled=not text):
with st.spinner("Analyzing... β³"):
accelerator = Accelerator()
classifier = pipeline("sentiment-analysis")
result = classifier(text)
st.success(f"**Result:** {result[0]['label']} (Confidence: {result[0]['score']:.2f}) β
")
st.write("π - GPU use with Accelerator. Ex: 'Auto GPU' β Device | Speed unleashed! β‘")
# Optimum Section (Simplified)
elif library == "Optimum":
st.header("β‘ Optimum: Hardware Acceleration")
st.write("Speed up inference with optimized models (e.g., ONNX).")
# ORTModelForSequenceClassification: ONNX-optimized classification
# β‘ - Fast classify with ORTModelForSequenceClassification. Ex: 'Text' β Label | Speed king! ποΈ
# π₯οΈ - CPU boost with ORTModelForSequenceClassification. Ex: 'Sentiment' β Result | No GPU? No prob! πͺ
# π - Efficient with ORTModelForSequenceClassification. Ex: 'Batch text' β Labels | Quick wins! π
text = st.text_area("Enter Text to Classify βοΈ", "", placeholder="Type something...")
if st.button("Classify π", disabled=not text):
with st.spinner("Processing... β³"):
st.success("**Result:** Placeholder (Optimum boosts speed!) β
")
st.write("β‘ - Fast classify with ORTModelForSequenceClassification. Ex: 'Text' β Label | Speed king! ποΈ") |