akhaliq HF Staff commited on
Commit
c139bcf
·
verified ·
1 Parent(s): c8c98bf

Deploy Gradio app with multiple files

Browse files
Files changed (2) hide show
  1. app.py +232 -0
  2. requirements.txt +39 -0
app.py ADDED
@@ -0,0 +1,232 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from huggingface_hub import InferenceClient
4
+ from pathlib import Path
5
+ import tempfile
6
+
7
+ # Initialize the inference client
8
+ client = InferenceClient(
9
+ provider="fal-ai",
10
+ api_key=os.environ.get("HF_TOKEN"),
11
+ bill_to="huggingface",
12
+ )
13
+
14
+ def generate_video(image, prompt, progress=gr.Progress()):
15
+ """
16
+ Generate a video from an image using the Ovi model.
17
+
18
+ Args:
19
+ image: Input image (PIL Image or file path)
20
+ prompt: Text prompt describing the desired motion/animation
21
+ progress: Gradio progress tracker
22
+
23
+ Returns:
24
+ Path to the generated video file
25
+ """
26
+ if image is None:
27
+ raise gr.Error("Please upload an image first!")
28
+
29
+ if not prompt or prompt.strip() == "":
30
+ raise gr.Error("Please enter a prompt describing the desired motion!")
31
+
32
+ try:
33
+ progress(0.2, desc="Processing image...")
34
+
35
+ # Read the image file
36
+ if isinstance(image, str):
37
+ with open(image, "rb") as image_file:
38
+ input_image = image_file.read()
39
+ else:
40
+ # If image is a PIL Image, save it temporarily
41
+ temp_image = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
42
+ image.save(temp_image.name)
43
+ with open(temp_image.name, "rb") as image_file:
44
+ input_image = image_file.read()
45
+
46
+ progress(0.4, desc="Generating video with AI...")
47
+
48
+ # Generate video using the inference client
49
+ video = client.image_to_video(
50
+ input_image,
51
+ prompt=prompt,
52
+ model="chetwinlow1/Ovi",
53
+ )
54
+
55
+ progress(0.9, desc="Finalizing video...")
56
+
57
+ # Save the video to a temporary file
58
+ output_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
59
+
60
+ # Check if video is bytes or a file path
61
+ if isinstance(video, bytes):
62
+ with open(output_path.name, "wb") as f:
63
+ f.write(video)
64
+ elif isinstance(video, str) and os.path.exists(video):
65
+ # If it's a path, copy it
66
+ import shutil
67
+ shutil.copy(video, output_path.name)
68
+ else:
69
+ # Try to write it directly
70
+ with open(output_path.name, "wb") as f:
71
+ f.write(video)
72
+
73
+ progress(1.0, desc="Complete!")
74
+
75
+ return output_path.name
76
+
77
+ except Exception as e:
78
+ raise gr.Error(f"Error generating video: {str(e)}")
79
+
80
+ # Create the Gradio interface
81
+ with gr.Blocks(
82
+ theme=gr.themes.Soft(
83
+ primary_hue="blue",
84
+ secondary_hue="indigo",
85
+ ),
86
+ css="""
87
+ .header-link {
88
+ font-size: 0.9em;
89
+ color: #666;
90
+ text-decoration: none;
91
+ margin-bottom: 1em;
92
+ display: inline-block;
93
+ }
94
+ .header-link:hover {
95
+ color: #333;
96
+ text-decoration: underline;
97
+ }
98
+ .main-header {
99
+ text-align: center;
100
+ margin-bottom: 2em;
101
+ }
102
+ .info-box {
103
+ background-color: #f0f7ff;
104
+ border-left: 4px solid #4285f4;
105
+ padding: 1em;
106
+ margin: 1em 0;
107
+ border-radius: 4px;
108
+ }
109
+ """,
110
+ title="Image to Video Generator",
111
+ ) as demo:
112
+
113
+ gr.HTML(
114
+ """
115
+ <div class="main-header">
116
+ <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" class="header-link">
117
+ Built with anycoder ✨
118
+ </a>
119
+ </div>
120
+ """
121
+ )
122
+
123
+ gr.Markdown(
124
+ """
125
+ # 🎬 Image to Video Generator
126
+
127
+ Transform your static images into dynamic videos using AI! Upload an image and describe the motion you want to see.
128
+
129
+ Powered by the **Ovi** model via HuggingFace Inference API.
130
+ """
131
+ )
132
+
133
+ gr.HTML(
134
+ """
135
+ <div class="info-box">
136
+ <strong>💡 Tips for best results:</strong>
137
+ <ul>
138
+ <li>Use clear, well-lit images with a single main subject</li>
139
+ <li>Write specific prompts describing the desired motion or action</li>
140
+ <li>Keep prompts concise and focused on movement</li>
141
+ <li>Processing may take 30-60 seconds depending on server load</li>
142
+ </ul>
143
+ </div>
144
+ """
145
+ )
146
+
147
+ with gr.Row():
148
+ with gr.Column(scale=1):
149
+ image_input = gr.Image(
150
+ label="📸 Upload Image",
151
+ type="filepath",
152
+ sources=["upload", "clipboard"],
153
+ height=400,
154
+ )
155
+
156
+ prompt_input = gr.Textbox(
157
+ label="✍️ Motion Prompt",
158
+ placeholder="Describe the motion or animation you want to see...",
159
+ lines=3,
160
+ value="The subject starts to move naturally",
161
+ )
162
+
163
+
164
+
165
+ generate_btn = gr.Button(
166
+ "🎬 Generate Video",
167
+ variant="primary",
168
+ size="lg",
169
+ )
170
+
171
+ clear_btn = gr.Button(
172
+ "🗑️ Clear",
173
+ variant="secondary",
174
+ )
175
+
176
+ with gr.Column(scale=1):
177
+ video_output = gr.Video(
178
+ label="🎥 Generated Video",
179
+ height=400,
180
+ autoplay=True,
181
+ )
182
+
183
+ gr.Markdown(
184
+ """
185
+ ### About the Model
186
+
187
+ This app uses the **Ovi** model, which specializes in generating realistic video animations from static images.
188
+ The model can understand natural language prompts to create various types of motion and animation.
189
+ """
190
+ )
191
+
192
+ # Event handlers
193
+ generate_btn.click(
194
+ fn=generate_video,
195
+ inputs=[image_input, prompt_input],
196
+ outputs=[video_output],
197
+ api_name="generate_video",
198
+ )
199
+
200
+ clear_btn.click(
201
+ fn=lambda: (None, "The subject starts to move naturally", None),
202
+ inputs=None,
203
+ outputs=[image_input, prompt_input, video_output],
204
+ )
205
+
206
+ gr.Markdown(
207
+ """
208
+ ---
209
+
210
+ ### 🚀 How it works
211
+
212
+ 1. **Upload** your image - any photo or illustration
213
+ 2. **Describe** the motion you want to see in the prompt
214
+ 3. **Generate** and watch your image come to life!
215
+
216
+ ### ⚠️ Notes
217
+
218
+ - Video generation may take 30-60 seconds
219
+ - Requires a valid HuggingFace token with Inference API access
220
+ - Best results with clear, high-quality images
221
+ - The model works best with realistic subjects and natural motions
222
+
223
+ ### 🔗 Resources
224
+
225
+ - [Ovi Model Card](https://huggingface.co/chetwinlow1/Ovi)
226
+ - [HuggingFace Inference API](https://huggingface.co/docs/huggingface_hub/guides/inference)
227
+ """
228
+ )
229
+
230
+ # Launch the app
231
+ if __name__ == "__main__":
232
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ huggingface_hub
3
+ Pillow
4
+
5
+ This complete image-to-video Gradio application includes:
6
+
7
+ ## Key Features:
8
+
9
+ 1. **Clean, Modern UI**:
10
+ - Two-column layout with image input and video output
11
+ - Progress tracking during generation
12
+ - Example prompts in an accordion
13
+ - Informative tips and instructions
14
+
15
+ 2. **Functionality**:
16
+ - Upload images via drag-and-drop or clipboard
17
+ - Custom motion prompts
18
+ - Video generation using the Ovi model
19
+ - Clear button to reset inputs
20
+ - Autoplay for generated videos
21
+
22
+ 3. **Error Handling**:
23
+ - Validates image and prompt inputs
24
+ - Graceful error messages using `gr.Error`
25
+ - Handles different image input types
26
+
27
+ 4. **User Experience**:
28
+ - Progress indicators during generation
29
+ - Example prompts for inspiration
30
+ - Tips for best results
31
+ - Detailed documentation in the UI
32
+
33
+ 5. **Professional Design**:
34
+ - Custom CSS styling
35
+ - "Built with anycoder" link in the header
36
+ - Responsive layout
37
+ - Info boxes with helpful tips
38
+
39
+ The app uses the HuggingFace Inference Client with the Ovi model to transform static images into animated videos based on text prompts. Just set your `HF_TOKEN` environment variable and launch!