Skip to content
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

Giving examples for dataframe component as a pandas.DataFrame #2089

Closed
merveenoyan opened this issue Aug 26, 2022 · 9 comments · Fixed by #2128
Closed

Giving examples for dataframe component as a pandas.DataFrame #2089

merveenoyan opened this issue Aug 26, 2022 · 9 comments · Fixed by #2128
Assignees
Labels
enhancement New feature or request python Backend-related issue (Python)

Comments

@merveenoyan
Copy link
Contributor

merveenoyan commented Aug 26, 2022

I’m trying to give examples to dataframe component as list of lists like we do with other things, however I couldn’t succeed. when I look at the documentation, it doesn’t exist in the example as well. The example is not parsed correctly.

The list of lists is [[header1_row_1 header2_row1 ..] [header2_row1, header2_row2 ...]] where headers are features and rows are values. Each inner list is a sample (see example below)

(Pdb) examples
[['material_7', 'material_8', 5, 8, 154.02, 14, 6, 16.637, 20.719, 12.824, 16.067, 15.181, 18.546, 19.402, 643.086, 6, 19.532, 11.017, 15.639, 16.709, 10.057, 20.201, 11.106, 'C'], ['material_7', 'material_8', 5, 8, 108.73, 4, 7, 16.207, 20.058, 11.898, 13.871, 14.266, 15.734, 16.886, 642.533, 9, 18.128, 11.866, 17.891, 20.302, 'NaN', 18.148, 10.221, 'C'], ['material_7', 'material_6', 6, 9, 99.84, 6, 7, 17.17, 20.858, 10.968, 16.448, 15.6, 14.637, 13.86, 673.545, 6, 'NaN', 10.064, 16.287, 17.445, 12.117, 20.659, 11.999, 'E']]

It also gets each input and divides the string and treats it weirdly (see screenshot below) (it says m a t .. for first example as you can see, where it's material_7)

Ekran Resmi 2022-08-26 14 08 38

Also on Spaces, I get "list index out of range" for the above reason.
Can you tell me what am I doing wrong?

@freddyaboulton
Copy link
Collaborator

Hi @merveenoyan !

I think the follwoing should work

import gradio as gr
import pandas as pd


examples = [[[[7, 8, 9], [10, 11, 12]]],
            [[[5, 6, 7], [8, 9, 10]]]]

def get_max(df: pd.DataFrame) -> float:
    return df.max().max()


gr.Interface(get_max, gr.DataFrame(type="pandas", headers=["a", "b", "c"]), gr.Number(),
             examples=examples).launch()

but I agree we should make it possible to pass a dataframe directly and fix the examples are displayed in the UI.

@merveenoyan
Copy link
Contributor Author

So it's a list of lists of lists where each inner list is feature or sample? Can you specify? @freddyaboulton

@abidlabs
Copy link
Member

Hi @merveenoyan, in the latest released version of gradio==3.1.7, you provide examples as a list of list of 2D lists. As you said, each innermost 2D list is the sample to populate the dataframe. So providing multiple examples would look like this:

import gradio as gr
import pandas as pd
import numpy as np

examples = [[np.random.normal(size=(2,3)).tolist()], [np.random.normal(size=(2,3)).tolist()]]

def get_max(df: pd.DataFrame) -> float:
    return df.max().max()


gr.Interface(get_max, gr.DataFrame(type="pandas", headers=["a", "b", "c"]), gr.Number(),
             examples=examples).launch()

@abidlabs
Copy link
Member

abidlabs commented Aug 26, 2022

If you build from source from main or wait until the next release, you'll also have the ability to pass in example CSV files like this:

import gradio as gr
import pandas as pd
import numpy as np

examples = [["test.csv"]]

def get_max(df: pd.DataFrame) -> float:
    return df.max().max()


gr.Interface(get_max, gr.DataFrame(type="pandas", headers=["a", "b", "c"]), gr.Number(),
             examples=examples).launch()

but the preview looks weird. I'll create an issue around that

Does that cover what you are looking to do?

@freddyaboulton
Copy link
Collaborator

freddyaboulton commented Aug 26, 2022

@merveenoyan Yea in general, the data for the examples parameter is a list of lists, where each element of the inner list is the data for that particular component.

In the case of a DataFrame, the “data for that particular component”, is a list of lists where each inner list is a row of the dataframe.

In the future, I think we can make passing a pandas dataframe work so it’s easier to specify:

examples = [[pd.DataFrame(…)], [pd.DataFrame(…)]]

Edit: whoops @abidlabs commented while I was typing this out 😂

@merveenoyan
Copy link
Contributor Author

Thanks a lot, I will try these!

@abidlabs
Copy link
Member

@freddyaboulton in theory, the Dataframe component already supports examples = [[pd.DataFrame(…)], [pd.DataFrame(…)]] but the problem is that a pandas dataframe cannot be json serialized so there's an error that gets thrown when trying to generate the config file for the frontend. It would be cool if we can solve this serialization issue because I agree that providing pandas dataframes is more intuitive than a series of nested lists

@merveenoyan mind if I reopen this issue to track this feature?

@abidlabs abidlabs reopened this Aug 26, 2022
@abidlabs abidlabs changed the title Giving examples for dataframe component Giving examples for dataframe component as a pandas.DataFrame Aug 26, 2022
@abidlabs abidlabs added enhancement New feature or request python Backend-related issue (Python) and removed question labels Aug 26, 2022
@merveenoyan
Copy link
Contributor Author

@abidlabs I remember seeing that as well that I had to give as a list of lists, it makes more sense to give as a pandas dataframe (it's more native when you think about it).

@merveenoyan
Copy link
Contributor Author

Hey hey, it works fine! (on my local at least 😅) thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request python Backend-related issue (Python)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants