Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
height = gr.Slider(300, 2000, value=800, step=50, label="Height")
|
| 3 |
+
with gr.Row():
|
| 4 |
+
bg = gr.Textbox(value=DEFAULT_BG, label="Background color (e.g., white or #111827)")
|
| 5 |
+
seed = gr.Number(value=42, precision=0, label="Random seed")
|
| 6 |
+
stopwords = gr.Textbox(value="", label="Stopwords to exclude (comma-separated)")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
with gr.Row():
|
| 10 |
+
btn_load = gr.Button("Load / Preview CSV", variant="secondary")
|
| 11 |
+
btn_generate = gr.Button("Generate Word Cloud", variant="primary")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
preview = gr.Dataframe(label="CSV preview", interactive=False, wrap=True, max_rows=10)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
with gr.Row():
|
| 18 |
+
img = gr.Image(type="filepath", label="Word Cloud", show_download_button=True)
|
| 19 |
+
download = gr.File(label="Download image")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
top_table = gr.Dataframe(label="Top skills (weighted)", interactive=False)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# --- events ---
|
| 26 |
+
def on_load(file):
|
| 27 |
+
df = _load_csv(file)
|
| 28 |
+
s_col, c_col = _guess_columns(df)
|
| 29 |
+
df_state_value = df
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# choices
|
| 33 |
+
choices = list(df.columns)
|
| 34 |
+
skill_update = gr.update(choices=choices, value=s_col)
|
| 35 |
+
count_choices = ["(none)"] + choices
|
| 36 |
+
count_value = c_col if c_col else "(none)"
|
| 37 |
+
count_update = gr.update(choices=count_choices, value=count_value)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
return df_state_value, skill_update, count_update, df.head(10)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
btn_load.click(on_load, inputs=[csv_file], outputs=[df_state, skill_col, count_col, preview])
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def on_generate(df, file, s_col, c_col, max_w, w, h, bg_color, font, stop, seed_val):
|
| 47 |
+
# df from State may be None if user didn't click Load; try to load now
|
| 48 |
+
if df is None:
|
| 49 |
+
df = _load_csv(file)
|
| 50 |
+
# still need to guess columns
|
| 51 |
+
s_guess, c_guess = _guess_columns(df)
|
| 52 |
+
s_col = s_col or s_guess
|
| 53 |
+
if (not c_col) or c_col == "(none)":
|
| 54 |
+
c_col = c_guess
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
if c_col == "(none)":
|
| 58 |
+
c_col = None
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
freq_df = build_frequencies(df, s_col, c_col, stop)
|
| 62 |
+
out_path = generate_wordcloud_image(freq_df, font, int(w), int(h), int(max_w), bg_color, int(seed_val))
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# Show top 100 rows for quick inspection
|
| 66 |
+
top_show = freq_df.head(100)
|
| 67 |
+
return out_path, out_path, top_show
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
btn_generate.click(
|
| 71 |
+
on_generate,
|
| 72 |
+
inputs=[df_state, csv_file, skill_col, count_col, max_words, width, height, bg, font_file, stopwords, seed],
|
| 73 |
+
outputs=[img, download, top_table],
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
demo.launch()
|