NIML commited on
Commit
66e4c56
·
verified ·
1 Parent(s): 1945546
Files changed (1) hide show
  1. app.py +46 -20
app.py CHANGED
@@ -1,33 +1,59 @@
1
  import gradio as gr
2
  from palette_generator import generate_palette
3
- import plotly.express as px
 
4
 
5
- def generate_and_plot(n, temp):
6
- # Генерируем палитру
7
- palette, lightness, chroma, hues, description, hex_codes = generate_palette(n, temp)
8
 
9
- # Создаём график
10
- fig = px.scatter(x=hues, y=lightness, color=hex_codes, size=[10]*len(hues),
11
- labels={"x": "Hue", "y": "Lightness"}, title="Color Palette")
12
-
13
- return fig, description, hex_codes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- # Интерфейс Gradio
16
  with gr.Blocks() as demo:
 
 
17
  with gr.Row():
18
- n_input = gr.Slider(1, 9, value=5, step=1, label="Количество цветов")
19
- temp_input = gr.Dropdown(choices=["Все", "Тёплая", "Холодная"], value="Все", label="Температура")
20
- generate_btn = gr.Button("Сгенерировать")
21
  with gr.Row():
22
- plot_output = gr.Plot(label="График палитры")
23
- with gr.Column():
24
- description_output = gr.Textbox(label="Описание палитры")
25
- hex_output = gr.Textbox(label="HEX-коды")
 
 
 
 
 
 
 
 
 
26
 
27
  generate_btn.click(
28
- fn=generate_and_plot,
29
- inputs=[n_input, temp_input],
30
- outputs=[plot_output, description_output, hex_output]
31
  )
32
 
33
  demo.launch()
 
1
  import gradio as gr
2
  from palette_generator import generate_palette
3
+ import matplotlib.pyplot as plt
4
+ import numpy as np
5
 
6
+ def plot_palette(palette, lightness, chroma, hues):
7
+ if palette is None:
8
+ return "Не удалось сгенерировать палитру. Попробуйте снова.", None, None
9
 
10
+ fig, ax = plt.subplots(figsize=(10, 2))
11
+ for i, color in enumerate(palette):
12
+ ax.add_patch(plt.Rectangle((i, 0), 1, 1, color=color))
13
+ ax.text(i + 0.5, -0.2, f"L={lightness[i]:.2f}\nC={chroma[i]:.2f}\nH={hues[i]:.1f}",
14
+ ha='center', va='top')
15
+ ax.set_xlim(0, len(palette))
16
+ ax.set_ylim(0, 1)
17
+ ax.axis('off')
18
+ return fig, None, None
19
+
20
+ def generate_and_plot(n_colors, temp_category):
21
+ try:
22
+ palette, lightness, chroma, hues, description, hex_codes = generate_palette(
23
+ n_colors, temp_category
24
+ )
25
+ # Формируем текст для HEX-кодов
26
+ hex_text = "\n".join(f"Цвет {i+1}: {hex_code}" for i, hex_code in enumerate(hex_codes))
27
+ plot, _, _ = plot_palette(palette, lightness, chroma, hues)
28
+ return plot, description, hex_text
29
+ except ValueError as e:
30
+ return None, f"Ошибка: {str(e)}", None
31
 
 
32
  with gr.Blocks() as demo:
33
+ gr.Markdown("# Генератор цветовых палитр")
34
+
35
  with gr.Row():
36
+ n_colors = gr.Slider(2, 9, value=5, step=1, label="Количество цветов") # Обновлено: max 9
37
+
 
38
  with gr.Row():
39
+ temp_category = gr.Dropdown(
40
+ choices=["Все", "Тёплая", "Холодная"],
41
+ label="Температура",
42
+ value="Все",
43
+ interactive=True
44
+ )
45
+
46
+ with gr.Column():
47
+ output_plot = gr.Plot(label="Палитра")
48
+ output_desc = gr.Textbox(label="Описание палитры", lines=5)
49
+ output_hex = gr.Textbox(label="HEX-коды", lines=5)
50
+
51
+ generate_btn = gr.Button("Сгенерировать")
52
 
53
  generate_btn.click(
54
+ generate_and_plot,
55
+ inputs=[n_colors, temp_category],
56
+ outputs=[output_plot, output_desc, output_hex]
57
  )
58
 
59
  demo.launch()