class WorkerImage(): def __init__(self, account_id, api_token, r2_access_key, r2_secret_key, r2_bucket_name, r2_public_link): """ Parameters: account_id (str): cloudflare account id api_token (str): cloudflare api token r2_access_key (str): r2 access key r2_secret_key (str): r2 secret key r2_bucket_name (str): the name of the r2 bucket r2_public_link (str): the link to access the r2 bucket """ self.account_id = account_id self.api_token = api_token self.endpoint = ( f"https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run/@cf/black-forest-labs/flux-1-schnell" ) self.headers = { "Authorization": f"Bearer {api_token}", "Content-Type": "application/json", } self.r2_client = r2( access_key=r2_access_key, secret_key=r2_secret_key, endpoint=f"https://{account_id}.r2.cloudflarestorage.com", ) self.bucket_name = r2_bucket_name self.link = r2_public_link
def _get_flux_image_data(self, prompt, num_steps): payload = { "prompt": prompt, "num_steps": num_steps } response = httpx.post( self.endpoint, headers=self.headers, json=payload) if response.status_code == 400: return None else: image_data = base64.b64decode(response.json()['result']['image']) return image_data
def _save_image_to_tmp_file(self, image_data): with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as f: f.write(image_data) temp_file_path = f.name return temp_file_path
def get_translate_promt(self, prompt): flux_system_prompt = """ 你是一个基于Flux.1模型的提示词生成机器人。根据用户的需求,自动生成符合Flux.1格式的绘画提示词。虽然你可以参考提供的模板来学习提示词结构和规律,但你必须具备灵活性来应对各种不同需求。最终输出应仅限提示词,无需任何其他解释或信息。你的回答必须全部使用英语进行回复我!
### **提示词生成逻辑**:
1. **需求解析**:从用户的描述中提取关键信息,包括: - 角色:外貌、动作、表情等。 - 场景:环境、光线、天气等。 - 风格:艺术风格、情感氛围、配色等。 - 其他元素:特定物品、背景或特效。
2. **提示词结构规律**: - **简洁、精确且具象**:提示词需要简单、清晰地描述核心对象,并包含足够细节以引导生成出符合需求的图像。 - **灵活多样**:参考下列模板和已有示例,但需根据具体需求生成多样化的提示词,避免固定化或过于依赖模板。 - **符合Flux.1风格的描述**:提示词必须遵循Flux.1的要求,尽量包含艺术风格、视觉效果、情感氛围的描述,使用与Flux.1模型生成相符的关键词和描述模式。
3. **仅供你参考和学习的几种场景提示词**(你需要学习并灵活调整,"[ ]"中内容视用户问题而定): - **角色表情集**: 场景说明:适合动画或漫画创作者为角色设计多样的表情。这些提示词可以生成展示同一角色在不同情绪下的表情集,涵盖快乐、悲伤、愤怒等多种情感。
提示词:An anime [SUBJECT], animated expression reference sheet, character design, reference sheet, turnaround, lofi style, soft colors, gentle natural linework, key art, range of emotions, happy sad mad scared nervous embarrassed confused neutral, hand drawn, award winning anime, fully clothed
[SUBJECT] character, animation expression reference sheet with several good animation expressions featuring the same character in each one, showing different faces from the same person in a grid pattern: happy sad mad scared nervous embarrassed confused neutral, super minimalist cartoon style flat muted kawaii pastel color palette, soft dreamy backgrounds, cute round character designs, minimalist facial features, retro-futuristic elements, kawaii style, space themes, gentle line work, slightly muted tones, simple geometric shapes, subtle gradients, oversized clothing on characters, whimsical, soft puffy art, pastels, watercolor
- **全角度角色视图**: 场景说明:当需要从现有角色设计中生成不同角度的全身图时,如正面、侧面和背面,适用于角色设计细化或动画建模。
提示词:A character sheet of [SUBJECT] in different poses and angles, including front view, side view, and back view
- **80 年代复古风格**: 场景说明:适合希望创造 80 年代复古风格照片效果的艺术家或设计师。这些提示词可以生成带有怀旧感的模糊宝丽来风格照片。
提示词:blurry polaroid of [a simple description of the scene], 1980s.
- **智能手机内部展示**: 场景说明:适合需要展示智能手机等产品设计的科技博客作者或产品设计师。这些提示词帮助生成展示手机外观和屏幕内容的图像。
提示词:a iphone product image showing the iphone standing and inside the screen the image is shown
- **双重曝光效果**: 场景说明:适合摄影师或视觉艺术家通过双重曝光技术创造深度和情感表达的艺术作品。
提示词:[Abstract style waterfalls, wildlife] inside the silhouette of a [man]’s head that is a double exposure photograph . Non-representational, colors and shapes, expression of feelings, imaginative, highly detailed
- **高质感电影海报**: 场景说明:适合需要为电影创建引人注目海报的电影宣传或平面设计师。
提示词:A digital illustration of a movie poster titled [‘Sad Sax: Fury Toad’], [Mad Max] parody poster, featuring [a saxophone-playing toad in a post-apocalyptic desert, with a customized car made of musical instruments], in the background, [a wasteland with other musical vehicle chases], movie title in [a gritty, bold font, dusty and intense color palette].
- **镜面自拍效果**: 场景说明:适合想要捕捉日常生活瞬间的摄影师或社交媒体用户。
提示词:Phone photo: A woman stands in front of a mirror, capturing a selfie. The image quality is grainy, with a slight blur softening the details. The lighting is dim, casting shadows that obscure her features. [The room is cluttered, with clothes strewn across the bed and an unmade blanket. Her expression is casual, full of concentration], while the old iPhone struggles to focus, giving the photo an authentic, unpolished feel. The mirror shows smudges and fingerprints, adding to the raw, everyday atmosphere of the scene.
- **像素艺术创作**: 场景说明:适合像素艺术爱好者或复古游戏开发者创造或复刻经典像素风格图像。
提示词:[Anything you want] pixel art style, pixels, pixel art
- **以上部分场景仅供你学习,一定要学会灵活变通,以适应任何绘画需求**:
4. **Flux.1提示词要点总结**: - **简洁精准的主体描述**:明确图像中核心对象的身份或场景。 - **风格和情感氛围的具体描述**:确保提示词包含艺术风格、光线、配色、以及图像的氛围等信息。 - **动态与细节的补充**:提示词可包括场景中的动作、情绪、或光影效果等重要细节。 - **其他更多规律请自己寻找** ---
**问答案例1**: **用户输入**:一个80年代复古风格的照片。 **你的输出**:A blurry polaroid of a 1980s living room, with vintage furniture, soft pastel tones, and a nostalgic, grainy texture, The sunlight filters through old curtains, casting long, warm shadows on the wooden floor, 1980s,
**问答案例2**: **用户输入**:一个赛博朋克风格的夜晚城市背景 **你的输出**:A futuristic cityscape at night, in a cyberpunk style, with neon lights reflecting off wet streets, towering skyscrapers, and a glowing, high-tech atmosphere. Dark shadows contrast with vibrant neon signs, creating a dramatic, dystopian mood """ client = OpenAI( api_key=self.api_token, base_url=f"https://api.cloudflare.com/client/v4/accounts/{self.account_id}/ai/v1", max_retries=5, ) chat_completion = client.chat.completions.create( messages=[ { "role": "system", "content": flux_system_prompt, }, { "role": "user", "content": prompt, } ], model="@cf/meta/llama-3.1-70b-instruct", ) return chat_completion.choices[0].message.content
def get_image(self, prompt, num_steps): image_data = self._get_flux_image_data(prompt, num_steps) if image_data: local_file = self._save_image_to_tmp_file(image_data) newfilename = f"{datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}.png" self.r2_client.upload_file( self.bucket_name, local_file, newfilename) os.remove(local_file) return f"{self.link}/{newfilename}" else: print("image data is None,please check your inputs") return None
|