UrFavB0i commited on
Commit
facc56d
·
1 Parent(s): b6b2d99

refactor: chnage in UI component

Browse files
Files changed (1) hide show
  1. app.py +118 -67
app.py CHANGED
@@ -1,91 +1,142 @@
 
1
  import os
2
 
3
  import gradio as gr
4
  import requests
5
 
6
 
7
- def generate(images, model_choice):
8
- outputs = []
 
 
 
 
 
 
 
 
 
9
 
10
  for img in images:
11
- img_path = img.name if hasattr(img, "name") else img # handle temp file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- if model_choice == "Flux Kontext Pro":
14
- response = requests.post(
15
- "https://api.fluxkontext.pro/v1/generate",
16
- headers={"Authorization": f"Bearer {os.getenv('FLUX_API_KEY')}"},
17
- files={"image": open(img_path, "rb")},
18
- )
19
- result = response.json().get("result_url")
20
-
21
- elif model_choice == "Wan 2.5":
22
- response = requests.post(
23
- "https://dashscope.aliyuncs.com/api/v1/services/aigc/image-generation",
24
- headers={
25
- "Authorization": f"Bearer {os.getenv('WAN_API_KEY')}",
26
- "Content-Type": "application/json",
27
- },
28
- json={"model": "wan2.5-t2v-preview", "input": {"image": img_path}},
29
- )
30
- result = response.json().get("output", {}).get("url")
31
 
32
- elif model_choice == "Qwen 3":
33
- response = requests.post(
34
- "https://qwen-api.aliyun.com/v1/image/generate",
35
- headers={"Authorization": f"Bearer {os.getenv('QWEN_API_KEY')}"},
36
- files={"image": open(img_path, "rb")},
37
- )
38
- result = response.json().get("data", {}).get("image_url")
39
 
40
- elif model_choice == "Nano Banana":
41
- response = requests.post(
42
- "https://api.nanobanana.io/v1/image-gen",
43
- headers={"Authorization": f"Bearer {os.getenv('NANO_API_KEY')}"},
44
- files={"image": open(img_path, "rb")},
45
  )
46
- result = response.json().get("url")
47
 
48
- elif model_choice == "GPT":
49
- response = requests.post(
50
- "https://api.openai.com/v1/images/edits",
51
- headers={"Authorization": f"Bearer {os.getenv('OPENAI_API_KEY')}"},
52
- files={"image": open(img_path, "rb")},
53
  )
54
- result = response.json().get("data", [{}])[0].get("url")
55
-
56
- else:
57
- result = None
58
 
59
- outputs.append(result)
60
-
61
- return outputs
62
-
63
-
64
- with gr.Blocks(theme=gr.themes.Glass()) as demo:
65
- gr.Markdown("## 🔥 Multi-API Image-to-Image Generator")
66
 
67
- with gr.Row(equal_height=True):
68
- # Left Column
69
- with gr.Column(scale=1):
70
- image_input = gr.File(file_count="multiple", label="Upload your images")
71
- model_choice = gr.Dropdown(
72
- [
73
- "Flux Kontext Pro",
74
- "Flux Kontext MAX",
75
- "Wan 2.5",
76
- "Qwen 3",
77
- "Nano Banana",
78
- "GPT",
79
  ],
80
- value="Flux Kontext Pro",
81
- label="Select Model",
82
  )
83
 
84
- # Right Column
 
 
85
  with gr.Column(scale=1):
86
  gallery = gr.Gallery(label="Generated Results", columns=2, height=400)
87
- btn = gr.Button("Generate", variant="primary", size="lg")
88
 
89
- btn.click(generate, [image_input, model_choice], gallery)
 
 
 
 
 
90
 
91
  demo.launch()
 
1
+ import base64
2
  import os
3
 
4
  import gradio as gr
5
  import requests
6
 
7
 
8
+ def generate(
9
+ images,
10
+ prompt,
11
+ size,
12
+ ):
13
+ api_key = os.getenv("BYTEPLUS_API_KEY")
14
+ base_url = os.getenv("BYTEPLUS_URL").rstrip("/")
15
+ response_format = "b64_json"
16
+ watermark = False
17
+
18
+ images_iput = []
19
 
20
  for img in images:
21
+ try:
22
+ with open(img.name, "rb") as f:
23
+ image_bytes = f.read()
24
+ encoded = base64.b64encode(image_bytes).decode("utf-8")
25
+ images_iput.append(encoded)
26
+ except Exception as e:
27
+ print(f"⚠️ Failed to process image {img.name}: {e}")
28
+
29
+ try:
30
+ model_name = "seedream-4-0-250828"
31
+ request_data = {
32
+ "model": model_name,
33
+ "prompt": prompt,
34
+ "response_format": response_format,
35
+ "sequential_image_generation": "disabled",
36
+ "size": size,
37
+ "watermark": watermark,
38
+ }
39
+ except Exception as e:
40
+ print(f"⚠️ Failed to process everything: {e}")
41
+ # if model_choice == "Flux Kontext Pro":
42
+ # response = requests.post(
43
+ # "https://api.fluxkontext.pro/v1/generate",
44
+ # headers={"Authorization": f"Bearer {os.getenv('FLUX_API_KEY')}"},
45
+ # files={"image": open(img_path, "rb")},
46
+ # )
47
+ # result = response.json().get("result_url")
48
+
49
+ # elif model_choice == "Wan 2.5":
50
+ # response = requests.post(
51
+ # "https://dashscope.aliyuncs.com/api/v1/services/aigc/image-generation",
52
+ # headers={
53
+ # "Authorization": f"Bearer {os.getenv('WAN_API_KEY')}",
54
+ # "Content-Type": "application/json",
55
+ # },
56
+ # json={"model": "wan2.5-t2v-preview", "input": {"image": img_path}},
57
+ # )
58
+ # result = response.json().get("output", {}).get("url")
59
+
60
+ # elif model_choice == "Qwen 3":
61
+ # response = requests.post(
62
+ # "https://qwen-api.aliyun.com/v1/image/generate",
63
+ # headers={"Authorization": f"Bearer {os.getenv('QWEN_API_KEY')}"},
64
+ # files={"image": open(img_path, "rb")},
65
+ # )
66
+ # result = response.json().get("data", {}).get("image_url")
67
+
68
+ # elif model_choice == "Nano Banana":
69
+ # response = requests.post(
70
+ # "https://api.nanobanana.io/v1/image-gen",
71
+ # headers={"Authorization": f"Bearer {os.getenv('NANO_API_KEY')}"},
72
+ # files={"image": open(img_path, "rb")},
73
+ # )
74
+ # result = response.json().get("url")
75
+
76
+ # elif model_choice == "GPT":
77
+ # response = requests.post(
78
+ # "https://api.openai.com/v1/images/edits",
79
+ # headers={"Authorization": f"Bearer {os.getenv('OPENAI_API_KEY')}"},
80
+ # files={"image": open(img_path, "rb")},
81
+ # )
82
+ # result = response.json().get("data", [{}])[0].get("url")
83
+
84
+ # else:
85
+ # result = None
86
+
87
+ # outputs.append(result)
88
+
89
+ return
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
+ with gr.Blocks(theme=gr.themes.Glass()) as demo:
93
+ gr.Markdown("## 🔥 Multi-API Image-to-Image Generator")
 
 
 
 
 
94
 
95
+ with gr.Row(equal_height=True):
96
+ # Left Column — inputs
97
+ with gr.Column(scale=1):
98
+ image_input = gr.File(
99
+ file_count="multiple", label="Upload your reference images (optional)"
100
  )
 
101
 
102
+ prompt_input = gr.Textbox(
103
+ label="Prompt",
104
+ placeholder="Describe how you want to modify or generate the image...",
105
+ lines=2,
 
106
  )
 
 
 
 
107
 
108
+ variation_choice = gr.Dropdown(
109
+ choices=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
110
+ label="Model / Variation",
111
+ value=1,
112
+ )
 
 
113
 
114
+ size_choice = gr.Dropdown(
115
+ choices=[
116
+ "2048x2048",
117
+ "1728x2304",
118
+ "2304x1728",
119
+ "2560x1440",
120
+ "1440x2560",
121
+ "1664x2496",
122
+ "2496x1664",
123
+ "3024x1296",
 
 
124
  ],
125
+ label="Output Size",
126
+ value="2048x2048",
127
  )
128
 
129
+ btn = gr.Button("🚀 Generate", variant="primary", size="lg")
130
+
131
+ # Right Column — outputs
132
  with gr.Column(scale=1):
133
  gallery = gr.Gallery(label="Generated Results", columns=2, height=400)
 
134
 
135
+ # Connect button click to function
136
+ btn.click(
137
+ fn=generate,
138
+ inputs=[image_input, prompt_input, variation_choice, size_choice],
139
+ outputs=gallery,
140
+ )
141
 
142
  demo.launch()