ritwik098 commited on
Commit
e1012e2
·
verified ·
1 Parent(s): 8c379d4

Create utils/image.py

Browse files
Files changed (1) hide show
  1. utils/image.py +83 -0
utils/image.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ import os
3
+ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
4
+ from configs.config import Config
5
+
6
+ import base64, json
7
+ from openai import OpenAI
8
+ from datetime import datetime
9
+
10
+ client = OpenAI(
11
+ base_url="https://api.studio.nebius.com/v1/",
12
+ api_key=Config.nebius_api,
13
+ )
14
+
15
+ def generate_image_data(prompt: str, width: int = 1024, height: int = 1024, steps: int = 4, seed: int = -1, negative_prompt: str = "") -> dict:
16
+ """
17
+ Generates an image using the Nebius Studio model.
18
+
19
+ Args:
20
+ prompt (str): The prompt for image generation.
21
+ width (int, optional): Width of the image. Default is 1024.
22
+ height (int, optional): Height of the image. Default is 1024.
23
+ steps (int, optional): Number of inference steps. Default is 4.
24
+ seed (int, optional): Random seed. Default is -1 (random).
25
+ negative_prompt (str, optional): Negative prompt to avoid unwanted features.
26
+
27
+ Returns:
28
+ dict: JSON response from the API, including base64 image.
29
+ """
30
+ response = client.images.generate(
31
+ model="black-forest-labs/flux-schnell",
32
+ response_format="b64_json",
33
+ extra_body={
34
+ "response_extension": "png",
35
+ "width": width,
36
+ "height": height,
37
+ "num_inference_steps": steps,
38
+ "negative_prompt": negative_prompt,
39
+ "seed": seed
40
+ },
41
+ prompt=prompt
42
+ )
43
+ return response.to_dict()
44
+
45
+ def save_image_from_b64(image_data: str, output_folder: str = "image") -> str:
46
+ """
47
+ Decodes base64 image data and saves it as a PNG file.
48
+
49
+ Args:
50
+ image_data (str): Base64 encoded image string.
51
+ output_folder (str): Folder where the image will be saved.
52
+
53
+ Returns:
54
+ str: Path to the saved image file.
55
+ """
56
+ # Create the output directory if it doesn't exist
57
+ os.makedirs(output_folder, exist_ok=True)
58
+
59
+ # Generate unique filename
60
+ filename = datetime.now().strftime("%Y%m%d_%H%M%S") + ".png"
61
+ file_path = os.path.join(output_folder, filename)
62
+
63
+ # Decode and save image
64
+ with open(file_path, "wb") as f:
65
+ f.write(base64.b64decode(image_data))
66
+
67
+ return file_path
68
+
69
+ def generate_images(prompt: str):
70
+ """
71
+ Generate images based on the script using Nebius Studio.
72
+ This is a placeholder function that simulates image generation.
73
+ """
74
+ # For simplicity, we just return a mock image path
75
+ image_data = generate_image_data(prompt)
76
+
77
+ if "Error" in image_data:
78
+ return {"error": image_data}
79
+
80
+ # Save the generated image from base64 data
81
+ image_path = save_image_from_b64(image_data['data'][0]['b64_json'])
82
+
83
+ return {"image_path": image_path}