- 
                Notifications
    You must be signed in to change notification settings 
- Fork 749
Adding snippet for embedding visualisation #5802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            koaning
  wants to merge
  7
  commits into
  main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
bulk-example
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from 4 commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      b57dae5
              
                added a bulk example
              
              
                koaning 9c0f8f6
              
                Update altair-15.py
              
              
                koaning 685b569
              
                rename
              
              
                koaning 8a98b69
              
                Merge branch 'bulk-example' of github.com:marimo-team/marimo into bul…
              
              
                koaning c2cabfb
              
                Apply suggestions from code review
              
              
                koaning 0b48154
              
                Merge branch 'main' of github.com:marimo-team/marimo into bulk-example
              
              
                koaning 0f6bded
              
                Merge branch 'bulk-example' of github.com:marimo-team/marimo into bul…
              
              
                koaning File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # /// script | ||
| # requires-python = ">=3.12" | ||
| # dependencies = [ | ||
| # "altair==5.5.0", | ||
| # "marimo", | ||
| # "model2vec==0.6.0", | ||
| # "polars==1.31.0", | ||
| # "scikit-learn==1.7.1", | ||
| # "umap-learn==0.5.9.post2", | ||
| # ] | ||
| # /// | ||
|  | ||
| import marimo | ||
|  | ||
| __generated_with = "0.14.13" | ||
| app = marimo.App(width="columns") | ||
|  | ||
|  | ||
| @app.cell | ||
| def _(mo): | ||
| mo.md( | ||
| r""" | ||
| # Visualization: Embedding Summary and Bulk Selection | ||
|  | ||
| Create interactive dashboards using `mo.altair_chart` and `UMAP`. | ||
| This technique is generally useful for any kind of embedding, but we're demonstrating it with text embeddings below. | ||
| """ | ||
| ) | ||
| return | ||
|  | ||
|  | ||
|  | ||
| @app.cell | ||
| def _(): | ||
| import marimo as mo | ||
| import polars as pl | ||
| import altair as alt | ||
| from model2vec import StaticModel | ||
| from umap import UMAP | ||
| return StaticModel, UMAP, alt, mo, pl | ||
|  | ||
|  | ||
| @app.cell | ||
| def _(StaticModel, UMAP, pl): | ||
| DATASET = "https://calmcode.io/static/data/clinc.csv" | ||
| TEXT_COL = "text" | ||
|  | ||
| df = pl.read_csv(DATASET).sample(10_000) | ||
|  | ||
| # We're using Model2Vec because it so lightweight, sentence-transformers will also work! | ||
|         
                  koaning marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| tfm = StaticModel.from_pretrained("minishlab/potion-base-8M") | ||
| df = df.with_columns(emb=tfm.encode(df[TEXT_COL].to_list())) | ||
|  | ||
| # UMAP turns the high-dimensional embeddings into 2D points which are easier to visualize. | ||
| x_pca = UMAP(n_components=2).fit_transform(df["emb"].to_numpy()) | ||
| df = df.with_columns(x=x_pca[:, 0], y=x_pca[:, 1]).select(TEXT_COL, "x", "y") | ||
| return TEXT_COL, df | ||
|  | ||
|  | ||
| @app.cell | ||
| def _(alt, df, mo): | ||
| chart = mo.ui.altair_chart(alt.Chart(df).mark_point().encode(x="x", y="y").properties(width=500)) | ||
| return (chart,) | ||
|  | ||
|  | ||
| @app.cell | ||
| def _(TEXT_COL, chart, mo): | ||
| mo.hstack([chart, chart.value.select(TEXT_COL)]) | ||
| return | ||
|  | ||
|  | ||
| if __name__ == "__main__": | ||
| app.run() | ||
|  | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.