Spaces:
Running
Running
DeFactOfficial
commited on
Commit
β’
a830538
1
Parent(s):
a6f672d
switch to docker runtime... gradio front end moved to other space
Browse files- Dockerfile +27 -0
- README.md +7 -9
- mmapi/api.js β api.js +0 -0
- app.py +0 -44
- app.py.old +0 -253
- ffmpeg-release-amd64-static.tar +0 -3
- init.sh β ffmpeg_install.sh +0 -0
- mmapi/image.js β image.js +0 -0
- log.txt +3 -0
- mmapi/package-lock.json β package-lock.json +0 -0
- mmapi/package.json β package.json +0 -0
- {mmapi/public β public}/index.html +0 -0
- requirements.txt +0 -1
Dockerfile
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Node base image
|
2 |
+
FROM node:20
|
3 |
+
|
4 |
+
# Switch to the "node" user
|
5 |
+
USER node
|
6 |
+
|
7 |
+
# Set home to the user's home directory
|
8 |
+
ENV HOME=/home/node \
|
9 |
+
PATH=/home/node/.local/bin:$PATH
|
10 |
+
|
11 |
+
# Set the working directory to the user's home directory
|
12 |
+
WORKDIR $HOME/app
|
13 |
+
|
14 |
+
# Moving file to user's home directory
|
15 |
+
ADD . $HOME/app
|
16 |
+
|
17 |
+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
18 |
+
COPY --chown=node . $HOME/app
|
19 |
+
|
20 |
+
# Loading Dependencies
|
21 |
+
RUN npm install
|
22 |
+
RUN ffmpeg_install.sh
|
23 |
+
# Expose application's default port
|
24 |
+
EXPOSE 7860
|
25 |
+
|
26 |
+
# Entry Point
|
27 |
+
ENTRYPOINT ["nodejs", "./api.js"]
|
README.md
CHANGED
@@ -1,13 +1,11 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
colorFrom: green
|
5 |
-
colorTo:
|
6 |
-
sdk:
|
7 |
-
|
8 |
-
|
9 |
-
pinned: false
|
10 |
-
short_description: create optimized text+image inputs for text2video models
|
11 |
---
|
12 |
|
13 |
-
|
|
|
1 |
---
|
2 |
+
title: MMAPI Service
|
3 |
+
emoji: π’
|
4 |
colorFrom: green
|
5 |
+
colorTo: pink
|
6 |
+
sdk: docker
|
7 |
+
pinned: true
|
8 |
+
app_port: 7860
|
|
|
|
|
9 |
---
|
10 |
|
11 |
+
This is the coolest API ever...
|
mmapi/api.js β api.js
RENAMED
File without changes
|
app.py
DELETED
@@ -1,44 +0,0 @@
|
|
1 |
-
import subprocess
|
2 |
-
import os
|
3 |
-
import socket
|
4 |
-
from datetime import datetime
|
5 |
-
import gradio as gr
|
6 |
-
|
7 |
-
# Function to check if a port is in use
|
8 |
-
def is_port_in_use(port):
|
9 |
-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
10 |
-
return sock.connect_ex(('localhost', port)) == 0
|
11 |
-
|
12 |
-
# Log file path
|
13 |
-
log_file_path = 'log.txt'
|
14 |
-
|
15 |
-
# Log the startup time
|
16 |
-
with open(log_file_path, 'a') as log_file:
|
17 |
-
log_file.write(f"\n[{datetime.now()}] Script started.\n")
|
18 |
-
|
19 |
-
# Port number to check
|
20 |
-
port_to_check = 7860
|
21 |
-
|
22 |
-
# Check if the port is already in use
|
23 |
-
if is_port_in_use(port_to_check):
|
24 |
-
print("API service already running, enjoy!")
|
25 |
-
with open(log_file_path, 'a') as log_file:
|
26 |
-
log_file.write(f"[{datetime.now()}] API service already running on port {port_to_check}, exiting script.\n")
|
27 |
-
else:
|
28 |
-
# Define the command to run
|
29 |
-
command = ['node', 'api.js']
|
30 |
-
|
31 |
-
# Log the startup information
|
32 |
-
with open(log_file_path, 'a') as log_file:
|
33 |
-
log_file.write(f"[{datetime.now()}] No service found on port {port_to_check}. Starting API service...\n")
|
34 |
-
|
35 |
-
# Open the log file for writing the process output
|
36 |
-
with open(log_file_path, 'a') as log_file:
|
37 |
-
# Spawn the node process if the port is not in use
|
38 |
-
process = subprocess.Popen(command, stdout=log_file, stderr=subprocess.STDOUT,
|
39 |
-
stdin=subprocess.DEVNULL, close_fds=True,
|
40 |
-
start_new_session=True)
|
41 |
-
|
42 |
-
# Log the process spawn success
|
43 |
-
with open(log_file_path, 'a') as log_file:
|
44 |
-
log_file.write(f"[{datetime.now()}] API service started and running in the background.\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py.old
DELETED
@@ -1,253 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
|
3 |
-
import argparse
|
4 |
-
from openai import OpenAI
|
5 |
-
import base64
|
6 |
-
from mimetypes import guess_type
|
7 |
-
|
8 |
-
|
9 |
-
preferred_txt2image_diffusion_model= "black-forest-labs/FLUX.1-dev"
|
10 |
-
txt2image_endpoint=f"http://localhost:8000/generate/image?model={preferred_txt2image_diffusion_model}&prompt="
|
11 |
-
|
12 |
-
|
13 |
-
sys_prompt_i2v = """
|
14 |
-
|
15 |
-
You are a highly skilled videographer and creative director who transforms the user's creative vision into the sort of detailed yet concise caption which, combined with the provided image,
|
16 |
-
will provide grounding and direction to a specialized video generation model which will create a stunning video clip based on your work.
|
17 |
-
Your task is to provide a vivid and engaging video description that brings the user's vision to life, while remaining precise and concise.
|
18 |
-
|
19 |
-
**Note**: The input image is the first frame of the video, and the output video caption should describe the motion starting from the current image. User input is optional and can be empty.
|
20 |
-
|
21 |
-
**Note**: Do NOT include transitions from one shot to the next. The caption must describe a single shot (with camera motion) but no jump cuts etc - after you create the caption, an advanced specialist AI will create a broadcast quality, HD video clip of between 5 and 10 seconds. The video generation is conditioned on the same image that the user has provided here (as a starting point) and whatever you decide to output as a caption. (if user doesn't specify, assume 6s when thinking of a caption - but never say the length in the caption - the user specifies that separately in a UI). PLEASE do a good job and make an effort to be creative and descriptive, particularly regarding motion. This video gen model is bleeding edge stuff and a 10s video takes 15m to generate on a very expensive A100 that I personally am renting. So try our best not to mess it up please :)
|
22 |
-
|
23 |
-
**Answering Style**:
|
24 |
-
Answers should be comprehensive, conversational, and use complete sentences. The answer should be in English no matter what the user's input is. Provide context where necessary and maintain a certain tone. Begin directly without introductory phrases like "The image/video showcases" "The photo captures" and more. For example, say "A woman is on a beach", instead of "A woman is depicted in the image".
|
25 |
-
DO NOT include any line breaks in your response. The response should be a single continuous paragraph.
|
26 |
-
MAXIMUM LENGTH: 100 words
|
27 |
-
"""
|
28 |
-
|
29 |
-
sys_prompt_t2v="""
|
30 |
-
You are a highly skilled videographer and creative director who specializes in creating stunning videos based on the user's request.
|
31 |
-
|
32 |
-
Your job is to provide a vivid and engaging video description that brings the user's vision to life, while remaining precise and concise.
|
33 |
-
|
34 |
-
The description you provide will be sent to a specialized video rendering agent who generates a HD video clip based on your work.
|
35 |
-
|
36 |
-
Video clips can be between 2s and 10s in duration; if the user specifies the duration, keep that in mind when describing the scene... Otherwise, you can safely assume that the clip will be 6 seconds long, as that's the default setting for the txt2video model.
|
37 |
-
|
38 |
-
Remember to be descriptive and imaginative in your responses.
|
39 |
-
|
40 |
-
**Answering Style**:
|
41 |
-
Answers should be comprehensive, conversational, and use complete sentences. The answer should be in English no matter what the user's input is. Provide context where necessary and maintain a certain tone. Begin directly without introductory phrases like "The image/video showcases" "The photo captures" and more. For example, say "A woman is on a beach", instead of "A woman is depicted in the image".
|
42 |
-
DO NOT include any line breaks in your response. The response should be a single continuous paragraph.
|
43 |
-
MAXIMUM LENGTH: 100 words
|
44 |
-
|
45 |
-
"""
|
46 |
-
|
47 |
-
def image_to_url(image_path):
|
48 |
-
mime_type, _ = guess_type(image_path)
|
49 |
-
if mime_type is None:
|
50 |
-
mime_type = "application/octet-stream"
|
51 |
-
with open(image_path, "rb") as image_file:
|
52 |
-
base64_encoded_data = base64.b64encode(image_file.read()).decode("utf-8")
|
53 |
-
return f"data:{mime_type};base64,{base64_encoded_data}"
|
54 |
-
|
55 |
-
def prompt_to_image(t2v_prompt, save_to_folder=None):
|
56 |
-
client = OpenAI()
|
57 |
-
new_prompt = client.chat.completions.create(messages=[{"role": "system", "content": "You are an assistant to a videographer. They have provided you with a description of a video clip, a scene that is going to be produced. Please respond with a detailed, precise yet concise description to generate a still image: the first frame of the video scene that's been described to you"},
|
58 |
-
{"role": "user", "content": f"Video Clip Description: {t2v_prompt}"}], model="gpt-4o", temperature=0.8, top_p=0.95, stream=False)
|
59 |
-
first_frame_prompt = new_prompt.choices[0].message.content
|
60 |
-
|
61 |
-
import urllib.parse
|
62 |
-
encoded_prompt = urllib.parse.quote(first_frame_prompt)
|
63 |
-
|
64 |
-
# create the prompt-in-url link... this does not actually generate the image UNTIL THE URL IS HIT FOR THE FIRST TIME
|
65 |
-
image_url = f"{txt2image_endpoint}{encoded_prompt}"
|
66 |
-
|
67 |
-
print("Image URL: ", image_url)
|
68 |
-
if save_to_folder is not None:
|
69 |
-
print("Please wait... Rendering the image and saving it to the folder")
|
70 |
-
# download the image to the folder
|
71 |
-
import requests
|
72 |
-
response = requests.get(image_url)
|
73 |
-
image_path = os.path.join(save_to_folder, "first_frame.png")
|
74 |
-
with open(image_path, "wb") as image_file:
|
75 |
-
image_file.write(response.content)
|
76 |
-
print("Image saved to: ", image_path)
|
77 |
-
|
78 |
-
return image_url
|
79 |
-
else:
|
80 |
-
return image_url
|
81 |
-
|
82 |
-
|
83 |
-
with open(image_path, "wb") as image_file:
|
84 |
-
image_file.write(base64.b64decode(converted_prompt.split(",")[1]))
|
85 |
-
def convert_prompt(prompt: str, retry_times: int = 3, type: str = "t2v", image_path: str = None, generate_first_frame: bool = True):
|
86 |
-
"""
|
87 |
-
Convert a prompt to a format that can be used by the model for inference
|
88 |
-
"""
|
89 |
-
|
90 |
-
client = OpenAI()
|
91 |
-
## If you using with Azure OpenAI, please uncomment the below line and comment the above line
|
92 |
-
# client = AzureOpenAI(
|
93 |
-
# api_key="",
|
94 |
-
# api_version="",
|
95 |
-
# azure_endpoint=""
|
96 |
-
# )
|
97 |
-
|
98 |
-
text = prompt.strip()
|
99 |
-
for i in range(retry_times):
|
100 |
-
if type == "t2v":
|
101 |
-
response = client.chat.completions.create(
|
102 |
-
messages=[
|
103 |
-
{"role": "system", "content": f"{sys_prompt_t2v}"},
|
104 |
-
{
|
105 |
-
"role": "user",
|
106 |
-
"content": "I would like a video of a sunset over the Eiffel Tower with hot air balloons floating in the sky."
|
107 |
-
},
|
108 |
-
{
|
109 |
-
"role": "assistant",
|
110 |
-
"content": "The camera pans over Paris as the sun sets behind the Eiffel Tower, while colorful hot air balloons drift gracefully across the twilight sky."
|
111 |
-
},
|
112 |
-
{
|
113 |
-
"role": "user",
|
114 |
-
"content": "Can you create a video of a time-lapse of a flower blooming in a meadow at dawn?"
|
115 |
-
},
|
116 |
-
{
|
117 |
-
"role": "assistant",
|
118 |
-
"content": "A time-lapse captures a flower slowly blooming in a dewy meadow as the first light of dawn illuminates its petals."
|
119 |
-
},
|
120 |
-
{
|
121 |
-
"role": "user",
|
122 |
-
"content": "Please make a video showing an astronaut floating above Earth with the sun rising."
|
123 |
-
},
|
124 |
-
{
|
125 |
-
"role": "assistant",
|
126 |
-
"content": "An astronaut drifts weightlessly in space, gazing at Earth as the sun rises over the horizon, casting a golden glow."
|
127 |
-
},
|
128 |
-
{
|
129 |
-
"role": "user",
|
130 |
-
"content": "I want a video of a surfer riding a massive wave in slow motion."
|
131 |
-
},
|
132 |
-
{
|
133 |
-
"role": "assistant",
|
134 |
-
"content": "In slow motion, a surfer expertly rides a towering wave, ocean spray glistening as the wave curls around them."
|
135 |
-
},
|
136 |
-
{
|
137 |
-
"role": "user",
|
138 |
-
"content": "Create a video of a city street at night with neon signs reflecting off wet pavement."
|
139 |
-
},
|
140 |
-
{
|
141 |
-
"role": "assistant",
|
142 |
-
"content": "A bustling city street glows at night, vibrant neon signs reflecting off rain-soaked pavement as people pass by."
|
143 |
-
},
|
144 |
-
{
|
145 |
-
"role": "user",
|
146 |
-
"content": "Show a video of children flying kites in a grassy field under a clear blue sky."
|
147 |
-
},
|
148 |
-
{
|
149 |
-
"role": "assistant",
|
150 |
-
"content": "Children laugh as they run through a lush green field, their colorful kites soaring against a clear blue sky."
|
151 |
-
},
|
152 |
-
{
|
153 |
-
"role": "user",
|
154 |
-
"content": "I would like a video of a snow-covered mountain peak with clouds moving overhead."
|
155 |
-
},
|
156 |
-
{
|
157 |
-
"role": "assistant",
|
158 |
-
"content": "A majestic snow-capped mountain stands tall as time-lapse clouds drift swiftly across the sky above."
|
159 |
-
},
|
160 |
-
{
|
161 |
-
"role": "user",
|
162 |
-
"content": "Please create a video of a cat chasing a laser pointer dot around a cozy living room."
|
163 |
-
},
|
164 |
-
{
|
165 |
-
"role": "assistant",
|
166 |
-
"content": "A playful cat darts around a warm living room, eagerly chasing a moving red laser dot across the floor and furniture."
|
167 |
-
},
|
168 |
-
{
|
169 |
-
"role": "user",
|
170 |
-
"content": "Make a video of a chef cooking in a bustling kitchen with flames rising from the pan."
|
171 |
-
},
|
172 |
-
{
|
173 |
-
"role": "assistant",
|
174 |
-
"content": "A chef skillfully tosses ingredients in a sizzling pan, flames briefly rising as the kitchen buzzes with activity."
|
175 |
-
},
|
176 |
-
{
|
177 |
-
"role": "user",
|
178 |
-
"content": "I want a video of a close-up of a hummingbird sipping nectar from a vibrant red flower."
|
179 |
-
},
|
180 |
-
{
|
181 |
-
"role": "assistant",
|
182 |
-
"content": "In a detailed close-up, a hummingbird hovers effortlessly, its wings a blur as it sips nectar from a bright red flower."
|
183 |
-
},
|
184 |
-
{
|
185 |
-
"role": "user",
|
186 |
-
"content": "Create a video of a drone view of waves crashing against rugged cliffs."
|
187 |
-
},
|
188 |
-
{
|
189 |
-
"role": "assistant",
|
190 |
-
"content": "From a drone's perspective, powerful waves crash against rugged cliffs, sending sprays of white foam into the air."
|
191 |
-
},
|
192 |
-
{
|
193 |
-
"role": "user",
|
194 |
-
"content": f"{text}"
|
195 |
-
}
|
196 |
-
],
|
197 |
-
model="gpt-4o", # glm-4-plus and gpt-4o have be tested
|
198 |
-
temperature=0.8,
|
199 |
-
top_p=0.95,
|
200 |
-
stream=False
|
201 |
-
)
|
202 |
-
|
203 |
-
textual_answer = response.choices[0].message.content
|
204 |
-
if (generate_first_frame):
|
205 |
-
|
206 |
-
image_gen_url = prompt_to_image(t2v_prompt = textual_answer, save_to_folder=None)
|
207 |
-
updated_prompt = f"{textual_answer}\n\nFirst Frame: {image_gen_url}"
|
208 |
-
return updated_prompt
|
209 |
-
else:
|
210 |
-
return textual_answer
|
211 |
-
else:
|
212 |
-
response = client.chat.completions.create(
|
213 |
-
model="gpt-4o",
|
214 |
-
messages=[
|
215 |
-
{"role": "system", "content": f"{sys_prompt_i2v}"},
|
216 |
-
{
|
217 |
-
"role": "user",
|
218 |
-
"content": [
|
219 |
-
{"type": "text", "text": prompt},
|
220 |
-
{
|
221 |
-
"type": "image_url",
|
222 |
-
"image_url": {
|
223 |
-
"url": image_to_url(image_path),
|
224 |
-
},
|
225 |
-
},
|
226 |
-
],
|
227 |
-
},
|
228 |
-
],
|
229 |
-
temperature=0.7,
|
230 |
-
top_p=0.9,
|
231 |
-
stream=False,
|
232 |
-
max_tokens=100,
|
233 |
-
)
|
234 |
-
return response.choices[0].message.content
|
235 |
-
return prompt
|
236 |
-
|
237 |
-
|
238 |
-
# Create the Gradio Blocks interface
|
239 |
-
with gr.Blocks() as demo:
|
240 |
-
# Create a Textbox for the user to enter the prompt
|
241 |
-
input_prompt = gr.Textbox(label="Enter your prompt")
|
242 |
-
|
243 |
-
# Create a Button to submit the prompt
|
244 |
-
submit_button = gr.Button("Submit")
|
245 |
-
|
246 |
-
# Create a Textbox to display the result
|
247 |
-
output_result = gr.Textbox(label="Result")
|
248 |
-
|
249 |
-
# Define the event listener for the button click
|
250 |
-
submit_button.click(fn=convert_prompt, inputs=input_pr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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ffmpeg-release-amd64-static.tar
DELETED
@@ -1,3 +0,0 @@
|
|
1 |
-
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:31175d37bf41c60170775ac168f1d3de847f6adc6758b01c41bfb71027c68499
|
3 |
-
size 168796160
|
|
|
|
|
|
|
|
init.sh β ffmpeg_install.sh
RENAMED
File without changes
|
mmapi/image.js β image.js
RENAMED
File without changes
|
log.txt
CHANGED
@@ -36,3 +36,6 @@ Node.js v20.18.0
|
|
36 |
|
37 |
[2024-10-25 05:24:34.969499] Script started.
|
38 |
[2024-10-25 05:24:34.969881] API service already running on port 7860, exiting script.
|
|
|
|
|
|
|
|
36 |
|
37 |
[2024-10-25 05:24:34.969499] Script started.
|
38 |
[2024-10-25 05:24:34.969881] API service already running on port 7860, exiting script.
|
39 |
+
|
40 |
+
[2024-10-25 05:40:32.131066] Script started.
|
41 |
+
[2024-10-25 05:40:32.131521] API service already running on port 7860, exiting script.
|
mmapi/package-lock.json β package-lock.json
RENAMED
File without changes
|
mmapi/package.json β package.json
RENAMED
File without changes
|
{mmapi/public β public}/index.html
RENAMED
File without changes
|
requirements.txt
DELETED
@@ -1 +0,0 @@
|
|
1 |
-
openai
|
|
|
|