Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import random
|
4 |
+
from io import BytesIO
|
5 |
+
from PIL import Image
|
6 |
+
import os
|
7 |
+
import time
|
8 |
+
|
9 |
+
# Ambil base URL dari HF Secret (sembunyikan dari code)
|
10 |
+
BASE_URL = os.environ.get('POLLINATIONS_URL') # Fallback jika secret tidak ada (untuk testing lokal)
|
11 |
+
|
12 |
+
def generate_image(prompt, model):
|
13 |
+
if not prompt:
|
14 |
+
raise gr.Error("Please enter a prompt.")
|
15 |
+
|
16 |
+
# Randomize seed
|
17 |
+
seed = random.randint(1, 999999)
|
18 |
+
|
19 |
+
# Construct full URL (direct to Pollinations)
|
20 |
+
url = f"{BASE_URL}{prompt}?width=2048&height=2048&seed={seed}&nologo=true&model={model}"
|
21 |
+
|
22 |
+
max_retries = 2 # Retry jika error (misal limit)
|
23 |
+
for attempt in range(max_retries):
|
24 |
+
try:
|
25 |
+
response = requests.get(url, stream=True)
|
26 |
+
response.raise_for_status() # Raise error jika bukan 200
|
27 |
+
|
28 |
+
# Convert to PIL Image
|
29 |
+
img = Image.open(BytesIO(response.content))
|
30 |
+
return img
|
31 |
+
|
32 |
+
except requests.exceptions.HTTPError as e:
|
33 |
+
if response.status_code == 500 and 'Access to kontext model' in response.text:
|
34 |
+
if attempt < max_retries - 1:
|
35 |
+
time.sleep(1) # Delay sebelum retry
|
36 |
+
continue
|
37 |
+
raise gr.Error("Access denied for kontext model (limit reached). Try turbo/flux or authenticate at pollinations.ai.")
|
38 |
+
else:
|
39 |
+
raise gr.Error(f"Error: {response.status_code} - {response.text}")
|
40 |
+
except Exception as e:
|
41 |
+
raise gr.Error(f"Unexpected error: {str(e)}")
|
42 |
+
|
43 |
+
# Gradio Interface
|
44 |
+
with gr.Blocks() as demo:
|
45 |
+
gr.Markdown("# Fake flux pro Image Generator")
|
46 |
+
gr.Markdown("The Hugging Face Space https://huggingface.co/spaces/NihalGazi/FLUX-Pro-Unlimited by NihalGazi seems to be a fake version of FLUX Pro. It appears to be using the Pollinations API, because when I tried to replicate it using the same API, the image results were very similar.")
|
47 |
+
|
48 |
+
prompt_input = gr.Textbox(label="Prompt", placeholder="e.g., emma watson")
|
49 |
+
model_input = gr.Dropdown(choices=["kontext", "turbo", "flux"], label="Model", value="turbo") # Default turbo (lebih reliable)
|
50 |
+
|
51 |
+
generate_btn = gr.Button("Generate")
|
52 |
+
output_image = gr.Image(label="Generated Image")
|
53 |
+
|
54 |
+
generate_btn.click(generate_image, inputs=[prompt_input, model_input], outputs=output_image)
|
55 |
+
|
56 |
+
demo.launch()
|