import os import gradio as gr from huggingface_hub import InferenceClient import itertools import threading import time import json import datetime # ========================================== # 1. HARD-CODED CONFIGURATION & PRESETS # ========================================== SECRET_API_KEY = os.getenv("APP_PASSWORD") raw_tokens = [os.getenv(f"HF_TOKEN_{i}") for i in range(1, 11)] raw_tokens.append(os.getenv("HF_TOKEN")) valid_tokens = [t for t in raw_tokens if t is not None and t.strip() != ""] clients = [InferenceClient("stabilityai/stable-diffusion-xl-base-1.0", token=t) for t in valid_tokens] if not clients: print("⚠️ WARNING: Space Settings में कोई HF_TOKEN नहीं मिला! ऐप काम नहीं करेगा।") client_pool = itertools.cycle(clients) if clients else None lock = threading.Lock() # ✨✨✨ LOCKED PRESETS (हार्ड-कोडेड) ✨✨✨ LOCKED_SEED = 83492751 STYLE_SUFFIX = ", a masterfully detailed oil painting, rich expressionist brushwork, visible oil textures, rich colors, cinematic lighting, masterpiece, 8k resolution, in the style of Rembrandt and Van Gogh" DEFAULT_NEGATIVE_PROMPT = "blurry, text, watermark, bad anatomy, deformed, amateur, low quality, photorealistic, photography" # ========================================== # 2. CORE LOGIC (RATIO + STYLE LOCK + FAILOVER) # ========================================== def generate_image(prompt, user_negative_prompt, ratio, api_key): if api_key != SECRET_API_KEY: raise gr.Error("❌ Access Denied: Invalid API Password!") if not client_pool: raise gr.Error("❌ Server Error: API Tokens missing.") if ratio == "YouTube Video (16:9)": w, h = 1024, 576 else: w, h = 576, 1024 # प्रॉम्प्ट को हार्ड-कोडेड स्टाइल के साथ बदलना cleaned_prompt = prompt.strip() if not cleaned_prompt: raise gr.Error("❌ Empty Prompt!") final_prompt = f"{cleaned_prompt}{STYLE_SUFFIX}" if user_negative_prompt.strip(): final_negative_prompt = f"{DEFAULT_NEGATIVE_PROMPT}, {user_negative_prompt.strip()}" else: final_negative_prompt = DEFAULT_NEGATIVE_PROMPT attempts = len(clients) last_error = "" start_time = time.time() for attempt in range(attempts): with lock: current_client = next(client_pool) try: timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") print(f"[{timestamp}] 🚀 Attempt {attempt + 1}/{attempts} - Generating image...") image = current_client.text_to_image( final_prompt, negative_prompt=final_negative_prompt, width=w, height=h, seed=LOCKED_SEED, # सीड लॉक है! guidance_scale=7.5, num_inference_steps=40 ) duration = round(time.time() - start_time, 2) log_data = { "timestamp": timestamp, "status": "Success", "ratio": ratio, "duration": duration, "token_index": valid_tokens.index(current_client.token), "prompt_snippet": cleaned_prompt[:30] + "..." } return image, log_data except Exception as e: last_error = str(e) print(f"⚠️ Token failed. Switching... Error: {last_error[:50]}") continue raise gr.Error(f"⚠️ All tokens exhausted. Please try again later. Details: {last_error}") # ========================================== # 3. STUDIO UI & FULL API DOCUMENTATION # ========================================== with gr.Blocks(theme=gr.themes.Soft(primary_hue="amber"), title="Sparkling Gyan API Pro v3") as app: gr.Markdown("""
Pro-Grade AI Generator with LOCKED Distributed Token Engine & MASTER Oil Painting Style
Style: Masterful Oil Painting (Rembrandt/Van Gogh Style)
Seed: {LOCKED_SEED} (Always Locked)
Quality: Masterpiece, 8k Resolution