ginipick commited on
Commit
472e171
·
verified ·
1 Parent(s): ce8c568

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -97
app.py CHANGED
@@ -4,38 +4,13 @@ import os
4
  import logging
5
  import json
6
  from datetime import datetime
7
- import tempfile
8
- import numpy as np
9
- from PIL import Image
10
  import shutil
11
- import httpx
12
- import time
13
- import base64
14
- from gradio_client import Client, handle_file
15
- import cv2
16
- from moviepy.editor import VideoFileClip
17
 
18
  # 로깅 설정
19
  logging.basicConfig(level=logging.INFO)
20
 
21
- # 타임아웃 설정을 30초로 늘림
22
- httpx_client = httpx.Client(timeout=30.0)
23
-
24
- max_retries = 3
25
- retry_delay = 5 # 5초 대기
26
-
27
- for attempt in range(max_retries):
28
- try:
29
- api_client = Client("http://211.233.58.202:7960/")
30
- api_client.httpx_client = httpx_client # httpx 클라이언트 설정
31
- break # 성공하면 루프 종료
32
- except httpx.ReadTimeout:
33
- if attempt < max_retries - 1: # 마지막 시도가 아니면
34
- print(f"Connection timed out. Retrying in {retry_delay} seconds...")
35
- time.sleep(retry_delay)
36
- else:
37
- print("Failed to connect after multiple attempts.")
38
- raise # 모든 시도 실패 시 예외 발생
39
 
40
  # 갤러리 저장 디렉토리 설정
41
  GALLERY_DIR = "gallery"
@@ -44,16 +19,16 @@ GALLERY_JSON = "gallery.json"
44
  # 갤러리 디렉토리 생성
45
  os.makedirs(GALLERY_DIR, exist_ok=True)
46
 
47
- def save_to_gallery(video_path, prompt):
48
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
49
- new_video_path = os.path.join(GALLERY_DIR, f"{timestamp}.mp4")
50
 
51
- # 비디오 파일 복사
52
- shutil.copy2(video_path, new_video_path)
53
 
54
  # 갤러리 정보 저장
55
  gallery_info = {
56
- "video": new_video_path,
57
  "prompt": prompt,
58
  "timestamp": timestamp
59
  }
@@ -69,55 +44,44 @@ def save_to_gallery(video_path, prompt):
69
  with open(GALLERY_JSON, "w") as f:
70
  json.dump(gallery, f, indent=2)
71
 
72
- return new_video_path
73
 
74
  def load_gallery():
75
  if os.path.exists(GALLERY_JSON):
76
  with open(GALLERY_JSON, "r") as f:
77
  gallery = json.load(f)
78
- return [{"image": item["video"], "caption": item["prompt"]} for item in reversed(gallery)]
79
  return []
80
 
81
- def update_gallery():
82
- gallery_items = load_gallery()
83
- return [
84
- {"video": item['image'], "caption": item['caption']}
85
- for item in gallery_items
86
- ]
87
-
88
- def respond(image, prompt, steps, cfg_scale, eta, fs, seed, video_length):
89
- logging.info(f"Received prompt: {prompt}, steps: {steps}, cfg_scale: {cfg_scale}, "
90
- f"eta: {eta}, fs: {fs}, seed: {seed}, video_length: {video_length}")
91
 
92
  try:
93
- # 이미지 파일 처리
94
- if image is not None:
95
- image_file = handle_file(image)
96
- else:
97
- image_file = None
98
-
99
- # 비디오 생성 요청
100
  result = api_client.predict(
101
- image=image_file,
102
- prompt=prompt,
103
- steps=steps,
104
- cfg_scale=cfg_scale,
105
- eta=eta,
106
- fs=fs,
107
  seed=seed,
108
- video_length=video_length,
109
- api_name="/infer"
 
 
 
 
110
  )
111
  logging.info("API response received: %s", result)
112
-
113
- if isinstance(result, dict) and 'video' in result:
114
- saved_video_path = save_to_gallery(result['video'], prompt)
115
- return saved_video_path
 
 
116
  else:
117
- raise ValueError("예상치 못한 API 응답 형식")
118
  except Exception as e:
119
- logging.error("API 요청 오류 발생: %s", str(e))
120
- return "오류로 인해 비디오 생성에 실패했습니다."
121
 
122
  css = """
123
  footer {
@@ -126,12 +90,20 @@ footer {
126
  """
127
 
128
  examples = [
129
- ["A glamorous young woman with long, wavy blonde hair and smokey eye makeup, posing in a luxury hotel room. She's wearing a sparkly gold cocktail dress and holding up a white card with 'openfree.ai' written on it in elegant calligraphy. Soft, warm lighting creates a luxurious atmosphere. ", "q1.webp"],
 
 
 
 
 
 
 
130
  ["A fit male fitness influencer with short dark hair and stubble, standing shirtless in a modern gym. He has defined abs and arm muscles, and is holding a protein shake in one hand and a card that says 'openfree.ai' in the other. Bright, clean lighting highlights his physique.", "q2.webp"],
131
- ["A bohemian-style female travel blogger with sun-kissed skin and messy beach waves, sitting on a tropical beach at sunset. She's wearing a flowy white sundress and holding up a weathered postcard with 'openfree.ai' scrawled on it. Golden hour lighting bathes the scene in warm tones. ", "q3.webp"],
132
- ["A trendy male fashion influencer with perfectly styled hair and designer stubble, posing on a city street. He's wearing a tailored suit and holding up a sleek black business card with 'openfree.ai' printed in minimalist white font. The background shows blurred city lights, creating a chic urban atmosphere.", "q4.webp"],
133
- ["A fresh-faced young female beauty guru with freckles and natural makeup, sitting at a vanity covered in cosmetics. She's wearing a pastel pink robe and holding up a makeup palette with 'openfree.ai' written on it in lipstick. Soft, flattering lighting enhances her radiant complexion. ", "q5.webp"],
134
- ["A stylish young woman with long, wavy ombre hair and winged eyeliner, posing in front of a neon-lit city skyline at night. She's wearing a sleek black leather jacket over a sparkly crop top and holding up a holographic business card that says 'openfree.ai' in futuristic font. The card reflects the colorful neon lights, creating a cyberpunk aesthetic.", "q6.webp"],
 
135
  ["Photo realistic scene inspired by LOTR: [A tiny red dragon sleeps curled up in a nest on a medieval wizard's table]. Shot with a macro lens (f/2.8, 50mm) and a Canon EOSR5, the soft focus captures [the cozy morning light filtering through a near by window]. The pastel colors and whimsical steam shapes enhance the serene atmosphere, evoking a DnD RPG setting. The image is rendered in 16K and 8K, highlighting [the intricate details and medieval charm].", "q8.webp"],
136
  ["썬글라스를 착용한 귀여운 강아지가 'Openfree.ai'라고 쓰여진 표지판을 들고있다.", "q9.webp"],
137
  ["미레적인 도시의 해지는 풍경", "q10.webp"],
@@ -147,64 +119,64 @@ examples = [
147
  ["A fantasy map of a fictional world, with detailed terrain and cities.", "q19.webp"]
148
  ]
149
 
150
- def use_prompt_and_image(prompt, image):
151
- return prompt, image
152
-
153
- def show_video(evt: gr.SelectData):
154
- return evt.value["video"] if isinstance(evt.value, dict) and "video" in evt.value else None
155
 
156
  with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
157
  with gr.Tab("Generate"):
158
  with gr.Row():
159
- input_image = gr.Image(label="Upload an image", type="filepath")
160
- input_text = gr.Textbox(label="Enter your prompt for video generation")
161
- output_video = gr.Video(label="Generated Video")
162
 
163
  with gr.Row():
164
- steps = gr.Slider(minimum=1, maximum=100, step=1, label="Steps", value=30)
165
- cfg_scale = gr.Slider(minimum=1, maximum=15, step=0.1, label="CFG Scale", value=3.5)
166
- eta = gr.Slider(minimum=0, maximum=1, step=0.1, label="ETA", value=1)
167
- fs = gr.Slider(minimum=1, maximum=30, step=1, label="FPS", value=8)
168
  seed = gr.Slider(minimum=0, maximum=1000000, step=1, label="Seed", value=123)
169
- video_length = gr.Slider(minimum=1, maximum=10, step=1, label="Video Length (seconds)", value=2)
 
 
 
 
 
 
 
 
170
 
171
  with gr.Row():
172
  for prompt, image_file in examples:
173
  with gr.Column():
174
  gr.Image(image_file, label=prompt[:50] + "...")
175
- gr.Button("Use this example").click(
176
- fn=use_prompt_and_image,
177
  inputs=[],
178
- outputs=[input_text, input_image],
179
  api_name=False
180
  ).then(
181
- lambda p=prompt, i=image_file: (p, i),
182
  inputs=[],
183
- outputs=[input_text, input_image]
184
  )
185
-
186
  with gr.Tab("Gallery"):
187
  gallery = gr.Gallery(
188
- label="Generated Videos",
189
  show_label=False,
190
  elem_id="gallery",
191
  columns=[5],
192
  rows=[3],
193
  object_fit="contain",
194
- height="auto",
195
- preview=True
196
  )
197
- selected_video = gr.Video(label="Selected Video")
198
  refresh_btn = gr.Button("Refresh Gallery")
199
 
 
 
 
200
  refresh_btn.click(fn=update_gallery, inputs=None, outputs=gallery)
201
  demo.load(fn=update_gallery, inputs=None, outputs=gallery)
202
- gallery.select(show_video, None, selected_video)
203
 
204
  input_text.submit(
205
  fn=respond,
206
- inputs=[input_image, input_text, steps, cfg_scale, eta, fs, seed, video_length],
207
- outputs=output_video
208
  ).then(
209
  fn=update_gallery,
210
  inputs=None,
 
4
  import logging
5
  import json
6
  from datetime import datetime
 
 
 
7
  import shutil
 
 
 
 
 
 
8
 
9
  # 로깅 설정
10
  logging.basicConfig(level=logging.INFO)
11
 
12
+ # API 클라이언트 설정
13
+ api_client = Client("http://211.233.58.202:7960/")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  # 갤러리 저장 디렉토리 설정
16
  GALLERY_DIR = "gallery"
 
19
  # 갤러리 디렉토리 생성
20
  os.makedirs(GALLERY_DIR, exist_ok=True)
21
 
22
+ def save_to_gallery(image_path, prompt):
23
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
24
+ new_image_path = os.path.join(GALLERY_DIR, f"{timestamp}.png")
25
 
26
+ # 이미지 파일 복사
27
+ shutil.copy2(image_path, new_image_path)
28
 
29
  # 갤러리 정보 저장
30
  gallery_info = {
31
+ "image": new_image_path,
32
  "prompt": prompt,
33
  "timestamp": timestamp
34
  }
 
44
  with open(GALLERY_JSON, "w") as f:
45
  json.dump(gallery, f, indent=2)
46
 
47
+ return new_image_path
48
 
49
  def load_gallery():
50
  if os.path.exists(GALLERY_JSON):
51
  with open(GALLERY_JSON, "r") as f:
52
  gallery = json.load(f)
53
+ return [(item["image"], item["prompt"]) for item in reversed(gallery)]
54
  return []
55
 
56
+ def respond(message, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
57
+ logging.info(f"Received message: {message}, seed: {seed}, randomize_seed: {randomize_seed}, "
58
+ f"width: {width}, height: {height}, guidance_scale: {guidance_scale}, "
59
+ f"num_inference_steps: {num_inference_steps}")
 
 
 
 
 
 
60
 
61
  try:
62
+ # 이미지 생성 요청
 
 
 
 
 
 
63
  result = api_client.predict(
64
+ prompt=message,
 
 
 
 
 
65
  seed=seed,
66
+ randomize_seed=randomize_seed,
67
+ width=width,
68
+ height=height,
69
+ guidance_scale=guidance_scale,
70
+ num_inference_steps=num_inference_steps,
71
+ api_name="/infer_t2i"
72
  )
73
  logging.info("API response received: %s", result)
74
+
75
+ # 결과 확인 처리
76
+ if isinstance(result, tuple) and len(result) >= 1:
77
+ image_path = result[0]
78
+ saved_image_path = save_to_gallery(image_path, message)
79
+ return saved_image_path
80
  else:
81
+ raise ValueError("Unexpected API response format")
82
  except Exception as e:
83
+ logging.error("Error during API request: %s", str(e))
84
+ return "Failed to generate image due to an error."
85
 
86
  css = """
87
  footer {
 
90
  """
91
 
92
  examples = [
93
+ ["A photograph of the front view portrait of an Man in a full body dynamic pose on a red background in the style of high fashion moment, with rich colors, dramatic light, in a fantasy art style, with surrealism, elegant details, a golden ratio composition, and detailed texture", "q21.png"],
94
+ ["the image show the fourth elements, each one in a part of the picture, first part is at top left and show a splashing multicolor water text with many water reflections, the text is made of water, the water word is 'WATER', the background is splashing water, the second part of the image is a top right and show a soil rounded text, the word made of soil is 'EARTH', the background is planet earth, the third part of the image is at bottom left and show a cloud multicolor rounded text, the word is 'AIR' made of colorfull cloud the background is a sunset, and the last part of the image in the bottom right show a red fire rounded text made of lava, the colorfull big word made of fire is 'FIRE', the background is the closeup ", "q22.png"],
95
+ ["Dramatic news broadcast scene in a Teahupoʻo wave's where a cow surfing, mimicking pro surf rider poses. Yogis laugh and take pictures. The news banner reads: 'COW win Olympics!!'. An inset shows an anchor openfree.ai@","q23.png"],
96
+ ["a beautiful cute joyful and playful 29 year old woman view from bottom, red haired, dressed in a cozy sherpa jacket over a turtleneck and skinny jeans, in the street of Paris at night eiffel tower in background, detailed masterpiece most beautiful artwork in the world Ultrarealistic, Sony A7, Nostalgic lighting","q24.png"],
97
+ ["A man climbing a rock wall reaching for the next hold", "q25.png"],
98
+ ["pov of an austronaut holding a small red rock that has a portal showing the Statue of Liberty, alien landscape of a red planet in the background, surreal","q26.png"],
99
+ ["a smartphone product showcase, smartphone display showing immersive wallpaper and time, blurred turquoise background","q27.png"],
100
+ ["A glamorous young woman with long, wavy blonde hair and smokey eye makeup, posing in a luxury hotel room. She’s wearing a sparkly gold cocktail dress and holding up a white card with 'openfree.ai' written on it in elegant calligraphy. Soft, warm lighting creates a luxurious atmosphere. ", "q1.webp"],
101
  ["A fit male fitness influencer with short dark hair and stubble, standing shirtless in a modern gym. He has defined abs and arm muscles, and is holding a protein shake in one hand and a card that says 'openfree.ai' in the other. Bright, clean lighting highlights his physique.", "q2.webp"],
102
+ ["A bohemian-style female travel blogger with sun-kissed skin and messy beach waves, sitting on a tropical beach at sunset. Shes wearing a flowy white sundress and holding up a weathered postcard with 'openfree.ai' scrawled on it. Golden hour lighting bathes the scene in warm tones. ", "q3.webp"],
103
+ ["A trendy male fashion influencer with perfectly styled hair and designer stubble, posing on a city street. Hes wearing a tailored suit and holding up a sleek black business card with 'openfree.ai' printed in minimalist white font. The background shows blurred city lights, creating a chic urban atmosphere.", "q4.webp"],
104
+ ["A fresh-faced young female beauty guru with freckles and natural makeup, sitting at a vanity covered in cosmetics. Shes wearing a pastel pink robe and holding up a makeup palette with 'openfree.ai' written on it in lipstick. Soft, flattering lighting enhances her radiant complexion. ", "q5.webp"],
105
+ ["A stylish young woman with long, wavy ombre hair and winged eyeliner, posing in front of a neon-lit city skyline at night. Shes wearing a sleek black leather jacket over a sparkly crop top and holding up a holographic business card that says 'openfree.ai' in futuristic font. The card reflects the colorful neon lights, creating a cyberpunk aesthetic.", "q6.webp"],
106
+ ["Create a surreal advertisement poster for a fictional time travel agency. The background should depict a swirling vortex of clock faces and historical landmarks from different eras. In the foreground, place large, bold text that reads “AI TOURS: YOUR PAST IS OUR FUTURE” in a retro-futuristic font. The text should appear to be partially disintegrating into particles that are being sucked into the time vortex. Include smaller text at the bottom with fictional pricing and the slogan “History is just a ticket away!”", "q7.webp"],
107
  ["Photo realistic scene inspired by LOTR: [A tiny red dragon sleeps curled up in a nest on a medieval wizard's table]. Shot with a macro lens (f/2.8, 50mm) and a Canon EOSR5, the soft focus captures [the cozy morning light filtering through a near by window]. The pastel colors and whimsical steam shapes enhance the serene atmosphere, evoking a DnD RPG setting. The image is rendered in 16K and 8K, highlighting [the intricate details and medieval charm].", "q8.webp"],
108
  ["썬글라스를 착용한 귀여운 강아지가 'Openfree.ai'라고 쓰여진 표지판을 들고있다.", "q9.webp"],
109
  ["미레적인 도시의 해지는 풍경", "q10.webp"],
 
119
  ["A fantasy map of a fictional world, with detailed terrain and cities.", "q19.webp"]
120
  ]
121
 
122
+ def use_prompt(prompt):
123
+ return prompt
 
 
 
124
 
125
  with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
126
  with gr.Tab("Generate"):
127
  with gr.Row():
128
+ input_text = gr.Textbox(label="Enter your prompt for image generation")
129
+ output_image = gr.Image(label="Generated Image")
 
130
 
131
  with gr.Row():
 
 
 
 
132
  seed = gr.Slider(minimum=0, maximum=1000000, step=1, label="Seed", value=123)
133
+ randomize_seed = gr.Checkbox(label="Randomize Seed", value=False)
134
+
135
+ with gr.Row():
136
+ width = gr.Slider(minimum=256, maximum=1024, step=64, label="Width", value=1024)
137
+ height = gr.Slider(minimum=256, maximum=1024, step=64, label="Height", value=576)
138
+
139
+ with gr.Row():
140
+ guidance_scale = gr.Slider(minimum=1, maximum=20, step=0.1, label="Guidance Scale", value=5)
141
+ num_inference_steps = gr.Slider(minimum=1, maximum=100, step=1, label="Number of Inference Steps", value=28)
142
 
143
  with gr.Row():
144
  for prompt, image_file in examples:
145
  with gr.Column():
146
  gr.Image(image_file, label=prompt[:50] + "...")
147
+ gr.Button("Use this prompt").click(
148
+ fn=use_prompt,
149
  inputs=[],
150
+ outputs=input_text,
151
  api_name=False
152
  ).then(
153
+ lambda x=prompt: x,
154
  inputs=[],
155
+ outputs=input_text
156
  )
157
+
158
  with gr.Tab("Gallery"):
159
  gallery = gr.Gallery(
160
+ label="Generated Images",
161
  show_label=False,
162
  elem_id="gallery",
163
  columns=[5],
164
  rows=[3],
165
  object_fit="contain",
166
+ height="auto"
 
167
  )
 
168
  refresh_btn = gr.Button("Refresh Gallery")
169
 
170
+ def update_gallery():
171
+ return load_gallery()
172
+
173
  refresh_btn.click(fn=update_gallery, inputs=None, outputs=gallery)
174
  demo.load(fn=update_gallery, inputs=None, outputs=gallery)
 
175
 
176
  input_text.submit(
177
  fn=respond,
178
+ inputs=[input_text, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
179
+ outputs=output_image
180
  ).then(
181
  fn=update_gallery,
182
  inputs=None,