aura77 commited on
Commit
c4d1a9d
·
verified ·
1 Parent(s): 2d696d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -30
app.py CHANGED
@@ -9,7 +9,7 @@ from PIL import Image
9
  from gradio_client import Client
10
 
11
  # ==========================================
12
- # 1. API Keys
13
  # ==========================================
14
  KEYS = {
15
  "deepai": "16e69995-00d4-4ffd-993c-a026632f6de6",
@@ -23,7 +23,10 @@ KEYS = {
23
  "modelslab": "42zL3I6pH7Mv94jhNf4RGyuKGGT3GoEhfXZv47WwmQsfyhgC8XNttfOpvMKL",
24
  "openrouter": "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",
25
  "omniinfer": "sk_fTeDYbT2PibvuC27eGFbeE4E-7wcCuWp4SVgFDFWHDU",
26
- "fireworks": "key_kH0svuL0"
 
 
 
27
  }
28
 
29
  # ==========================================
@@ -86,7 +89,8 @@ def generate_pollinations(p, w, h, ar):
86
  raise Exception("Error")
87
 
88
  # ==========================================
89
- # 3. HF & Slow API Functions (SLOW)
 
90
  # ==========================================
91
  def generate_stable_horde(p, w, h, ar):
92
  res = requests.post("https://stablehorde.net/api/v2/generate/async", headers={"apikey": KEYS['stablehorde']}, json={"prompt": p, "params": {"n": 1, "width": 512, "height": 512}})
@@ -98,21 +102,36 @@ def generate_stable_horde(p, w, h, ar):
98
  if check.status_code == 200 and check.json().get("done", False): return get_image_from_url(check.json()['generations'][0]['img'])
99
  raise Exception("Error")
100
 
101
- def generate_hf_space(client_name, api_name, p):
102
- try:
103
- client = Client(client_name)
104
- result = client.predict(p, api_name=api_name)
105
- return Image.open(result[0] if isinstance(result, (list, tuple)) else result)
106
- except:
107
- raise Exception("Error")
 
 
 
 
 
 
 
108
 
109
  def generate_hf_api(prompt, model_id, w, h):
110
- # এই ফাংশনে সাইজ (width, height) ফিক্স করে দেওয়া হয়েছে!
111
  url = f"https://api-inference.huggingface.co/models/{model_id}"
 
 
 
112
  payload = {"inputs": prompt, "parameters": {"width": w, "height": h}}
113
- res = requests.post(url, json=payload)
114
- if res.status_code == 200: return Image.open(BytesIO(res.content))
115
- raise Exception("Error")
 
 
 
 
 
 
116
 
117
  # ==========================================
118
  # 4. API Categorization
@@ -132,29 +151,23 @@ FAST_APIS = {
132
 
133
  SLOW_APIS = {
134
  "Stable Horde (Crowdsourced)": generate_stable_horde,
135
- "HF Space: FLUX.1": lambda p, w, h, ar: generate_hf_space("black-forest-labs/FLUX.1-schnell", "/infer", p),
136
- "HF Space: SDXL Light": lambda p, w, h, ar: generate_hf_space("ByteDance/SDXL-Lightning", "/generate_image", p),
 
137
 
138
- # আগের ৪টি HF API
139
  "HF API: OpenJourney": lambda p, w, h, ar: generate_hf_api(p, "prompthero/openjourney", w, h),
140
  "HF API: RealVisXL": lambda p, w, h, ar: generate_hf_api(p, "SG161222/RealVisXL_V4.0", w, h),
141
  "HF API: Animagine XL": lambda p, w, h, ar: generate_hf_api(p, "cagliostrolab/animagine-xl-3.1", w, h),
142
  "HF API: Playground v2.5": lambda p, w, h, ar: generate_hf_api(p, "playgroundai/playground-v2.5-1024px-aesthetic", w, h),
143
-
144
- # তোমার চাওয়া নতুন ১০টি HF API!
145
  "HF API: DreamShaper XL": lambda p, w, h, ar: generate_hf_api(p, "Lykon/dreamshaper-xl-1.0", w, h),
146
  "HF API: OpenDalle V1.1": lambda p, w, h, ar: generate_hf_api(p, "dataautogpt3/OpenDalleV1.1", w, h),
147
  "HF API: Anything V5 (Anime)": lambda p, w, h, ar: generate_hf_api(p, "stablediffusionapi/anything-v5", w, h),
148
- "HF API: Disney Pixar Cartoon": lambda p, w, h, ar: generate_hf_api(p, "stablediffusionapi/disney-pixar-cartoon", w, h),
149
  "HF API: CyberRealistic": lambda p, w, h, ar: generate_hf_api(p, "stablediffusionapi/cyberrealistic", w, h),
150
- "HF API: Absolute Reality": lambda p, w, h, ar: generate_hf_api(p, "stablediffusionapi/absolute-reality-v181", w, h),
151
- "HF API: Juggernaut XL": lambda p, w, h, ar: generate_hf_api(p, "stablediffusionapi/juggernaut-xl-v5", w, h),
152
- "HF API: SDXL Base 1.0": lambda p, w, h, ar: generate_hf_api(p, "stabilityai/stable-diffusion-xl-base-1.0", w, h),
153
- "HF API: Realistic Vision V5": lambda p, w, h, ar: generate_hf_api(p, "SG161222/Realistic_Vision_V5.1_noVAE", w, h),
154
- "HF API: Stable Diffusion 1.5": lambda p, w, h, ar: generate_hf_api(p, "runwayml/stable-diffusion-v1-5", w, h)
155
  }
156
 
157
- # সব মিলিয়ে একটি মাস্টার ডিকশনারি (ম্যানুয়াল সিলেকশনের জন্য)
158
  ALL_APIS = {**FAST_APIS, **SLOW_APIS}
159
 
160
  # ==========================================
@@ -177,7 +190,6 @@ def generate_image_logic(prompt, style, size, speed_mode, selected_api):
177
  }
178
  enhanced_prompt = f"{prompt.strip()}{style_modifiers.get(style, '')}, aspect ratio {ar}"
179
 
180
- # লজিক ১: অটোমেটিক (Fast বা Slow মোড অনুযায়ী)
181
  if selected_api == "অটোমেটিক (Auto Fallback)":
182
  target_apis = FAST_APIS if speed_mode == "Fast (ফাস্ট)" else SLOW_APIS
183
  yield None, f"⏳ {speed_mode} মোডে শুরু হচ্ছে..."
@@ -194,7 +206,6 @@ def generate_image_logic(prompt, style, size, speed_mode, selected_api):
194
  continue
195
  yield None, "🚫 **দুঃখিত! এই মোডের সবগুলো সার্ভার বর্তমানে ব্যস্ত।**"
196
 
197
- # লজিক ২: ম্যানুয়ালি নির্দিষ্ট API সিলেক্ট করলে
198
  else:
199
  yield None, f"⏳ শুধুমাত্র **{selected_api}**-এ কল করা হচ্ছে..."
200
  api_func = ALL_APIS[selected_api]
@@ -211,7 +222,7 @@ def generate_image_logic(prompt, style, size, speed_mode, selected_api):
211
  api_choices = ["অটোমেটিক (Auto Fallback)"] + list(ALL_APIS.keys())
212
 
213
  with gr.Blocks(theme=gr.themes.Base()) as demo:
214
- gr.Markdown("<h1 style='text-align: center;'>🎨 Pro AI Image Generator (28 Servers)</h1>")
215
 
216
  with gr.Row():
217
  with gr.Column(scale=1):
@@ -227,7 +238,6 @@ with gr.Blocks(theme=gr.themes.Base()) as demo:
227
  label="ছবির সাইজ (Size)", value="স্কয়ার (1:1) - Instagram"
228
  )
229
 
230
- # নতুন Speed Mode অপশন
231
  speed_mode = gr.Radio(
232
  choices=["Fast (ফাস্ট)", "Slow (স্লো) - High Quality"],
233
  label="স্পিড মোড (অটোমেটিকের জন্য)", value="Fast (ফাস্ট)"
 
9
  from gradio_client import Client
10
 
11
  # ==========================================
12
+ # 1. API Keys (এখানে একটি নতুন অপশন যুক্ত হয়েছে)
13
  # ==========================================
14
  KEYS = {
15
  "deepai": "16e69995-00d4-4ffd-993c-a026632f6de6",
 
23
  "modelslab": "42zL3I6pH7Mv94jhNf4RGyuKGGT3GoEhfXZv47WwmQsfyhgC8XNttfOpvMKL",
24
  "openrouter": "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",
25
  "omniinfer": "sk_fTeDYbT2PibvuC27eGFbeE4E-7wcCuWp4SVgFDFWHDU",
26
+ "fireworks": "key_kH0svuL0",
27
+
28
+ # ⚠️ নিচে তোমার Hugging Face Token টি দিতে হবে! না দিলে Slow API গুলো কাজ করবে না।
29
+ "huggingface": "hf_xxxxxxxxxxxxxxxxxxxxxxxxx"
30
  }
31
 
32
  # ==========================================
 
89
  raise Exception("Error")
90
 
91
  # ==========================================
92
+ # 3. HF & Slow API Functions (SLOW)
93
+ # (এখানে প্যারামিটারগুলো ফিক্স করা হয়েছে)
94
  # ==========================================
95
  def generate_stable_horde(p, w, h, ar):
96
  res = requests.post("https://stablehorde.net/api/v2/generate/async", headers={"apikey": KEYS['stablehorde']}, json={"prompt": p, "params": {"n": 1, "width": 512, "height": 512}})
 
102
  if check.status_code == 200 and check.json().get("done", False): return get_image_from_url(check.json()['generations'][0]['img'])
103
  raise Exception("Error")
104
 
105
+ def generate_hf_flux(p, w, h, ar):
106
+ client = Client("black-forest-labs/FLUX.1-schnell")
107
+ res = client.predict(prompt=p, seed=0, randomize_seed=True, width=w, height=h, num_inference_steps=4, api_name="/infer")
108
+ return Image.open(res[0])
109
+
110
+ def generate_hf_sdxl_lightning(p, w, h, ar):
111
+ client = Client("ByteDance/SDXL-Lightning")
112
+ res = client.predict(p, "4-Step", api_name="/generate_image")
113
+ return Image.open(res)
114
+
115
+ def generate_hf_sd3(p, w, h, ar):
116
+ client = Client("stabilityai/stable-diffusion-3.5-large")
117
+ res = client.predict(prompt=p, negative_prompt="", seed=0, randomize_seed=True, width=w, height=h, guidance_scale=4.5, num_inference_steps=40, api_name="/infer")
118
+ return Image.open(res[0])
119
 
120
  def generate_hf_api(prompt, model_id, w, h):
 
121
  url = f"https://api-inference.huggingface.co/models/{model_id}"
122
+ headers = {"Authorization": f"Bearer {KEYS['huggingface']}"} if KEYS.get('huggingface') and KEYS['huggingface'] != "hf_xxxxxxxxxxxxxxxxxxxxxxxxx" else {}
123
+
124
+ # প্রথমে সাইজ দিয়ে ট্রাই করবে
125
  payload = {"inputs": prompt, "parameters": {"width": w, "height": h}}
126
+ res = requests.post(url, headers=headers, json=payload)
127
+
128
+ # অনেক HF মডেল সাইজ রিকোয়েস্ট সাপোর্ট করে না বলে ক্র্যাশ করে, তাই ফেইল করলে সাইজ ছাড়া ট্রাই করবে
129
+ if res.status_code != 200:
130
+ res = requests.post(url, headers=headers, json={"inputs": prompt})
131
+
132
+ if res.status_code == 200:
133
+ return Image.open(BytesIO(res.content))
134
+ raise Exception(f"HF Error: {res.status_code}")
135
 
136
  # ==========================================
137
  # 4. API Categorization
 
151
 
152
  SLOW_APIS = {
153
  "Stable Horde (Crowdsourced)": generate_stable_horde,
154
+ "HF Space: FLUX.1": generate_hf_flux,
155
+ "HF Space: SDXL Light": generate_hf_sdxl_lightning,
156
+ "HF Space: SD3.5": generate_hf_sd3,
157
 
158
+ # 10 HF APIs
159
  "HF API: OpenJourney": lambda p, w, h, ar: generate_hf_api(p, "prompthero/openjourney", w, h),
160
  "HF API: RealVisXL": lambda p, w, h, ar: generate_hf_api(p, "SG161222/RealVisXL_V4.0", w, h),
161
  "HF API: Animagine XL": lambda p, w, h, ar: generate_hf_api(p, "cagliostrolab/animagine-xl-3.1", w, h),
162
  "HF API: Playground v2.5": lambda p, w, h, ar: generate_hf_api(p, "playgroundai/playground-v2.5-1024px-aesthetic", w, h),
 
 
163
  "HF API: DreamShaper XL": lambda p, w, h, ar: generate_hf_api(p, "Lykon/dreamshaper-xl-1.0", w, h),
164
  "HF API: OpenDalle V1.1": lambda p, w, h, ar: generate_hf_api(p, "dataautogpt3/OpenDalleV1.1", w, h),
165
  "HF API: Anything V5 (Anime)": lambda p, w, h, ar: generate_hf_api(p, "stablediffusionapi/anything-v5", w, h),
166
+ "HF API: Disney Pixar": lambda p, w, h, ar: generate_hf_api(p, "stablediffusionapi/disney-pixar-cartoon", w, h),
167
  "HF API: CyberRealistic": lambda p, w, h, ar: generate_hf_api(p, "stablediffusionapi/cyberrealistic", w, h),
168
+ "HF API: Realistic Vision": lambda p, w, h, ar: generate_hf_api(p, "SG161222/Realistic_Vision_V5.1_noVAE", w, h)
 
 
 
 
169
  }
170
 
 
171
  ALL_APIS = {**FAST_APIS, **SLOW_APIS}
172
 
173
  # ==========================================
 
190
  }
191
  enhanced_prompt = f"{prompt.strip()}{style_modifiers.get(style, '')}, aspect ratio {ar}"
192
 
 
193
  if selected_api == "অটোমেটিক (Auto Fallback)":
194
  target_apis = FAST_APIS if speed_mode == "Fast (ফাস্ট)" else SLOW_APIS
195
  yield None, f"⏳ {speed_mode} মোডে শুরু হচ্ছে..."
 
206
  continue
207
  yield None, "🚫 **দুঃখিত! এই মোডের সবগুলো সার্ভার বর্তমানে ব্যস্ত।**"
208
 
 
209
  else:
210
  yield None, f"⏳ শুধুমাত্র **{selected_api}**-এ কল করা হচ্ছে..."
211
  api_func = ALL_APIS[selected_api]
 
222
  api_choices = ["অটোমেটিক (Auto Fallback)"] + list(ALL_APIS.keys())
223
 
224
  with gr.Blocks(theme=gr.themes.Base()) as demo:
225
+ gr.Markdown("<h1 style='text-align: center;'>🎨 Pro AI Image Generator (24 Servers)</h1>")
226
 
227
  with gr.Row():
228
  with gr.Column(scale=1):
 
238
  label="ছবির সাইজ (Size)", value="স্কয়ার (1:1) - Instagram"
239
  )
240
 
 
241
  speed_mode = gr.Radio(
242
  choices=["Fast (ফাস্ট)", "Slow (স্লো) - High Quality"],
243
  label="স্পিড মোড (অটোমেটিকের জন্য)", value="Fast (ফাস্ট)"