awacke1 commited on
Commit
e1ddba2
Β·
verified Β·
1 Parent(s): 7c520c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -166
app.py CHANGED
@@ -1,182 +1,159 @@
1
  import streamlit as st
2
- import streamlit.components.v1 as components
3
- from transformers import pipeline
4
- from diffusers import StableDiffusionPipeline
5
- from datasets import load_dataset
6
- from peft import PeftConfig
7
  from accelerate import Accelerator
8
  from optimum.onnxruntime import ORTModelForSequenceClassification
9
  import torch
10
- import time
 
11
 
12
- # Cache resource-intensive models
13
  @st.cache_resource
14
- def load_diffuser_model():
15
  return StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
16
 
17
  # Sidebar navigation
18
- st.sidebar.title("πŸ€— Libraries Demo")
19
- st.sidebar.markdown("Explore text πŸ“, images πŸ–ΌοΈ, and model ops πŸ”— with Hugging Face and Arcee!")
20
- page = st.sidebar.selectbox(
21
- "Choose a Section",
22
- [
23
- "🏠 Home",
24
- "πŸ”„ Workflow",
25
- "πŸ“ Transformers",
26
- "πŸ–ΌοΈ Diffusers",
27
- "πŸ“Š Datasets",
28
- "βš™οΈ PEFT",
29
- "πŸš€ Accelerate",
30
- "⚑ Optimum",
31
- "πŸ“š DistillKit",
32
- "πŸ”— MergeKit",
33
- "❄️ Spectrum"
34
- ],
35
- help="Select a library to explore!"
36
- )
37
-
38
- # Mermaid graph for DistillKit, MergeKit, and Spectrum workflows
39
- mermaid_code = """
40
- graph TD
41
- subgraph DistillKit
42
- A1[Load Teacher Model] --> B1[Load Student Model]
43
- B1 --> C1[Configure Distillation]
44
- C1 --> D1[Perform Distillation]
45
- D1 --> E1[Evaluate Model]
46
- end
47
- subgraph MergeKit
48
- A2[Select Models] --> B2[Choose Merge Method]
49
- B2 --> C2[Set Parameters]
50
- C2 --> D2[Merge Models]
51
- D2 --> E2[Save Merged Model]
52
- end
53
- subgraph Spectrum
54
- A3[Load Model] --> B3[Analyze Layers]
55
- B3 --> C3[Generate Config]
56
- C3 --> D3[Apply Freezing]
57
- D3 --> E3[Train/Evaluate Model]
58
- end
59
- """
60
-
61
- # Home Page
62
- if page == "🏠 Home":
63
- st.title("Hugging Face & Arcee Libraries Demo 🌟")
64
- st.markdown("""
65
- Welcome to an interactive demo of powerful libraries for text, image, and model processing!
66
- - **πŸ“ Text**: Analyze or generate text with Transformers.
67
- - **πŸ–ΌοΈ Images**: Create visuals with Diffusers.
68
- - **πŸ”— Models**: Distill, merge, and optimize with Arcee's DistillKit, MergeKit, and Spectrum.
69
- Navigate via the sidebar to explore each library!
70
- """)
71
-
72
- # Workflow Page with Mermaid Graph
73
- elif page == "πŸ”„ Workflow":
74
- st.header("πŸ”„ Workflows: DistillKit, MergeKit, Spectrum")
75
- st.markdown("See how inputs flow to outputs in Arcee’s libraries with this Mermaid graph:")
76
- components.html(f"""
77
- <div id="mermaid"></div>
78
- <script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
79
- <script>
80
- mermaid.initialize({{ startOnLoad: true }});
81
- const mermaidCode = `{mermaid_code}`;
82
- mermaid.render('graph', mermaidCode, (svgCode) => {{
83
- document.getElementById('mermaid').innerHTML = svgCode;
84
- }});
85
- </script>
86
- """, height=600)
87
 
88
  # Transformers Section
89
- elif page == "πŸ“ Transformers":
90
- st.header("πŸ“ Transformers")
91
- st.markdown("Process text with pre-trained models.")
92
- task = st.selectbox("Task", ["Sentiment Analysis", "Text Generation"])
93
- text = st.text_area("Input Text", "")
94
- if st.button("Run") and text:
95
- with st.spinner("Processing..."):
96
- if task == "Sentiment Analysis":
97
- result = pipeline("sentiment-analysis")(text)
98
- st.write(f"Result: {result[0]['label']} (Score: {result[0]['score']:.2f})")
99
- else:
100
- result = pipeline("text-generation")(text, max_length=50)[0]['generated_text']
101
- st.write(f"Generated: {result}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  # Diffusers Section
104
- elif page == "πŸ–ΌοΈ Diffusers":
105
- st.header("πŸ–ΌοΈ Diffusers")
106
- st.markdown("Generate images from text.")
107
- prompt = st.text_input("Prompt", "A futuristic city")
108
- if st.button("Generate"):
109
- with st.spinner("Generating..."):
110
- pipe = load_diffuser_model()
 
 
 
 
 
111
  image = pipe(prompt).images[0]
112
- st.image(image, caption=prompt)
 
 
 
 
113
 
114
  # Datasets Section
115
- elif page == "πŸ“Š Datasets":
116
- st.header("πŸ“Š Datasets")
117
- st.markdown("Load and explore datasets.")
118
- dataset = st.selectbox("Dataset", ["imdb", "squad"])
119
- if st.button("Load"):
120
- data = load_dataset(dataset, split="train[:5]")
121
- st.write(data)
122
-
123
- # PEFT Section
124
- elif page == "βš™οΈ PEFT":
125
- st.header("βš™οΈ PEFT")
126
- st.markdown("Parameter-efficient fine-tuning.")
127
- text = st.text_area("Text", "")
128
- if st.button("Classify") and text:
129
- st.write("Simulated PEFT classification: Positive")
130
-
131
- # Accelerate Section
132
- elif page == "πŸš€ Accelerate":
133
- st.header("πŸš€ Accelerate")
134
- st.markdown("Optimize across devices.")
135
- text = st.text_area("Text", "")
136
- if st.button("Analyze") and text:
137
- accelerator = Accelerator()
138
- result = pipeline("sentiment-analysis")(text)
139
- st.write(f"Result: {result[0]['label']} (Score: {result[0]['score']:.2f})")
140
-
141
- # Optimum Section
142
- elif page == "⚑ Optimum":
143
- st.header("⚑ Optimum")
144
- st.markdown("Hardware-accelerated inference.")
145
- text = st.text_area("Text", "")
146
- if st.button("Classify") and text:
147
- st.write("Simulated Optimum result: Positive")
148
-
149
- # DistillKit Section
150
- elif page == "πŸ“š DistillKit":
151
- st.header("πŸ“š DistillKit: Model Distillation")
152
- st.markdown("Distill large models into smaller, efficient ones. Here are the top 5 functions:")
153
-
154
- # 1. Load teacher model
155
- teacher = st.selectbox("Teacher Model", ["arcee-ai/Arcee-Spark", "bert-base-uncased"])
156
- st.write(f"1. Loaded teacher: {teacher}")
157
-
158
- # 2. Load student model
159
- student = st.selectbox("Student Model", ["Qwen/Qwen2-1.5B", "distilbert-base-uncased"])
160
- st.write(f"2. Loaded student: {student}")
161
-
162
- # 3. Configure distillation
163
- temp = st.slider("Temperature", 1.0, 5.0, 2.0)
164
- alpha = st.slider("Alpha", 0.0, 1.0, 0.5)
165
- st.write(f"3. Config: Temp={temp}, Alpha={alpha}")
166
-
167
- # 4. Perform distillation (simulated)
168
- if st.button("Distill"):
169
- with st.spinner("Distilling..."):
170
- time.sleep(2)
171
- st.success("4. Distillation complete!")
172
-
173
- # 5. Evaluate distilled model
174
- st.write("5. Evaluating...")
175
- metrics = {"accuracy": 0.85, "loss": 0.12}
176
- st.write(f"Metrics: {metrics}")
177
-
178
- st.markdown("""
179
- **How It Works:**
180
- DistillKit compresses a teacher model into a student model using distillation techniques.
181
- ```python
182
- config = {"teacher": "arcee-ai/Arcee-Spark", "student": "Qwen/Qwen2-1.5B", "temp": 2.0, "alpha": 0.5}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from transformers import pipeline, AutoModel, AutoTokenizer
3
+ from diffusers import StableDiffusionPipeline, DiffusionPipeline, DDIMScheduler
4
+ from datasets import load_dataset, Dataset
5
+ from peft import PeftModel, PeftConfig, get_peft_model
 
6
  from accelerate import Accelerator
7
  from optimum.onnxruntime import ORTModelForSequenceClassification
8
  import torch
9
+ from PIL import Image
10
+ import pandas as pd
11
 
12
+ # Cache resource-intensive models for performance
13
  @st.cache_resource
14
+ def load_diffuser():
15
  return StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
16
 
17
  # Sidebar navigation
18
+ st.sidebar.title("πŸ€— Hugging Face Libraries Demo")
19
+ library = st.sidebar.selectbox("Choose a library", ["Transformers", "Diffusers", "Datasets", "PEFT", "Accelerate", "Optimum"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # Transformers Section
22
+ if library == "Transformers":
23
+ st.header("πŸ“ Transformers: Text Processing Powerhouse")
24
+ st.write("Analyze or transform text with pre-trained NLP models. Try it out below! πŸ‘‡")
25
+
26
+ # pipeline: Easy inference for NLP tasks
27
+ # 🎭 - Sentiment analysis with pipeline. Ex: 'I love this!' β†’ Positive | Emotions in a snap! 😊
28
+ # ✍️ - Text generation with pipeline. Ex: 'Once upon a time' β†’ Story | Spin tales fast! πŸ“–
29
+ # ❓ - Question answering with pipeline. Ex: 'Who won?' β†’ Answer | Quiz master ready! 🧠
30
+ task = st.selectbox("Pick a Task", ["Sentiment Analysis 😊😒", "Text Generation ✍️", "Fill Mask πŸ€–"])
31
+ text_input = st.text_area("Enter text here ✍️", "", placeholder="Type your text...")
32
+
33
+ if st.button("Process Text πŸš€", disabled=not text_input):
34
+ with st.spinner("Processing... ⏳"):
35
+ if "Sentiment" in task:
36
+ classifier = pipeline("sentiment-analysis")
37
+ result = classifier(text_input)
38
+ st.success(f"**Sentiment Result:** {result[0]['label']} (Confidence: {result[0]['score']:.2f}) βœ…")
39
+ st.write("🎭 - Sentiment analysis with pipeline. Ex: 'I love this!' β†’ Positive | Emotions in a snap! 😊")
40
+ elif "Text Generation" in task:
41
+ generator = pipeline("text-generation")
42
+ result = generator(text_input, max_length=50)[0]['generated_text']
43
+ st.success(f"**Generated Text:** {result} βœ…")
44
+ st.write("✍️ - Text generation with pipeline. Ex: 'Once upon a time' β†’ Story | Spin tales fast! πŸ“–")
45
+ elif "Fill Mask" in task:
46
+ fill_mask = pipeline("fill-mask")
47
+ results = fill_mask(text_input.replace("[MASK]", fill_mask.tokenizer.mask_token))
48
+ for res in results[:3]:
49
+ st.write(f"Prediction: {res['sequence']} (Score: {res['score']:.2f})")
50
+ # AutoModel & AutoTokenizer: Load models and tokenizers
51
+ # πŸ€– - Mask filling with AutoModel. Ex: 'The [MASK] is blue' β†’ sky | Guess master! ✨
52
+ # πŸ“ - Tokenizing with AutoTokenizer. Ex: 'Hello world' β†’ tokens | Words to numbers! πŸ”’
53
+ # βš™οΈ - Custom inference with AutoModel. Ex: 'BERT for sentiment' β†’ logits | Model magic! πŸͺ„
54
+ st.write("πŸ€– - Mask filling with pipeline. Ex: 'The [MASK] is blue' β†’ sky | Guess master! ✨")
55
 
56
  # Diffusers Section
57
+ elif library == "Diffusers":
58
+ st.header("πŸ–ΌοΈ Diffusers: Image Generation Magic")
59
+ st.write("Turn text into stunning images with diffusion models! Let's create something cool. 🎨")
60
+
61
+ # StableDiffusionPipeline: Text-to-image generation
62
+ # πŸ–ΌοΈ - Text-to-image with StableDiffusionPipeline. Ex: 'Cat in space' β†’ Image | Art from words! πŸš€
63
+ # 🎨 - Style transfer with StableDiffusionPipeline. Ex: 'Van Gogh cat' β†’ Image | Paint like pros! πŸ–ŒοΈ
64
+ # 🌟 - Creative prompts with StableDiffusionPipeline. Ex: 'Dream city' β†’ Image | Imagine it, see it! ✨
65
+ prompt = st.text_input("Enter a Creative Prompt 🎨", "", placeholder="e.g., 'A cat in space'")
66
+ if st.button("Generate Image 🌟", disabled=not prompt):
67
+ with st.spinner("Generating image... ⏳"):
68
+ pipe = load_diffuser()
69
  image = pipe(prompt).images[0]
70
+ st.image(image, caption="Generated Image", use_column_width=True)
71
+ st.write("πŸ–ΌοΈ - Text-to-image with StableDiffusionPipeline. Ex: 'Cat in space' β†’ Image | Art from words! πŸš€")
72
+ # DiffusionPipeline & DDIMScheduler: Advanced diffusion control
73
+ # 🧩 - Inpainting with DiffusionPipeline. Ex: 'Fix broken image' β†’ Restored | Puzzle solver! πŸ› οΈ
74
+ # πŸ”§ - Scheduling with DDIMScheduler. Ex: 'Fast diffusion' β†’ Image | Speedy art! ⚑
75
 
76
  # Datasets Section
77
+ elif library == "Datasets":
78
+ st.header("πŸ“Š Datasets: Ready-to-Use Data")
79
+ st.write("Explore datasets for training or analysis. Pick one and see a sample! πŸ“ˆ")
80
+
81
+ # load_dataset: Access public datasets
82
+ # πŸ“š - Load reviews with load_dataset. Ex: 'imdb' β†’ Reviews | Movie buffs rejoice! 🎬
83
+ # ❓ - Load QA with load_dataset. Ex: 'squad' β†’ Q&A | Trivia time! 🧠
84
+ # πŸ—£οΈ - Load audio with load_dataset. Ex: 'common_voice' β†’ Audio | Hear the data! πŸŽ™οΈ
85
+ dataset_name = st.selectbox("Choose a Dataset", ["imdb 🎬", "squad ❓"])
86
+ if st.button("Load Dataset πŸ“₯"):
87
+ with st.spinner("Fetching dataset... ⏳"):
88
+ dataset = load_dataset(dataset_name.split()[0], split="train[:5]")
89
+ st.success(f"**Dataset Loaded:** {dataset_name} (showing first 5 samples) βœ…")
90
+ st.write(dataset)
91
+ st.write("πŸ“š - Load reviews with load_dataset. Ex: 'imdb' β†’ Reviews | Movie buffs rejoice! 🎬")
92
+
93
+ # Dataset.from_pandas: Convert DataFrames to datasets
94
+ # πŸ“Š - CSV to dataset with Dataset.from_pandas. Ex: 'Tweets CSV' β†’ Dataset | Your data shines! 🐦
95
+ # πŸ“ˆ - Analyze data with Dataset.from_pandas. Ex: 'Sales data' β†’ Dataset | Numbers talk! πŸ’°
96
+ # πŸ”„ - Preprocess with Dataset.from_pandas. Ex: 'Raw text' β†’ Dataset | Clean it up! 🧹
97
+ st.subheader("Create Dataset from CSV")
98
+ uploaded_file = st.file_uploader("Upload a CSV file", type="csv")
99
+ if uploaded_file is not None:
100
+ df = pd.read_csv(uploaded_file)
101
+ dataset = Dataset.from_pandas(df)
102
+ st.write(dataset)
103
+ st.write("πŸ“Š - CSV to dataset with Dataset.from_pandas. Ex: 'Tweets CSV' β†’ Dataset | Your data shines! 🐦")
104
+
105
+ # PEFT Section (Simplified)
106
+ elif library == "PEFT":
107
+ st.header("βš™οΈ PEFT: Efficient Fine-Tuning")
108
+ st.write("Learn about parameter-efficient fine-tuning (simplified demo).")
109
+
110
+ # PeftModel: Load fine-tuned models
111
+ # βš™οΈ - Inference with PeftModel. Ex: 'Adapted BERT' β†’ Output | Slim yet mighty! πŸ’ͺ
112
+ # πŸ€– - Custom tasks with PeftModel. Ex: 'NER tuning' β†’ Tags | Precision tweak! 🎯
113
+ # πŸ“‰ - Low resource with PeftModel. Ex: 'Small GPU' β†’ Model | Efficiency wins! πŸ†
114
+ # PeftConfig: Configure PEFT settings
115
+ # πŸ› οΈ - LoRA setup with PeftConfig. Ex: 'Rank 8' β†’ Config | Tune light! 🌟
116
+ # πŸ”§ - Adapter config with PeftConfig. Ex: 'Task-specific' β†’ Config | Fit like a glove! 🧀
117
+ # ⚑ - Fast tuning with PeftConfig. Ex: 'Quick fine-tune' β†’ Config | Speedy prep! πŸƒ
118
+ # get_peft_model: Wrap models with PEFT
119
+ # πŸ”— - Enhance with get_peft_model. Ex: 'BERT + LoRA' β†’ Model | Power boost! πŸš€
120
+ # πŸ“ - Task adapt with get_peft_model. Ex: 'Sentiment' β†’ Model | Tailored fit! βœ‚οΈ
121
+ # 🌐 - Multi-task with get_peft_model. Ex: 'QA + NER' β†’ Model | Versatility rules! 🌍
122
+ text = st.text_area("Enter Text to Classify ✍️", "", placeholder="Type something...")
123
+ if st.button("Classify πŸš€", disabled=not text):
124
+ with st.spinner("Processing... ⏳"):
125
+ st.success("**Result:** Placeholder (PEFT reduces parameters efficiently!) βœ…")
126
+ st.write("βš™οΈ - Inference with PeftModel. Ex: 'Adapted BERT' β†’ Output | Slim yet mighty! πŸ’ͺ")
127
+
128
+ # Accelerate Section (Simplified)
129
+ elif library == "Accelerate":
130
+ st.header("πŸš€ Accelerate: Device Optimization")
131
+ st.write("Optimize inference across devices with Accelerate.")
132
+
133
+ # Accelerator: Manage device placement
134
+ # πŸš€ - GPU use with Accelerator. Ex: 'Auto GPU' β†’ Device | Speed unleashed! ⚑
135
+ # πŸ”— - Multi-device with Accelerator. Ex: '2 GPUs' β†’ Setup | Teamwork rocks! 🀝
136
+ # πŸ› οΈ - Easy setup with Accelerator. Ex: 'Model prep' β†’ Ready | Smooth start! 🌈
137
+ text = st.text_area("Enter Text for Sentiment Analysis ✍️", "", placeholder="Type something...")
138
+ if st.button("Analyze πŸš€", disabled=not text):
139
+ with st.spinner("Analyzing... ⏳"):
140
+ accelerator = Accelerator()
141
+ classifier = pipeline("sentiment-analysis")
142
+ result = classifier(text)
143
+ st.success(f"**Result:** {result[0]['label']} (Confidence: {result[0]['score']:.2f}) βœ…")
144
+ st.write("πŸš€ - GPU use with Accelerator. Ex: 'Auto GPU' β†’ Device | Speed unleashed! ⚑")
145
+
146
+ # Optimum Section (Simplified)
147
+ elif library == "Optimum":
148
+ st.header("⚑ Optimum: Hardware Acceleration")
149
+ st.write("Speed up inference with optimized models (e.g., ONNX).")
150
+
151
+ # ORTModelForSequenceClassification: ONNX-optimized classification
152
+ # ⚑ - Fast classify with ORTModelForSequenceClassification. Ex: 'Text' β†’ Label | Speed king! 🏎️
153
+ # πŸ–₯️ - CPU boost with ORTModelForSequenceClassification. Ex: 'Sentiment' β†’ Result | No GPU? No prob! πŸ’ͺ
154
+ # πŸ“Š - Efficient with ORTModelForSequenceClassification. Ex: 'Batch text' β†’ Labels | Quick wins! πŸ†
155
+ text = st.text_area("Enter Text to Classify ✍️", "", placeholder="Type something...")
156
+ if st.button("Classify πŸš€", disabled=not text):
157
+ with st.spinner("Processing... ⏳"):
158
+ st.success("**Result:** Placeholder (Optimum boosts speed!) βœ…")
159
+ st.write("⚑ - Fast classify with ORTModelForSequenceClassification. Ex: 'Text' β†’ Label | Speed king! 🏎️")