File size: 2,554 Bytes
3d568b7
 
ae4be59
3d568b7
ae4be59
1c03ffb
ae4be59
3d568b7
 
 
 
 
 
 
 
26f3886
3d568b7
26f3886
 
 
 
3d568b7
26f3886
 
 
ae4be59
 
 
 
3d568b7
 
 
 
 
 
 
ae4be59
26f3886
 
 
 
 
 
 
 
 
1c03ffb
26f3886
 
 
 
 
 
 
3d568b7
26f3886
3d568b7
ae4be59
 
 
 
26f3886
ae4be59
 
 
 
 
3d568b7
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
import gradio as gr
import pandas as pd
import duckdb

PARQUET_FILES = {
    "Animagine XL 3.1": 'data.parquet',
}

preset_values = [
    "1girl, kamado nezuko, kimetsu no yaiba",
    "1girl, kanroji mitsuri, kimetsu no yaiba",
    "1girl, fern (sousou no frieren), sousou no frieren",
    "1girl, elaina (majo no tabitabi), majo no tabitabi"
]

def search_data(search_term, selected_file):
    try:
        con = duckdb.connect(database=':memory:', read_only=False)
        con.execute(f"CREATE TABLE my_table AS SELECT * FROM read_parquet('{PARQUET_FILES[selected_file]}')")
        if search_term.strip():
            results = con.execute(f"SELECT * FROM my_table WHERE lower(teks) LIKE '%{search_term.lower()}%'").fetchdf()
        else:
            search_terms_sql = " OR ".join([f"lower(teks) LIKE '%{term.lower()}%'" for term in preset_values])
            results = con.execute(f"SELECT * FROM my_table WHERE {search_terms_sql}").fetchdf()
        con.close()

        if len(results.columns) > 12:
            results = results.iloc[:, :12]
        return results
    except FileNotFoundError:
        return pd.DataFrame({'Error': ['Parquet file not found. Please check the file path.']})
    except Exception as e:
        return pd.DataFrame({'Error': [f'An error occurred: {e}']})

if __name__ == "__main__":
    with gr.Blocks() as app:
        gr.Markdown("##  Text Search for Animagine tag characters")
        with gr.Row(): # Menggunakan Row untuk layout horizontal
            with gr.Column(): # Kolom untuk input pencarian
                search_input = gr.Textbox(
                    label="Search for characters or series:",
                    placeholder="sousou no frieren",
                )
            with gr.Column(): # Kolom untuk dropdown
                file_dropdown = gr.Dropdown(
                    choices=list(PARQUET_FILES.keys()),
                    label="Select Character Tag",
                    value=list(PARQUET_FILES.keys())[0], # Default value
                )
        search_output = gr.Dataframe( # Output di luar row, di bawahnya
            label="Search Results",
            value=pd.DataFrame({'Characters tag': preset_values}),
            headers="auto",
        )

        inputs = [search_input, file_dropdown]
        search_input.change(
            fn=search_data,
            inputs=inputs,
            outputs=search_output,
        )
        file_dropdown.change(
            fn=search_data,
            inputs=inputs,
            outputs=search_output,
        )

    app.launch()