Sangwon78 commited on
Commit
98aef03
ยท
verified ยท
1 Parent(s): 33f8740

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -94
app.py CHANGED
@@ -1,119 +1,71 @@
1
  import os
 
2
  import gradio as gr
3
- import moviepy.editor as mp
4
- from PIL import Image
 
5
 
6
- def get_video_info(video_path):
7
  """
8
- ๋™์˜์ƒ ํŒŒ์ผ์˜ ์žฌ์ƒ์‹œ๊ฐ„๊ณผ FPS๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
 
9
  """
10
- clip = mp.VideoFileClip(video_path)
11
- duration = clip.duration # ์ดˆ ๋‹จ์œ„
12
- fps = clip.fps
13
- clip.close()
14
- return duration, fps
15
 
16
- def get_thumbnail(video_path, time_point):
17
  """
18
- ๋™์˜์ƒ์—์„œ ์ง€์ •ํ•œ ์‹œ๊ฐ„(time_point)์˜ ํ”„๋ ˆ์ž„์„ ์ถ”์ถœํ•˜์—ฌ PIL ์ด๋ฏธ์ง€๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
 
19
  """
20
- clip = mp.VideoFileClip(video_path)
21
- frame = clip.get_frame(time_point)
22
- clip.close()
23
- image = Image.fromarray(frame)
24
- return image
25
 
26
- def generate_gif(video_path, start_time, end_time, resolution_scale, frame_rate, speed, loop_count):
27
  """
28
- ๋™์˜์ƒ์˜ ์ง€์ • ๊ตฌ๊ฐ„(start_time ~ end_time)์„ ์„ ํƒํ•œ ์˜ต์…˜์— ๋”ฐ๋ผ GIF๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
29
- - resolution_scale: ์›๋ณธ ๋Œ€๋น„ ๋ฐฐ์œจ (1.0์ด๋ฉด ์›๋ณธ)
30
- - frame_rate: 0์ด๋ฉด ์›๋ณธ FPS, ๋‹ค๋ฅธ ๊ฐ’์ด๋ฉด ํ•ด๋‹น FPS๋กœ ์„ค์ •
31
- - speed: ์˜์ƒ ์žฌ์ƒ ์†๋„ ๋ฐฐ์ˆ˜ (1.0์ด๋ฉด ์›๋ณธ)
32
- - loop_count: 0์ด๋ฉด ๋ฌดํ•œ๋ฐ˜๋ณต, ๊ทธ ์™ธ๋Š” ์ง€์ •๋œ ํšŸ์ˆ˜ ๋ฐ˜๋ณต
33
  """
34
- clip = mp.VideoFileClip(video_path)
35
- clip = clip.subclip(start_time, end_time)
36
-
37
- # ์†๋„ ์กฐ์ ˆ (๋ฐฐ์†)
38
- if speed != 1.0:
39
- clip = clip.fx(mp.vfx.speedx, speed)
40
-
41
- # ํ•ด์ƒ๋„ ์กฐ์ ˆ
42
- if resolution_scale != 1.0:
43
- clip = clip.resize(resolution_scale)
44
-
45
- # ํ”„๋ ˆ์ž„ ์†๋„ ์„ค์ • (0์ด๋ฉด ์›๋ณธ FPS ์‚ฌ์šฉ)
46
- target_fps = clip.fps if frame_rate == 0 else frame_rate
47
 
48
- # GIF๋ฅผ ์ €์žฅํ•  ์ž„์‹œ ํŒŒ์ผ๋ช…
49
- gif_path = "output.gif"
50
- clip.write_gif(gif_path, fps=target_fps, loop=int(loop_count))
51
- clip.close()
52
- return gif_path
53
-
54
- def update_video_info(video_path):
55
  """
56
- ๋™์˜์ƒ ์—…๋กœ๋“œ ์‹œ ๋™์˜์ƒ ์ •๋ณด๋ฅผ ์ถ”์ถœํ•˜๊ณ , ์‹œ์ž‘/์ข…๋ฃŒ ์‹œ๊ฐ„ ์ธ๋„ค์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
57
- ์ข…๋ฃŒ ์‹œ๊ฐ„์€ ๋™์˜์ƒ ๊ธธ์ด๋กœ ์ž๋™ ์—…๋ฐ์ดํŠธ๋ฉ๋‹ˆ๋‹ค.
58
  """
59
- duration, fps = get_video_info(video_path)
60
- thumb_start = get_thumbnail(video_path, 0)
61
- # ๋™์˜์ƒ์˜ ๋ ํ”„๋ ˆ์ž„์€ ์žฌ์ƒ์‹œ๊ฐ„์— ๊ฐ€๊น๊ฒŒ ์ถ”์ถœ
62
- thumb_end = get_thumbnail(video_path, max(0, duration - 0.1))
63
- info_text = f"์˜์ƒ ๊ธธ์ด: {duration:.2f}์ดˆ, FPS: {fps}"
64
- return info_text, thumb_start, thumb_end, duration
65
 
66
- with gr.Blocks() as demo:
67
- gr.Markdown("## ๋™์˜์ƒ์„ GIF๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ")
68
 
69
  with gr.Row():
70
- with gr.Column():
71
- video_input = gr.Video(label="๋™์˜์ƒ ์—…๋กœ๋“œ")
72
- video_info = gr.Textbox(label="๋™์˜์ƒ ์ •๋ณด", interactive=False)
73
- with gr.Column():
74
- video_output = gr.Video(label="์—…๋กœ๋“œ๋œ ์˜์ƒ")
75
 
 
 
 
76
  with gr.Row():
77
- start_time_input = gr.Number(label="์‹œ์ž‘ ์‹œ๊ฐ„ (์ดˆ)", value=0)
78
- end_time_input = gr.Number(label="์ข…๋ฃŒ ์‹œ๊ฐ„ (์ดˆ)", value=0) # ์—…๋กœ๋“œ ์‹œ ๋™์˜์ƒ ๊ธธ์ด๋กœ ์—…๋ฐ์ดํŠธ๋จ
79
 
80
- with gr.Row():
81
- start_thumbnail = gr.Image(label="์‹œ์ž‘ ์‹œ๊ฐ„ ์ธ๋„ค์ผ", type="pil")
82
- end_thumbnail = gr.Image(label="์ข…๋ฃŒ ์‹œ๊ฐ„ ์ธ๋„ค์ผ", type="pil")
83
 
84
  with gr.Row():
85
- resolution_slider = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, label="ํ•ด์ƒ๋„ ์กฐ์ ˆ (๋ฐฐ์œจ)", value=1.0)
86
- frame_rate_slider = gr.Slider(minimum=0, maximum=60, step=1, label="ํ”„๋ ˆ์ž„ ์†๋„ (FPS) [0์€ ์›๋ณธ]", value=0)
87
- speed_slider = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, label="์†๋„ ์กฐ์ ˆ (๋ฐฐ์ˆ˜)", value=1.0)
88
- loop_slider = gr.Slider(minimum=0, maximum=10, step=1, label="๋ฐ˜๋ณต ํšŸ์ˆ˜ (0์€ ๋ฌดํ•œ ๋ฐ˜๋ณต)", value=0)
89
 
90
- generate_button = gr.Button("GIF ์ƒ์„ฑ")
 
 
 
91
 
92
- gif_output = gr.Image(label="์™„์„ฑ๋œ GIF ๋ฏธ๋ฆฌ๋ณด๊ธฐ", type="filepath")
93
- gif_download = gr.File(label="์™„์„ฑ๋œ GIF ๋‹ค์šด๋กœ๋“œ")
94
-
95
- # ๋™์˜์ƒ ์—…๋กœ๋“œ ์‹œ ์ž๋™ ์—…๋ฐ์ดํŠธ: ๋™์˜์ƒ ์ •๋ณด, ์ธ๋„ค์ผ, ์ข…๋ฃŒ ์‹œ๊ฐ„ ๊ธฐ๋ณธ๊ฐ’(๋™์˜์ƒ ๊ธธ์ด)
96
- def on_video_upload(video_path):
97
- if video_path is None:
98
- return gr.update(value=""), None, None, 0
99
- info, thumb_start, thumb_end, duration = update_video_info(video_path)
100
- return info, thumb_start, thumb_end, duration
101
-
102
- video_input.upload(on_video_upload, inputs=video_input,
103
- outputs=[video_info, start_thumbnail, end_thumbnail, end_time_input])
104
-
105
- # GIF ์ƒ์„ฑ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ๋™์˜์ƒ์˜ ์„ ํƒ ๊ตฌ๊ฐ„ ๋ฐ ์˜ต์…˜์„ ์ ์šฉํ•˜์—ฌ GIF๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
106
- def process(video_path, start_time, end_time, resolution, frame_rate, speed, loop):
107
- if video_path is None:
108
- return None, None
109
- gif_path = generate_gif(video_path, start_time, end_time, resolution, frame_rate, speed, int(loop))
110
- return gif_path, gif_path
111
-
112
- generate_button.click(process,
113
- inputs=[video_input, start_time_input, end_time_input, resolution_slider, frame_rate_slider, speed_slider, loop_slider],
114
- outputs=[gif_output, gif_download])
115
-
116
- # ์—…๋กœ๋“œ๋œ ๋™์˜์ƒ์„ ์˜ค๋ฅธ์ชฝ ์ถœ๋ ฅ์ฐฝ์— ๋ฏธ๋Ÿฌ๋ง
117
- video_input.change(lambda v: v, video_input, video_output)
118
-
119
  demo.launch()
 
1
  import os
2
+ import pandas as pd
3
  import gradio as gr
4
+ # ๋งŒ์•ฝ OPENAI API๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๊ฒฝ์šฐ, ์•„๋ž˜์™€ ๊ฐ™์ด os.getenv๋กœ ํ† ํฐ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
5
+ # import openai
6
+ # openai.api_key = os.getenv("OPENAI_API_KEY")
7
 
8
+ def process_upload(file):
9
  """
10
+ ์—…๋กœ๋“œ๋œ ์—‘์…€ ํŒŒ์ผ์—์„œ ์ฒซ 6๊ฐœ ์—ด(A~F)๋งŒ ์ฝ์–ด ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
11
+ ๋™์‹œ์— ์›๋ณธ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒํƒœ(state)๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
12
  """
13
+ if file is None:
14
+ return None, None, None
15
+ df = pd.read_excel(file.name)
16
+ df = df.iloc[:, :6] # A~F ์—ด ์„ ํƒ
17
+ return df, df, df
18
 
19
+ def sort_by_date(df):
20
  """
21
+ ๋ฆฌ๋ทฐ๋‚ ์งœ ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
22
+ ์ถœ๋ ฅํ™”๋ฉด 1: ์ตœ์‹  ๋‚ ์งœ ์ˆœ, ์ถœ๋ ฅํ™”๋ฉด 2: ์˜ค๋ž˜๋œ ๋‚ ์งœ ์ˆœ.
23
  """
24
+ # ๋ฆฌ๋ทฐ๋‚ ์งœ ์ปฌ๋Ÿผ์„ datetime ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ (์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ NaT ์ฒ˜๋ฆฌ)
25
+ df['๋ฆฌ๋ทฐ๋‚ ์งœ'] = pd.to_datetime(df['๋ฆฌ๋ทฐ๋‚ ์งœ'], errors='coerce')
26
+ df1 = df.sort_values(by='๋ฆฌ๋ทฐ๋‚ ์งœ', ascending=False)
27
+ df2 = df.sort_values(by='๋ฆฌ๋ทฐ๋‚ ์งœ', ascending=True)
28
+ return df1, df2
29
 
30
+ def sort_by_score(df):
31
  """
32
+ ๋ฆฌ๋ทฐ์ ์ˆ˜ ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
33
+ ์ถœ๋ ฅํ™”๋ฉด 1: ๋†’์€ ์ ์ˆ˜ ์ˆœ, ์ถœ๋ ฅํ™”๋ฉด 2: ๋‚ฎ์€ ์ ์ˆ˜ ์ˆœ.
 
 
 
34
  """
35
+ df1 = df.sort_values(by='๋ฆฌ๋ทฐ์ ์ˆ˜', ascending=False)
36
+ df2 = df.sort_values(by='๋ฆฌ๋ทฐ์ ์ˆ˜', ascending=True)
37
+ return df1, df2
 
 
 
 
 
 
 
 
 
 
38
 
39
+ def reset_data(df):
 
 
 
 
 
 
40
  """
41
+ ์›๋ณธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋Œ€๋กœ ๋‘ ํ™”๋ฉด์— ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
 
42
  """
43
+ return df, df
 
 
 
 
 
44
 
45
+ with gr.Blocks(title="์—‘์…€๋ถ„์„๊ธฐ") as demo:
46
+ gr.Markdown("# ์—‘์…€๋ถ„์„๊ธฐ")
47
 
48
  with gr.Row():
49
+ file_input = gr.File(label="์—‘์…€ํŒŒ์ผ ์—…๋กœ๋“œ", file_types=['.xlsx', '.xls'])
 
 
 
 
50
 
51
+ # ์ƒํƒœ(state)๋กœ ์—…๋กœ๋“œํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
52
+ state = gr.State()
53
+
54
  with gr.Row():
55
+ output1 = gr.Dataframe(label="์ถœ๋ ฅํ™”๋ฉด 1")
56
+ output2 = gr.Dataframe(label="์ถœ๋ ฅํ™”๋ฉด 2")
57
 
58
+ # ์—‘์…€ ํŒŒ์ผ ์—…๋กœ๋“œ ์‹œ process_upload ํ•จ์ˆ˜ ์‹คํ–‰ ํ›„ ์›๋ณธ ๋ฐ์ดํ„ฐ์™€ ํ•จ๊ป˜ ์ƒํƒœ ์ €์žฅ
59
+ file_input.change(fn=process_upload, inputs=file_input, outputs=[output1, output2, state])
 
60
 
61
  with gr.Row():
62
+ btn_date = gr.Button("๋ฆฌ๋ทฐ๋‚ ์งœ")
63
+ btn_score = gr.Button("๋ฆฌ๋ทฐ์ ์ˆ˜")
64
+ btn_reset = gr.Button("์›๋ณธ๋ณด๊ธฐ")
 
65
 
66
+ # ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ํ•ด๋‹น ํ•จ์ˆ˜ ์‹คํ–‰ (์ €์žฅ๋œ state๋ฅผ ์‚ฌ์šฉ)
67
+ btn_date.click(fn=sort_by_date, inputs=state, outputs=[output1, output2])
68
+ btn_score.click(fn=sort_by_score, inputs=state, outputs=[output1, output2])
69
+ btn_reset.click(fn=reset_data, inputs=state, outputs=[output1, output2])
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  demo.launch()