Sangwon78 commited on
Commit
e50f852
ยท
verified ยท
1 Parent(s): 030c90b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -153
app.py CHANGED
@@ -1,166 +1,80 @@
1
- import gradio as gr
2
- import logging
3
- import tempfile
4
  import os
5
- from moviepy.editor import VideoFileClip, vfx
6
- from PIL import Image
7
- import numpy as np
8
-
9
- # ๋กœ๊ทธ ์„ค์ •
10
- logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
11
-
12
- def video_upload_callback(video_path):
13
- """
14
- ๋™์˜์ƒ ์—…๋กœ๋“œ ์‹œ ์‹คํ–‰:
15
- - ์—…๋กœ๋“œ๋œ ๋™์˜์ƒ์„ ์žฌ์ƒ ๊ฐ€๋Šฅํ•˜๋„๋ก ์ถœ๋ ฅ
16
- - ๋™์˜์ƒ ์ •๋ณด๋ฅผ ์ถ”์ถœํ•˜์—ฌ ํ…์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ (์žฌ์ƒ ์‹œ๊ฐ„, ํ•ด์ƒ๋„, FPS)
17
- """
18
- if video_path is None:
19
- logging.error("๋™์˜์ƒ ํŒŒ์ผ์ด ์—…๋กœ๋“œ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
20
- return None, "๋™์˜์ƒ ํŒŒ์ผ์ด ์—†์Šต๋‹ˆ๋‹ค."
21
- try:
22
- logging.info("๋™์˜์ƒ ์—…๋กœ๋“œ ์‹œ์ž‘")
23
- clip = VideoFileClip(video_path)
24
- duration = clip.duration
25
- w, h = clip.size
26
- fps = clip.fps
27
- info_text = f"์˜์ƒ ์ •๋ณด: ์žฌ์ƒ ์‹œ๊ฐ„ = {duration:.2f}์ดˆ, ํ•ด์ƒ๋„ = {w}x{h}, FPS = {fps}"
28
- logging.info("๋™์˜์ƒ ์ •๋ณด ์ถ”์ถœ ์™„๋ฃŒ: " + info_text)
29
- clip.close()
30
- # ์Šฌ๋ผ์ด๋”์— ์ง์ ‘ ๊ฐ’์„ ์ „๋‹ฌํ•˜์ง€ ์•Š๊ณ , ์—ฌ๊ธฐ์„œ๋Š” 2๊ฐœ๋งŒ ๋ฐ˜ํ™˜
31
- return video_path, info_text
32
- except Exception as e:
33
- logging.error("๋™์˜์ƒ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: " + str(e))
34
- return None, "๋™์˜์ƒ ์ •๋ณด๋ฅผ ์ถ”์ถœํ•˜๋Š” ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค."
35
-
36
- def generate_gif_callback(video_path, start_time_str, end_time_str,
37
- resolution_percent, frame_rate, playback_speed, loop_count):
38
- if video_path is None:
39
- logging.error("๋™์˜์ƒ ํŒŒ์ผ์ด ์—†์Šต๋‹ˆ๋‹ค.")
40
- return None, None, None, None
41
-
42
- def parse_time(time_str):
43
- if ":" in time_str:
44
- parts = list(map(float, time_str.split(":")))
45
- if len(parts) == 3:
46
- return parts[0]*3600 + parts[1]*60 + parts[2]
47
- elif len(parts) == 2:
48
- return parts[0]*60 + parts[1]
49
- else:
50
- return parts[0]
51
- else:
52
- return float(time_str)
53
-
54
- try:
55
- start_time = parse_time(start_time_str)
56
- end_time = parse_time(end_time_str)
57
- logging.info(f"์ž…๋ ฅ๋œ ์‹œ์ž‘ ์‹œ๊ฐ„: {start_time}์ดˆ, ๋ ์‹œ๊ฐ„: {end_time}์ดˆ")
58
- except Exception as e:
59
- logging.error("์‹œ๊ฐ„ ํŒŒ์‹ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: " + str(e))
60
- return None, None, None, None
61
-
62
- try:
63
- clip = VideoFileClip(video_path)
64
- except Exception as e:
65
- logging.error("๋™์˜์ƒ ๋กœ๋“œ ์‹คํŒจ: " + str(e))
66
- return None, None, None, None
67
-
68
- if start_time < 0 or end_time > clip.duration or start_time >= end_time:
69
- logging.error("์ž˜๋ชป๋œ ์‹œ๊ฐ„ ์ž…๋ ฅ: ์‹œ์ž‘ ์‹œ๊ฐ„์ด๋‚˜ ๋ ์‹œ๊ฐ„์ด ์œ ํšจํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")
70
- clip.close()
71
- return None, None, None, None
72
 
73
- try:
74
- # ์‹œ์ž‘, ๋ ์‹œ๊ฐ„์˜ ์ธ๋„ค์ผ ์ถ”์ถœ
75
- frame_start = clip.get_frame(start_time)
76
- frame_end = clip.get_frame(end_time)
77
- thumbnail_start_img = Image.fromarray(frame_start)
78
- thumbnail_end_img = Image.fromarray(frame_end)
79
- logging.info("์ธ๋„ค์ผ ์ƒ์„ฑ ์™„๋ฃŒ")
80
- except Exception as e:
81
- logging.error("์ธ๋„ค์ผ ์ƒ์„ฑ ์˜ค๋ฅ˜: " + str(e))
82
- clip.close()
83
- return None, None, None, None
84
 
 
 
 
 
85
  try:
86
- # ์‹œ์ž‘~๋ ๊ตฌ๊ฐ„์˜ ์„œ๋ธŒํด๋ฆฝ ์ƒ์„ฑ
87
- subclip = clip.subclip(start_time, end_time)
88
- # ํ•ด์ƒ๋„ ์กฐ์ ˆ
89
- scale_factor = resolution_percent / 100.0
90
- if scale_factor != 1.0:
91
- subclip = subclip.resize(scale_factor)
92
- logging.info(f"ํ•ด์ƒ๋„ ์กฐ์ ˆ: {resolution_percent}% ์ ์šฉ")
93
- # ํ”„๋ ˆ์ž„ ์†๋„ ์กฐ์ ˆ
94
- if frame_rate != clip.fps:
95
- subclip = subclip.set_fps(frame_rate)
96
- logging.info(f"ํ”„๋ ˆ์ž„ ์†๋„ ์กฐ์ ˆ: {frame_rate} fps ์ ์šฉ")
97
- # ์˜์ƒ ์žฌ์ƒ ์†๋„ ์กฐ์ ˆ
98
- if playback_speed != 1.0:
99
- subclip = subclip.fx(vfx.speedx, playback_speed)
100
- logging.info(f"์˜์ƒ ์žฌ์ƒ ์†๋„ ์กฐ์ ˆ: {playback_speed}๋ฐฐ์† ์ ์šฉ")
101
  except Exception as e:
102
- logging.error("์„œ๋ธŒํด๋ฆฝ ์ƒ์„ฑ ๋ฐ ์†๋„/ํ•ด์ƒ๋„ ์กฐ์ ˆ ์ค‘ ์˜ค๋ฅ˜: " + str(e))
103
- clip.close()
104
- return thumbnail_start_img, thumbnail_end_img, None, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
  try:
107
- # GIF ์ƒ์„ฑ (์ž„์‹œ ํŒŒ์ผ์— ์ €์žฅ)
108
- with tempfile.NamedTemporaryFile(suffix=".gif", delete=False) as tmp:
109
- gif_path = tmp.name
110
- loop_value = loop_count # 0์ด๋ฉด ๋ฌดํ•œ๋ฐ˜๋ณต
111
- logging.info("GIF ์ƒ์„ฑ ์ค‘...")
112
- subclip.write_gif(gif_path, fps=frame_rate, loop=loop_value)
113
- logging.info("GIF ์ƒ์„ฑ ์™„๋ฃŒ")
 
 
 
 
114
  except Exception as e:
115
- logging.error("GIF ์ƒ์„ฑ ์˜ค๋ฅ˜: " + str(e))
116
- gif_path = None
117
-
118
- clip.close()
119
- subclip.close()
120
- return thumbnail_start_img, thumbnail_end_img, gif_path, gif_path
121
-
122
- with gr.Blocks() as demo:
123
- gr.Markdown("## ๋™์˜์ƒ ์—…๋กœ๋“œ ๋ฐ GIF ๋ณ€ํ™˜ ์ŠคํŽ˜์ด์Šค")
124
- with gr.Row():
125
- with gr.Column():
126
- video_input = gr.Video(label="๋™์˜์ƒ ์—…๋กœ๋“œ")
127
- video_output = gr.Video(label="์—…๋กœ๋“œ๋œ ๋™์˜์ƒ ์žฌ์ƒ")
128
- video_info_text = gr.Textbox(label="์˜์ƒ ์ •๋ณด", interactive=False)
129
- with gr.Column():
130
- # ๋ณ€๊ฒฝ ํฌ์ธํŠธ:
131
- # outputs์— 'gr.Slider()'๋ฅผ ์ง์ ‘ ๋„ฃ์ง€ ์•Š๊ณ , 2๊ฐœ๋งŒ ๋ฐ˜ํ™˜
132
- video_input.change(
133
- fn=video_upload_callback,
134
- inputs=video_input,
135
- outputs=[video_output, video_info_text]
136
- )
137
-
138
  with gr.Row():
139
- start_time = gr.Textbox(label="์‹œ์ž‘ ์‹œ๊ฐ„ (์ดˆ ๋˜๋Š” hh:mm:ss)", placeholder="์˜ˆ: 0 ๋˜๋Š” 00:00:00")
140
- end_time = gr.Textbox(label="๋ ์‹œ๊ฐ„ (์ดˆ ๋˜๋Š” hh:mm:ss)", placeholder="์˜ˆ: 10 ๋˜๋Š” 00:00:10")
141
-
 
 
142
  with gr.Row():
143
- thumbnail_start = gr.Image(label="์‹œ์ž‘ ๋ถ€๋ถ„ ์ธ๋„ค์ผ")
144
- thumbnail_end = gr.Image(label="๋ ๋ถ€๋ถ„ ์ธ๋„ค์ผ")
145
-
146
- with gr.Row():
147
- resolution_slider = gr.Slider(minimum=10, maximum=100, step=1, label="ํ•ด์ƒ๋„ ์กฐ์ ˆ (%)", value=100)
148
- frame_rate_slider = gr.Slider(minimum=1, maximum=60, step=1, label="ํ”„๋ ˆ์ž„ ์†๋„ (fps)", value=30)
149
- with gr.Row():
150
- playback_speed_slider = gr.Slider(minimum=0.1, maximum=3.0, step=0.1, label="์˜์ƒ ์žฌ์ƒ ์†๋„", value=1.0)
151
- gif_loop_slider = gr.Slider(minimum=0, maximum=10, step=1, label="GIF ๋ฐ˜๋ณต ํšŸ์ˆ˜ (0: ๋ฌดํ•œ ๋ฐ˜๋ณต)", value=0)
152
-
153
- gif_button = gr.Button("GIF ์ƒ์„ฑ")
154
- with gr.Row():
155
- gif_preview = gr.Image(label="GIF ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
156
- gif_download = gr.File(label="์™„์„ฑ๋œ GIF ๋‹ค์šด๋กœ๋“œ")
157
-
158
- gif_button.click(
159
- fn=generate_gif_callback,
160
- inputs=[video_input, start_time, end_time, resolution_slider, frame_rate_slider, playback_speed_slider, gif_loop_slider],
161
- outputs=[thumbnail_start, thumbnail_end, gif_preview, gif_download]
162
- )
163
 
164
  if __name__ == "__main__":
165
- logging.info("Gradio ์ŠคํŽ˜์ด์Šค ์‹œ์ž‘")
166
  demo.launch()
 
 
 
 
1
  import os
2
+ import gradio as gr
3
+ import openai
4
+ import requests
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ # OpenAI API ํ‚ค๋ฅผ ํ™˜๊ฒฝ๋ณ€์ˆ˜์—์„œ ๋ถˆ๋Ÿฌ์˜ด
7
+ openai.api_key = os.getenv("OPENAI_API_KEY")
 
 
 
 
 
 
 
 
 
8
 
9
+ def fetch_reference_text(url):
10
+ """URL์—์„œ ํ…์ŠคํŠธ ์ปจํ…์ธ ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ ๋นˆ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜."""
11
+ if not url:
12
+ return ""
13
  try:
14
+ response = requests.get(url, timeout=5)
15
+ response.raise_for_status()
16
+ return response.text
 
 
 
 
 
 
 
 
 
 
 
 
17
  except Exception as e:
18
+ print(f"URL({url})์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ์‹คํŒจ: {e}")
19
+ return ""
20
+
21
+ def generate_blog_post(tone, ref_url1, ref_url2, ref_url3):
22
+ # ๊ฐ URL์—์„œ ํ…์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ
23
+ ref_text1 = fetch_reference_text(ref_url1)
24
+ ref_text2 = fetch_reference_text(ref_url2)
25
+ ref_text3 = fetch_reference_text(ref_url3)
26
+
27
+ # ํ•˜๋‚˜์˜ ์ฐธ์กฐ๊ธ€ ํ…์ŠคํŠธ๋กœ ํ•ฉ์น˜๊ธฐ (๊ฐ๊ฐ์˜ ๋‚ด์šฉ์ด ์žˆ์œผ๋ฉด)
28
+ references = "\n\n".join([txt for txt in [ref_text1, ref_text2, ref_text3] if txt])
29
+
30
+ # ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€: ์ƒ์„ฑ๋  ๊ธ€์˜ ์กฐ๊ฑด์„ ์„ค๋ช…
31
+ system_message = (
32
+ "๋‹น์‹ ์€ ์ „๋ฌธ ๋ธ”๋กœ๊ทธ ๊ธ€ ์ž‘์„ฑ ๋„์šฐ๋ฏธ์ž…๋‹ˆ๋‹ค. "
33
+ "์‚ฌ์šฉ์ž๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ฐธ์กฐ๊ธ€์„ ๋ฐ”ํƒ•์œผ๋กœ 2,000์ž ์ •๋„์˜ ๋ธ”๋กœ๊ทธ ๊ธ€์„ ์ž‘์„ฑํ•˜์„ธ์š”. "
34
+ "๊ธ€์€ ์„œ๋ก , ๋ณธ๋ก , ๊ฒฐ๋ก ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. "
35
+ f"๋งํˆฌ๋Š” '{tone}' ์Šคํƒ€์ผ๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. "
36
+ "๊ฐ€๋Šฅํ•œ ํ•œ ์ฐธ์กฐ๊ธ€์˜ ๋‚ด์šฉ์„ ๋ฐ˜์˜ํ•˜์—ฌ ๊ธ€์˜ ์‹ ๋ขฐ์„ฑ์„ ๋†’์ด์„ธ์š”."
37
+ )
38
+
39
+ # ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€์— ์ฐธ์กฐ๊ธ€์„ ์ถ”๊ฐ€ (์ฐธ์กฐ๊ธ€์ด ์—†์œผ๋ฉด ์ƒ๋žต)
40
+ user_message = "์ฐธ์กฐ๊ธ€:\n" + (references if references else "์ฐธ์กฐ๊ธ€ ์—†์Œ") + "\n\n๋ธ”๋กœ๊ทธ ๊ธ€์„ ์ƒ์„ฑํ•ด ์ฃผ์„ธ์š”."
41
 
42
  try:
43
+ # OpenAI ChatCompletion API ํ˜ธ์ถœ (gpt-3.5-turbo ๋ชจ๋ธ ์‚ฌ์šฉ)
44
+ response = openai.ChatCompletion.create(
45
+ model="gpt-3.5-turbo",
46
+ messages=[
47
+ {"role": "system", "content": system_message},
48
+ {"role": "user", "content": user_message}
49
+ ],
50
+ temperature=0.7,
51
+ max_tokens=1500 # ํ† ํฐ ์ˆ˜ ์กฐ์ ˆ (2000์ž ๋ถ„๋Ÿ‰์— ๋งž์ถ”์–ด ์กฐ์ ˆ ๊ฐ€๋Šฅ)
52
+ )
53
+ blog_post = response.choices[0].message.content.strip()
54
  except Exception as e:
55
+ blog_post = f"๋ธ”๋กœ๊ทธ ๊ธ€ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {e}"
56
+
57
+ return blog_post
58
+
59
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ
60
+ with gr.Blocks(title="๋ธ”๋กœ๊ทธ ๊ธ€ ์ƒ์„ฑ๊ธฐ") as demo:
61
+ gr.Markdown("## ๋ธ”๋กœ๊ทธ ๊ธ€ ์ƒ์„ฑ๊ธฐ\n์ฐธ์กฐ๊ธ€ URL๊ณผ ์›ํ•˜๋Š” ๋งํˆฌ๋ฅผ ์„ ํƒํ•˜์—ฌ ๋ธ”๋กœ๊ทธ ๊ธ€์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.")
62
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  with gr.Row():
64
+ tone = gr.Dropdown(
65
+ choices=["์นœ๊ทผํ•œ๋งํˆฌ", "์ผ๋ฐ˜์ ์ธ๋งํˆฌ", "์ „๋ฌธ์ ์ธ๋งํˆฌ"],
66
+ label="๋งํˆฌ ์„ ํƒ",
67
+ value="์ผ๋ฐ˜์ ์ธ๋งํˆฌ"
68
+ )
69
  with gr.Row():
70
+ ref_url1 = gr.Textbox(label="์ฐธ์กฐ๊ธ€ 1 URL", placeholder="https://example.com/article1")
71
+ ref_url2 = gr.Textbox(label="์ฐธ์กฐ๊ธ€ 2 URL", placeholder="https://example.com/article2")
72
+ ref_url3 = gr.Textbox(label="์ฐธ์กฐ๊ธ€ 3 URL", placeholder="https://example.com/article3")
73
+
74
+ generate_btn = gr.Button("๋ธ”๋กœ๊ทธ ๊ธ€ ์ƒ์„ฑ")
75
+ output = gr.Textbox(label="์ƒ์„ฑ๋œ ๋ธ”๋กœ๊ทธ ๊ธ€", lines=20)
76
+
77
+ generate_btn.click(generate_blog_post, inputs=[tone, ref_url1, ref_url2, ref_url3], outputs=output)
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  if __name__ == "__main__":
 
80
  demo.launch()