Sangwon78 commited on
Commit
44c03ad
Β·
verified Β·
1 Parent(s): 3797fa6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -71
app.py CHANGED
@@ -1,84 +1,138 @@
1
- # app.py
2
  import os
3
- import pandas as pd
4
  import gradio as gr
 
 
 
 
 
5
 
6
- # μ—‘μ…€ νŒŒμΌμ„ 읽어 4κ°€μ§€ μ •λ ¬ 결과와 ν•΄λ‹Ή CSV 파일 경둜λ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜
7
- def process_file(file):
8
- # μ—‘μ…€ 파일 읽기 (첫 6개 μ—΄λ§Œ 선택)
9
- df = pd.read_excel(file.name)
10
- df = df.iloc[:, :6] # A~F μ—΄λ§Œ μ‚¬μš©
 
 
 
 
 
 
 
 
11
 
12
- # λ¦¬λ·°λ‚ μ§œ κΈ°μ€€ μ •λ ¬ (λ‚ μ§œν˜•μœΌλ‘œ λ³€ν™˜, λ³€ν™˜ μ‹€νŒ¨μ‹œ NaT)
13
- try:
14
- df['λ¦¬λ·°λ‚ μ§œ'] = pd.to_datetime(df['λ¦¬λ·°λ‚ μ§œ'], errors='coerce')
15
- except Exception as e:
16
- print("λ¦¬λ·°λ‚ μ§œ λ³€ν™˜ μ—λŸ¬:", e)
17
- date_recent = df.sort_values(by='λ¦¬λ·°λ‚ μ§œ', ascending=False)
18
- date_old = df.sort_values(by='λ¦¬λ·°λ‚ μ§œ', ascending=True)
 
 
19
 
20
- # 리뷰점수 κΈ°μ€€ μ •λ ¬ (μˆ«μžν˜•μœΌλ‘œ λ³€ν™˜)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  try:
22
- df['리뷰점수'] = pd.to_numeric(df['리뷰점수'], errors='coerce')
23
  except Exception as e:
24
- print("리뷰점수 λ³€ν™˜ μ—λŸ¬:", e)
25
- score_high = df.sort_values(by='리뷰점수', ascending=False)
26
- score_low = df.sort_values(by='리뷰점수', ascending=True)
27
-
28
- # CSV 파일둜 μ €μž₯ (ν˜„μž¬ μž‘μ—… 디렉토리에 μ €μž₯됨)
29
- date_recent_file = "date_recent.csv"
30
- date_old_file = "date_old.csv"
31
- score_high_file = "score_high.csv"
32
- score_low_file = "score_low.csv"
33
- date_recent.to_csv(date_recent_file, index=False)
34
- date_old.to_csv(date_old_file, index=False)
35
- score_high.to_csv(score_high_file, index=False)
36
- score_low.to_csv(score_low_file, index=False)
37
-
38
- return date_recent, date_old, score_high, score_low, date_recent_file, date_old_file, score_high_file, score_low_file
39
-
40
- # λ¦¬λ·°λ‚ μ§œ λ²„νŠΌ 클릭 μ‹œ μ‹€ν–‰λ˜λŠ” ν•¨μˆ˜
41
- def on_date(file):
42
- if file is None:
43
- return [None] * 6
44
- date_recent, date_old, score_high, score_low, date_recent_file, date_old_file, score_high_file, score_low_file = process_file(file)
45
- # 첫번째 ν™”λ©΄: λ¦¬λ·°λ‚ μ§œ λ‚΄λ¦Όμ°¨μˆœ, λ‘λ²ˆμ§Έ ν™”λ©΄: λ¦¬λ·°λ‚ μ§œ μ˜€λ¦„μ°¨μˆœ
46
- return date_recent, date_old, date_recent_file, date_old_file, score_high_file, score_low_file
47
-
48
- # 리뷰점수 λ²„νŠΌ 클릭 μ‹œ μ‹€ν–‰λ˜λŠ” ν•¨μˆ˜
49
- def on_score(file):
50
- if file is None:
51
- return [None] * 6
52
- date_recent, date_old, score_high, score_low, date_recent_file, date_old_file, score_high_file, score_low_file = process_file(file)
53
- # 첫번째 ν™”λ©΄: 리뷰점수 λ‚΄λ¦Όμ°¨μˆœ, λ‘λ²ˆμ§Έ ν™”λ©΄: 리뷰점수 μ˜€λ¦„μ°¨μˆœ
54
- return score_high, score_low, date_recent_file, date_old_file, score_high_file, score_low_file
55
 
56
  # Gradio μΈν„°νŽ˜μ΄μŠ€ ꡬ성
57
- with gr.Blocks(title="엑셀뢄석기") as demo:
58
- # μ—‘μ…€ 파일 μ—…λ‘œλ“œ μ»΄ν¬λ„ŒνŠΈ (μ—‘μ…€ 파일 ν™•μž₯자 μ§€μ •)
59
- file_input = gr.File(label="μ—‘μ…€νŒŒμΌ μ—…λ‘œλ“œ", file_types=[".xlsx", ".xls"])
60
 
61
- # 두 개의 λ²„νŠΌ 생성
62
- with gr.Row():
63
- btn_date = gr.Button("λ¦¬λ·°λ‚ μ§œ")
64
- btn_score = gr.Button("리뷰점수")
 
65
 
66
- # 두 개의 λ°μ΄ν„°ν”„λ ˆμž„ 좜λ ₯ ν™”λ©΄ (A~F μ—΄λ§Œ ν‘œμ‹œ)
67
- with gr.Row():
68
- output1 = gr.Dataframe(label="좜λ ₯ ν™”λ©΄ 1 (A~F)")
69
- output2 = gr.Dataframe(label="좜λ ₯ ν™”λ©΄ 2 (A~F)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
- # λ‹€μš΄λ‘œλ“œ 링크 4개 (각 CSV 파일 λ‹€μš΄λ‘œλ“œ)
72
- with gr.Row():
73
- dl_date_recent = gr.File(label="λ¦¬λ·°λ‚ μ§œ λ‚΄λ¦Όμ°¨μˆœ λ‹€μš΄λ‘œλ“œ")
74
- dl_date_old = gr.File(label="λ¦¬λ·°λ‚ μ§œ μ˜€λ¦„μ°¨μˆœ λ‹€μš΄λ‘œλ“œ")
75
- with gr.Row():
76
- dl_score_high = gr.File(label="리뷰점수 λ‚΄λ¦Όμ°¨μˆœ λ‹€μš΄λ‘œλ“œ")
77
- dl_score_low = gr.File(label="리뷰점수 μ˜€λ¦„μ°¨μˆœ λ‹€μš΄λ‘œλ“œ")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
- # λ²„νŠΌ 클릭 μ‹œ 각각 on_date, on_score ν•¨μˆ˜ 호좜
80
- # 각 ν•¨μˆ˜λŠ” 6개의 결과값을 λ°˜ν™˜ν•¨: 두 개의 좜λ ₯ 화면와 4개의 λ‹€μš΄λ‘œλ“œ 파일 경둜
81
- btn_date.click(on_date, inputs=[file_input], outputs=[output1, output2, dl_date_recent, dl_date_old, dl_score_high, dl_score_low])
82
- btn_score.click(on_score, inputs=[file_input], outputs=[output1, output2, dl_date_recent, dl_date_old, dl_score_high, dl_score_low])
83
-
84
- demo.launch()
 
 
1
  import os
 
2
  import gradio as gr
3
+ from moviepy.editor import VideoFileClip, vfx
4
+ from PIL import Image
5
+ import tempfile
6
+ import imageio
7
+ import cv2
8
 
9
+ def get_video_info(video_path):
10
+ """μ—…λ‘œλ“œλœ μ˜μƒμ˜ 해상도, FPS, 총 ν”„λ ˆμž„ 수, μž¬μƒμ‹œκ°„ 정보λ₯Ό λ°˜ν™˜"""
11
+ cap = cv2.VideoCapture(video_path)
12
+ if not cap.isOpened():
13
+ return "μ˜μƒ 정보λ₯Ό κ°€μ Έμ˜¬ 수 μ—†μŠ΅λ‹ˆλ‹€."
14
+ width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
15
+ height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
16
+ fps = cap.get(cv2.CAP_PROP_FPS)
17
+ frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
18
+ duration = frame_count / fps if fps > 0 else 0
19
+ cap.release()
20
+ info = f"해상도: {int(width)}x{int(height)}\nFPS: {fps:.2f}\n총 ν”„λ ˆμž„ 수: {int(frame_count)}\nμž¬μƒμ‹œκ°„: {duration:.2f}초"
21
+ return info
22
 
23
+ def extract_thumbnail(video_path, time_sec):
24
+ """μ£Όμ–΄μ§„ μ‹œκ°„(초)μ—μ„œ ν”„λ ˆμž„μ„ μΆ”μΆœν•˜μ—¬ μ΄λ―Έμ§€λ‘œ λ°˜ν™˜"""
25
+ clip = VideoFileClip(video_path)
26
+ frame = clip.get_frame(time_sec)
27
+ image = Image.fromarray(frame)
28
+ clip.reader.close()
29
+ if clip.audio:
30
+ clip.audio.reader.close_proc()
31
+ return image
32
 
33
+ def generate_gif(video_file, start_time, end_time, resolution_scale, frame_rate, speed, repeat):
34
+ """μž…λ ₯된 μ˜μƒμ˜ μ§€μ • ꡬ간을 μž˜λΌλ‚΄κ³  μ˜΅μ…˜μ— 맞좰 GIF νŒŒμΌμ„ 생성"""
35
+ def parse_time(t):
36
+ parts = t.split(":")
37
+ if len(parts) == 3:
38
+ h, m, s = parts
39
+ return int(h)*3600 + int(m)*60 + float(s)
40
+ elif len(parts) == 2:
41
+ m, s = parts
42
+ return int(m)*60 + float(s)
43
+ else:
44
+ return float(t)
45
+ start_sec = parse_time(start_time)
46
+ end_sec = parse_time(end_time)
47
+
48
+ clip = VideoFileClip(video_file)
49
+ trimmed = clip.subclip(start_sec, end_sec)
50
+
51
+ if speed != 1:
52
+ trimmed = trimmed.fx(vfx.speedx, speed)
53
+
54
+ if resolution_scale != 1:
55
+ new_width = int(trimmed.w * resolution_scale)
56
+ new_height = int(trimmed.h * resolution_scale)
57
+ trimmed = trimmed.resize(newsize=(new_width, new_height))
58
+
59
+ trimmed = trimmed.set_fps(frame_rate)
60
+
61
+ temp_gif = tempfile.NamedTemporaryFile(delete=False, suffix=".gif")
62
  try:
63
+ trimmed.write_gif(temp_gif.name, loop=repeat)
64
  except Exception as e:
65
+ return None, f"GIF 생성 μ‹€νŒ¨: {e}"
66
+ clip.reader.close()
67
+ if clip.audio:
68
+ clip.audio.reader.close_proc()
69
+ return temp_gif.name, "GIF 생성 μ™„λ£Œ!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  # Gradio μΈν„°νŽ˜μ΄μŠ€ ꡬ성
72
+ with gr.Blocks(title="GIF생성기") as demo:
73
+ gr.Markdown("# GIF생성기")
 
74
 
75
+ with gr.Tab("μ˜μƒ μ—…λ‘œλ“œ"):
76
+ with gr.Row():
77
+ video_input = gr.Video(label="μ˜μƒ μ—…λ‘œλ“œ", source="upload", type="filepath")
78
+ video_info = gr.Textbox(label="μ˜μƒ 정보", interactive=False)
79
+ video_input.change(fn=lambda v: get_video_info(v) if v is not None else "", inputs=video_input, outputs=video_info)
80
 
81
+ with gr.Tab("μ‹œκ°„ 선택 및 썸넀일"):
82
+ gr.Markdown("### μ‹œμž‘/μ’…λ£Œ μ‹œκ°„ 선택 (μ‹œκ°„ ν˜•μ‹: HH:MM:SS)")
83
+ with gr.Row():
84
+ start_time_input = gr.Textbox(label="μ‹œμž‘ μ‹œκ°„", value="00:00:00")
85
+ end_time_input = gr.Textbox(label="μ’…λ£Œ μ‹œκ°„", value="00:00:05")
86
+ with gr.Row():
87
+ start_thumbnail = gr.Image(label="μ‹œμž‘ μ‹œκ°„ 썸넀일")
88
+ end_thumbnail = gr.Image(label="μ’…λ£Œ μ‹œκ°„ 썸넀일")
89
+ generate_thumb_btn = gr.Button("썸넀일 생성")
90
+
91
+ def generate_thumbnails(video_file, start_time, end_time):
92
+ if video_file is None:
93
+ return None, None
94
+ def parse_time(t):
95
+ parts = t.split(":")
96
+ if len(parts) == 3:
97
+ h, m, s = parts
98
+ return int(h)*3600 + int(m)*60 + float(s)
99
+ elif len(parts) == 2:
100
+ m, s = parts
101
+ return int(m)*60 + float(s)
102
+ else:
103
+ return float(t)
104
+ start_sec = parse_time(start_time)
105
+ end_sec = parse_time(end_time)
106
+ try:
107
+ start_img = extract_thumbnail(video_file, start_sec)
108
+ end_img = extract_thumbnail(video_file, end_sec)
109
+ except Exception as e:
110
+ return None, None
111
+ return start_img, end_img
112
+
113
+ generate_thumb_btn.click(generate_thumbnails, inputs=[video_input, start_time_input, end_time_input], outputs=[start_thumbnail, end_thumbnail])
114
 
115
+ with gr.Tab("GIF μ„€μ • 및 생성"):
116
+ gr.Markdown("### GIF μ„€μ •")
117
+ with gr.Row():
118
+ resolution_scale = gr.Slider(minimum=0.1, maximum=1.0, value=1.0, label="해상도 쑰절 (배율)")
119
+ frame_rate = gr.Slider(minimum=1, maximum=60, step=1, value=15, label="ν”„λ ˆμž„ 속도 (FPS)")
120
+ with gr.Row():
121
+ speed = gr.Slider(minimum=0.1, maximum=3.0, step=0.1, value=1.0, label="속도 쑰절")
122
+ repeat = gr.Slider(minimum=0, maximum=10, step=1, value=0, label="반볡 횟수 (0은 λ¬΄ν•œ 반볡)")
123
+ generate_gif_btn = gr.Button("GIF 생성")
124
+ gif_preview = gr.Image(label="μ™„μ„±λœ GIF 미리보기")
125
+ gif_download = gr.File(label="μ™„μ„±λœ GIF λ‹€μš΄λ‘œλ“œ")
126
+
127
+ def process_gif(video_file, start_time, end_time, resolution_scale, frame_rate, speed, repeat):
128
+ if video_file is None:
129
+ return None, None
130
+ gif_path, msg = generate_gif(video_file, start_time, end_time, resolution_scale, frame_rate, speed, repeat)
131
+ if gif_path is None:
132
+ return None, None
133
+ return gif_path, gif_path
134
+
135
+ generate_gif_btn.click(process_gif, inputs=[video_input, start_time_input, end_time_input, resolution_scale, frame_rate, speed, repeat], outputs=[gif_preview, gif_download])
136
 
137
+ if __name__ == "__main__":
138
+ demo.launch()