Flux.1 是市场上最新的文本到图像模型,由 Black Forest Labs 为我们带来。它是一种最先进的模型,可以从文本描述生成高质量图像,处理复杂的描述并生成具有精细细节的高质量图像。
Flux.1 背后是谁?
Flux.1 由 Black Forest Labs 开发,该公司由 Stability AI 的一群前员工创建。
它是如何运作的?
与其他扩散模型(例如通过从随机起点逐渐消除噪声来创建图像的稳定扩散)不同,Flux.1 使用一种称为“流匹配”的技术生成图像,该技术采用更直接的方法,学习所需的精确变换将噪声转换为真实的图像。与常见的扩散模型相比,这可以更快地生成高质量图像,并且步骤更少。
此外,通过这种不同的方法,Flux.1 可以处理其中包含文本的图像,如下所示:
现代时尚笔记本电脑的逼真图像,打开的网页以干净、简约的设计显示文本“codestackme”。笔记本电脑应放置在光线柔和的白色桌子上,突出屏幕的光芒和金属外壳上的微妙反射。整体氛围应该专业、吸引人,传达出创新和科技进步的感觉。
如何为 Flux.1 编写一个好的提示?
Flux.1 的突出特点之一是其用户友好的提示机制。 CLIP(来自 OpenAI)和 T5(来自 GoogleAI)文本编码器的集成使模型能够解释具有高度细微差别的描述。 CLIP 擅长将文本与视觉内容对齐,而 T5 则增强了模型处理结构化文本输入的能力。它们共同使 Flux.1 能够生成与用户提供的详细提示非常匹配的图像。
Flux.1 有哪些类型的模型?
Flux.1 具有三个不同的版本:Schnell、Dev 和 Pro。
- Schnell 是最快的模型,针对速度和效率进行了优化。它是在 Apache 2.0 许可证下发布的,因此可以用于商业用途。
- Dev 提供了一个更加灵活和实验性的框架,它专注于想要微调或定制模型某些功能的开发人员和研究人员。它以非商业许可证发布。
- Pro 是最先进且资源密集的版本。它提供更高分辨率的输出,并可以生成更复杂的图像,但它只能通过 Black Forest Labs API 使用。
如何免费使用Flux.1?
对于那些有兴趣探索 Flux.1 的功能而无需财务承诺的人来说,使用 modal.com 作为资源提供者是一个可行的选择。 Modal.com 每月提供 30 美元的计算能力津贴,可以支持每月生成大量图像。您可以在 Modal.com 定价上了解有关其定价和产品的更多信息。
此推荐未经平台赞助或认可。
首先,您需要使用 GitHub 凭据登录,在 modal.com 上创建一个帐户。
接下来,您需要安装 Modal CLI。确保您的计算机上安装了 Python。 Python 设置完成后,打开终端并执行命令 pip install modal。安装完成后,运行 modal setup 将 CLI 与您的 Modal 帐户链接。
继续将此 GitHub 存储库克隆到您的计算机并导航到克隆的目录。
为了安全起见,请在模态仪表板中使用名为 API_KEY 的环境变量创建一个名为 Flux.1-secret 的密钥,并为其分配一个随机字符串。
最后,通过在终端中运行 modal deploy app.py --name Flux1 来部署服务。成功部署后,modal 将提供用于访问 Web 服务的 URL:
✓ Created objects. ├── ? Created mount PythonPackage:app ├── ? Created function Model.build. ├── ? Created function Model.*. ├── ? Created function Model._inference. └── ? Created web function Model.web_inference => <public_url> ✓ App deployed in 3.206s! ? </public_url>
要使用该服务,请向提供的 PUBLIC URL 发出 GET 请求。在标头中包含您之前设置的 x-api-key,并在查询参数中对提示进行编码。您还可以通过查询参数指定所需的图像尺寸。以下是如何构建请求的示例:
curl -H "x-api-key: <api_key>" <public_url>?width=<width>&height=<height>&prompt=<prompt> </prompt></height></width></public_url></api_key>
理解代码
让我们剖析一下 app.py 文件,这对于使用 modal 平台运行 Flux.1 图像生成服务至关重要。以下是设置和功能的详细说明:
import modal image = modal.Image.debian_slim(python_version="3.10").apt_install( "libglib2.0-0", "libsm6", "libxrender1", "libxext6", "ffmpeg", "libgl1", "git" ).pip_install( "git+https://github.com/huggingface/diffusers.git", "invisible_watermark", "transformers", "accelerate", "safetensors", "sentencepiece", )
此块定义了我们的应用程序的 Docker 映像,指定了操作系统、必要的库和 Python 包。该环境支持 Flux.1 模型和相关实用程序的执行。
app = modal.App('flux1') with image.imports(): import os import io import torch from diffusers import FluxPipeline from fastapi import Response, Header
Here, we initialize our app and import necessary Python libraries within the context of our previously defined Docker image. These imports are essential for image processing and handling web requests.
@app.cls(gpu=modal.gpu.A100(), container_idle_timeout=15, image=image, timeout=120, secrets=[modal.Secret.from_name("flux.1-secret")]) class Model: @modal.build() def build(self): from huggingface_hub import snapshot_download snapshot_download("black-forest-labs/FLUX.1-schnell") @modal.enter() def enter(self): print("Loading model...") self.pipeline = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16).to('cuda') print("Model loaded!") def inference(self, prompt: str, width: int = 1440, height: int = 1440): print("Generating image...") image = self.pipeline( prompt, output_type='pil', width=width, height=height, num_inference_steps=8, generator=torch.Generator("cpu").manual_seed( torch.randint(0, 1000000, (1,)).item() ) ).images[0] print("Image generated!") byte_stream = io.BytesIO() image.save(byte_stream, format="PNG") return byte_stream.getvalue() @modal.web_endpoint(docs=True) def web_inference(self, prompt: str, width: int = 1440, height: int = 1440, x_api_key: str = Header(None)): api_key = os.getenv("API_KEY") if x_api_key != api_key: return Response(content="Unauthorized", status_code=401) image = self.inference(prompt, width, height) return Response(content=image, media_type="image/png")
This section defines the main functionality of our service:
- @modal.build(): Downloads the model when the application builds.
- @modal.enter(): Loads the model into GPU memory the first time the service is invoked.
- @modal.web_endpoint(): Serves as the web endpoint for our service using FastAPI.
If you just want to run it as a local service, you can add @modal.method() and define it as following inside the class.
@modal.method() def _inference(self, prompt: str, width: int = 1440, height: int = 1440): return self.inference(prompt, width, height)
And outside it, define a local entry point
@app.local_entrypoint() def main(prompt: str = "A beautiful sunset over the mountains"): image_bytes = Model()._inference.remote(prompt) with open("output.png", "wb") as f: f.write(image_bytes)
Local entry point will run locally on your machine calling the _inference method remotely, so you still using the modal’s service, without exposing it to the internet.
Conclusion
Flux.1 is not just another tech breakthrough - it's a game-changer for anyone who's ever dreamed of bringing their ideas to life visually. Imagine being able to describe a scene in words and watch as it materializes into a stunning, detailed image right before your eyes. That's the magic of Flux.1. It's like having a super-talented artist at your fingertips, ready to paint your thoughts with incredible precision. Whether you're an artist looking to speed up your creative process, a designer in need of quick visual concepts, or just someone who loves playing with new tech, Flux.1 opens up a world of possibilities. It's not about replacing human creativity - it's about enhancing it, making the journey from imagination to reality smoother and more exciting than ever before.
以上是如何运行 FLUXor Free:分步指南的详细内容。更多信息请关注PHP中文网其他相关文章!

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。 Python以简洁和强大的生态系统着称,C 则以高性能和底层控制能力闻名。

2小时内可以学会Python的基本编程概念和技能。1.学习变量和数据类型,2.掌握控制流(条件语句和循环),3.理解函数的定义和使用,4.通过简单示例和代码片段快速上手Python编程。

Python在web开发、数据科学、机器学习、自动化和脚本编写等领域有广泛应用。1)在web开发中,Django和Flask框架简化了开发过程。2)数据科学和机器学习领域,NumPy、Pandas、Scikit-learn和TensorFlow库提供了强大支持。3)自动化和脚本编写方面,Python适用于自动化测试和系统管理等任务。

两小时内可以学到Python的基础知识。1.学习变量和数据类型,2.掌握控制结构如if语句和循环,3.了解函数的定义和使用。这些将帮助你开始编写简单的Python程序。

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

Python3.6环境下加载Pickle文件报错:ModuleNotFoundError:Nomodulenamed...

如何解决jieba分词在景区评论分析中的问题?当我们在进行景区评论分析时,往往会使用jieba分词工具来处理文�...


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具