Spaces:
Paused
Paused
import os | |
import logging | |
import numpy as np | |
import cv2 | |
from dotenv import load_dotenv | |
from huggingface_hub import InferenceClient, HfFolder | |
from PIL import Image | |
load_dotenv() | |
# Acquire token | |
hf_token = os.getenv("HUGGING_FACE_HUB_TOKEN") or HfFolder.get_token() | |
if not hf_token: | |
print("❌ No Hugging Face token found. Signature generation will fail.") | |
CLIENT = None | |
else: | |
print("✅ Using Hugging Face token for signature generation.") | |
CLIENT = InferenceClient(token=hf_token) | |
MODEL_ID = "stabilityai/stable-diffusion-xl-base-1.0" | |
def generate_signatures(name: str, num_variations: int = 3) -> list: | |
"""Generates multiple signature variations for a given name.""" | |
if CLIENT is None: | |
return [] | |
prompts = [ | |
f"A clean, elegant, handwritten signature of the name '{name}' on a plain white background. Cursive, professional.", | |
f"Calligraphy signature of '{name}'. Black ink on white paper. Minimalist, artistic.", | |
f"A doctor's signature for '{name}'. Quick, scribbled, but legible. Official-looking script." | |
] | |
images = [] | |
for i in range(num_variations): | |
prompt = prompts[i % len(prompts)] | |
logging.info(f"Generating signature variation {i+1} for '{name}'…") | |
try: | |
pil_img = CLIENT.text_to_image( | |
prompt, | |
model=MODEL_ID, | |
negative_prompt=( | |
"photograph, text, multiple signatures, watermark, blurry, colorful, background" | |
), | |
guidance_scale=8.0 | |
) | |
np_img = np.array(pil_img) | |
cv_img = cv2.cvtColor(np_img, cv2.COLOR_RGB2BGR) | |
images.append(cv_img) | |
except Exception as e: | |
logging.error(f"❌ Signature generation failed: {e}") | |
return images | |