seawolf2357 commited on
Commit
5b0e95f
โ€ข
1 Parent(s): 5fd7cca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -59
app.py CHANGED
@@ -1,61 +1,35 @@
1
- import os
2
- import tempfile
3
  import gradio as gr
4
- from huggingface_hub import HfApi
5
- import logging
6
-
7
- # ๋กœ๊น… ์„ค์ •
8
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
9
-
10
- # ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ Hugging Face ํ† ํฐ ์ฝ๊ธฐ
11
- hf_token = os.getenv("TOKEN")
12
- if not hf_token:
13
- logging.error("Hugging Face token is not set. Please set the HF_TOKEN environment variable.")
14
- exit()
15
-
16
- # Hugging Face API ์ดˆ๊ธฐํ™”
17
- api = HfApi()
18
-
19
- def upload_file_to_hf_space(uploaded_file):
20
- user_id = "seawolf2357"
21
- space_name = "video"
22
- repo_id = f"{user_id}/{space_name}"
23
-
24
- # ์ž„์‹œ ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ์—…๋กœ๋“œ๋œ ํŒŒ์ผ ๋ฐ์ดํ„ฐ ์“ฐ๊ธฐ
25
- with tempfile.NamedTemporaryFile(delete=True, suffix='.mp4', mode='wb') as tmp_file:
26
- # ์—…๋กœ๋“œ๋œ ํŒŒ์ผ์˜ ๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ž„์‹œ ํŒŒ์ผ์— ์“ฐ๊ธฐ
27
- if isinstance(uploaded_file, bytes):
28
- tmp_file.write(uploaded_file)
29
- else:
30
- # Gradio๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ํŒŒ์ผ ๋ฐ์ดํ„ฐ๊ฐ€ bytes๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
31
- data = uploaded_file.read() # ํŒŒ์ผ ๊ฐ์ฒด์—์„œ ๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ
32
- tmp_file.write(data)
33
-
34
- tmp_file.flush()
35
- file_path = tmp_file.name
36
-
37
- logging.info(f"Uploading {file_path} to Hugging Face Spaces")
38
- try:
39
- api.upload_file(
40
- path_or_fileobj=file_path,
41
- path_in_repo=os.path.basename(file_path),
42
- repo_id=repo_id,
43
- token=hf_token,
44
- )
45
- uploaded_file_url = f"https://huggingface.co/spaces/{repo_id}/blob/main/{os.path.basename(file_path)}"
46
- logging.info(f"File uploaded successfully: {uploaded_file_url}")
47
- return uploaded_file_url
48
- except Exception as e:
49
- logging.error(f"Failed to upload file: {e}")
50
- return "Failed to upload file."
51
-
52
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ • ๋ฐ ์‹คํ–‰
53
- iface = gr.Interface(
54
- fn=upload_file_to_hf_space,
55
- inputs=gr.File(label="Upload your MP4 file"),
56
- outputs="text",
57
- title="MP4 File Upload to Hugging Face Spaces",
58
- description="Upload an MP4 file and get its URL in Hugging Face Spaces. Please ensure the file is an MP4 format."
59
- )
60
 
61
- iface.launch(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from moviepy.editor import ImageSequenceClip
3
+ import tempfile
4
+ import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ def images_to_video(image_files):
7
+ # ์ž„์‹œ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ƒ์„ฑ
8
+ temp_dir = tempfile.mkdtemp()
9
+
10
+ # ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ์ž„์‹œ ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ์ €์žฅ
11
+ image_paths = []
12
+ for i, image_file in enumerate(image_files):
13
+ image_path = os.path.join(temp_dir, f"image_{i}.png")
14
+ with open(image_path, "wb") as f:
15
+ f.write(image_file)
16
+ image_paths.append(image_path)
17
+
18
+ # ์ด๋ฏธ์ง€ ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ๋น„๋””์˜ค ํด๋ฆฝ ์ƒ์„ฑ (๊ฐ ์ด๋ฏธ์ง€์˜ ์ง€์† ์‹œ๊ฐ„์€ 2์ดˆ)
19
+ clip = ImageSequenceClip(image_paths, fps=0.5) # fps=0.5 => ๊ฐ ์ด๋ฏธ์ง€๋Š” 2์ดˆ ๋™์•ˆ ๋ณด์ž„
20
+
21
+ # ๋น„๋””์˜ค ํŒŒ์ผ ์ €์žฅ
22
+ output_video_path = os.path.join(temp_dir, "output.mp4")
23
+ clip.write_videofile(output_video_path, fps=24) # 24fps๋กœ ์ถœ๋ ฅ ๋น„๋””์˜ค ์ €์žฅ
24
+
25
+ # ์ƒ์„ฑ๋œ ๋น„๋””์˜ค ํŒŒ์ผ ๊ฒฝ๋กœ ๋ฐ˜ํ™˜
26
+ return output_video_path
27
+
28
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
29
+ with gr.Blocks() as demo:
30
+ with gr.Row():
31
+ file_input = gr.File(label="Upload images", type="file", accept="image/*", multiple=True)
32
+ video_output = gr.Video(label="Output video")
33
+ file_input.change(images_to_video, inputs=file_input, outputs=video_output)
34
+
35
+ demo.launch()