amazing stuff
Browse files- App/Generate/database/Modal.py +48 -0
- App/Generate/database/Model.py +13 -11
App/Generate/database/Modal.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import aiohttp
|
2 |
+
import asyncio
|
3 |
+
import json
|
4 |
+
from typing import Dict, Any
|
5 |
+
|
6 |
+
|
7 |
+
class ModalImageGenerator:
|
8 |
+
def __init__(self, session: aiohttp.ClientSession):
|
9 |
+
self.session = session
|
10 |
+
self.base_url = (
|
11 |
+
"https://allanwatts705--kolors-app-model-web-inference.modal.run/"
|
12 |
+
)
|
13 |
+
|
14 |
+
async def generate_image(
|
15 |
+
self, prompt: str, reference_image_url: str, ip_adapter_weight: float = 0.4
|
16 |
+
) -> Dict[str, Any]:
|
17 |
+
params = {
|
18 |
+
"ip_adapter_weight": ip_adapter_weight,
|
19 |
+
"reference_image_url": reference_image_url,
|
20 |
+
"prompt": prompt,
|
21 |
+
}
|
22 |
+
|
23 |
+
async with self.session.get(self.base_url, params=params) as response:
|
24 |
+
if response.status == 200:
|
25 |
+
return await response.json()
|
26 |
+
else:
|
27 |
+
raise Exception(f"Failed to generate image: {response.status}")
|
28 |
+
|
29 |
+
async def wait_for_image(
|
30 |
+
self, result: Dict[str, Any], max_attempts: int = 60, delay: int = 5
|
31 |
+
) -> Dict[str, Any]:
|
32 |
+
attempts = 0
|
33 |
+
while attempts < max_attempts:
|
34 |
+
if result.get("image", {}).get("url"):
|
35 |
+
return result
|
36 |
+
await asyncio.sleep(delay)
|
37 |
+
attempts += 1
|
38 |
+
|
39 |
+
raise Exception("Image generation timed out")
|
40 |
+
|
41 |
+
async def generate_and_wait(
|
42 |
+
self, prompt: str, reference_image_url: str, ip_adapter_weight: float = 0.4
|
43 |
+
) -> str:
|
44 |
+
result = await self.generate_image(
|
45 |
+
prompt, reference_image_url, ip_adapter_weight
|
46 |
+
)
|
47 |
+
final_result = await self.wait_for_image(result)
|
48 |
+
return final_result["image"]["url"]
|
App/Generate/database/Model.py
CHANGED
@@ -14,6 +14,7 @@ from typing import List
|
|
14 |
from pydantic import BaseModel
|
15 |
import tempfile
|
16 |
import json
|
|
|
17 |
|
18 |
SUPABASE = os.environ.get("SUPABASE", "RANDOM_STRING")
|
19 |
database_url = SUPABASE
|
@@ -259,17 +260,18 @@ class Scene(orm.Model):
|
|
259 |
async def generate_images(self):
|
260 |
self.images = []
|
261 |
async with aiohttp.ClientSession() as session:
|
262 |
-
image_generator =
|
263 |
-
for
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
|
|
273 |
|
274 |
|
275 |
class Transition(orm.Model):
|
|
|
14 |
from pydantic import BaseModel
|
15 |
import tempfile
|
16 |
import json
|
17 |
+
from .Modal import ModalImageGenerator
|
18 |
|
19 |
SUPABASE = os.environ.get("SUPABASE", "RANDOM_STRING")
|
20 |
database_url = SUPABASE
|
|
|
260 |
async def generate_images(self):
|
261 |
self.images = []
|
262 |
async with aiohttp.ClientSession() as session:
|
263 |
+
image_generator = ModalImageGenerator(session)
|
264 |
+
for prompt in self.image_prompts:
|
265 |
+
# You may want to define a method to get a suitable reference image URL
|
266 |
+
reference_image_url = "https://image.lexica.art/full_webp/11caada0-c3c3-4b70-b7a5-d3172c07fc14"
|
267 |
+
try:
|
268 |
+
image_url = await image_generator.generate_and_wait(
|
269 |
+
prompt, reference_image_url
|
270 |
+
)
|
271 |
+
self.images.append(image_url)
|
272 |
+
except Exception as e:
|
273 |
+
print(f"Failed to generate image for prompt '{prompt}': {str(e)}")
|
274 |
+
await asyncio.sleep(1) # Add a small delay between requests
|
275 |
|
276 |
|
277 |
class Transition(orm.Model):
|