|
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 |
|
|
|
|
|
@st.cache_resource |
|
def load_diffuser(): |
|
return StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5") |
|
|
|
|
|
st.sidebar.title("π€ Hugging Face Libraries Demo") |
|
library = st.sidebar.selectbox("Choose a library", ["Transformers", "Diffusers", "Datasets", "PEFT", "Accelerate", "Optimum"]) |
|
|
|
|
|
if library == "Transformers": |
|
st.header("π Transformers: Text Processing Powerhouse") |
|
st.write("Analyze or transform text with pre-trained NLP models. Try it out below! π") |
|
|
|
|
|
|
|
|
|
|
|
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})") |
|
|
|
|
|
|
|
|
|
st.write("π€ - Mask filling with pipeline. Ex: 'The [MASK] is blue' β sky | Guess master! β¨") |
|
|
|
|
|
elif library == "Diffusers": |
|
st.header("πΌοΈ Diffusers: Image Generation Magic") |
|
st.write("Turn text into stunning images with diffusion models! Let's create something cool. π¨") |
|
|
|
|
|
|
|
|
|
|
|
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! π") |
|
|
|
|
|
|
|
|
|
|
|
elif library == "Datasets": |
|
st.header("π Datasets: Ready-to-Use Data") |
|
st.write("Explore datasets for training or analysis. Pick one and see a sample! π") |
|
|
|
|
|
|
|
|
|
|
|
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! π¬") |
|
|
|
|
|
|
|
|
|
|
|
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! π¦") |
|
|
|
|
|
elif library == "PEFT": |
|
st.header("βοΈ PEFT: Efficient Fine-Tuning") |
|
st.write("Learn about parameter-efficient fine-tuning (simplified demo).") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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! πͺ") |
|
|
|
|
|
elif library == "Accelerate": |
|
st.header("π Accelerate: Device Optimization") |
|
st.write("Optimize inference across devices with Accelerate.") |
|
|
|
|
|
|
|
|
|
|
|
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! β‘") |
|
|
|
|
|
elif library == "Optimum": |
|
st.header("β‘ Optimum: Hardware Acceleration") |
|
st.write("Speed up inference with optimized models (e.g., ONNX).") |
|
|
|
|
|
|
|
|
|
|
|
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! ποΈ") |