软件工程 - Task 1

这个作业属于哪个课程 H202601 软件工程与软件工程实践(福州大学计算机与大数据学院)
这个作业要求在哪里 2026 秋软件工程个人作业(第一次)
这个作业的目标 注册基础的博客与协作平台账号,为后续时间做准备;尝试调用生成式 AI 接口;个人主页的搭建展示自我,并借这次作业梳理自己的技术基础、兴趣方向与本学期目标
学号 102402136

0. 准备工作

老师,两位助教好~我是来自大数据专业的王智洋。

🐑羊咩咩,因而我的博客园名字是MieSheeeep

由于事务堆积,实在是不好意思,这么晚才提交本作业。

已完成以下准备

1. HuggingFace-API的调用

1.1 获取一个新APIkey

之前有使用过hf下载过一些数据或模型,但是从未使用过接口相关功能。于是创建了新的接口key,取得调用权限。

1788968241961

1.2 后端与网页

项目后端这里我选择用老牌库flask书写。apikey存放在.env中,运行时读取,方便读写与更改。

👉 点击查看后端代码
import base64
import os
from datetime import datetime
from pathlib import Path

import requests
from flask import Flask, render_template, request, send_from_directory, url_for

BASE_DIR = Path(__file__).parent
OUTPUT_DIR = BASE_DIR / "outputs"
TOKEN_FILE = BASE_DIR / ".env" / "cf"
MODEL = "@cf/black-forest-labs/flux-1-schnell"
API_URL = f"https://api.cloudflare.com/client/v4/accounts/6692c9352b2651dc726337aefb7f939b/ai/run/{MODEL}"


def read_cloudflare_token(token_file=TOKEN_FILE):
    token = os.getenv("CLOUDFLARE_API_TOKEN", "").strip()
    return token or (token_file.read_text(encoding="utf-8").strip() if token_file.is_file() else "")


def generate_image(prompt, steps=4, output_dir=OUTPUT_DIR, http_client=requests):
    prompt = prompt.strip()
    if not prompt:
        raise ValueError("提示词不能为空")
    steps = int(steps)
    if not 1 <= steps <= 8:
        raise ValueError("生成步数必须在 1 到 8 之间")
    token = read_cloudflare_token()
    if not token:
        raise ValueError("请在 .env/cf 中设置 Cloudflare API Token")

    response = http_client.post(
        API_URL,
        headers={"Authorization": f"Bearer {token}"},
        json={"prompt": prompt, "steps": steps},
        timeout=180,
    )
    response.raise_for_status()
    image = base64.b64decode(response.json()["result"]["image"], validate=True)
    output_dir.mkdir(parents=True, exist_ok=True)
    path = output_dir / datetime.now().strftime("%Y%m%d_%H%M%S_%f.jpg")
    path.write_bytes(image)
    return path


def create_app(image_generator=generate_image, output_dir=OUTPUT_DIR):
    app = Flask(__name__)

    def page(prompt="", steps=4, error=None, status=200, image_url=None):
        return render_template(
            "index.html", prompt=prompt, steps=steps, error=error, image_url=image_url
        ), status

    @app.get("/")
    def index():
        return page()

    @app.post("/generate")
    def generate():
        prompt, steps = request.form.get("prompt", ""), request.form.get("steps", "4")
        try:
            steps = int(steps)
        except ValueError:
            return page(prompt, steps, "生成步数必须是整数", 400)
        if not 1 <= steps <= 8:
            return page(prompt, steps, "生成步数必须在 1 到 8 之间", 400)
        try:
            path = image_generator(prompt, steps, output_dir)
        except ValueError as error:
            return page(prompt, steps, str(error), 400)
        except Exception:
            return page(prompt, steps, "生成失败,请稍后重试", 500)
        return page(prompt, steps, image_url=url_for("output_file", filename=path.name))

    @app.get("/outputs/<path:filename>")
    def output_file(filename):
        return send_from_directory(output_dir, filename)

    return app


app = create_app()

if __name__ == "__main__":
    app.run(debug=True)

前端这里由gpt倾情书写,可以看到效果还是不错的

AI 辅助生成的前端页面

经测试,发现可以正常调用hf接口。

最初的后端调用测试

测试时所生成的体育馆图片

更换接口后的生成结果

1.3 更换 API

意料之外的之情发生了,我只调用了两次api,hf的账单就来到了0.07刀(hf也太吝啬了),无法再次进行生成了。

经过查询,发现CloudFlare平台也有免费的额度,并且也是Flux模型,于是去注册了一个cf的账户,重新配置了后端代码以适配新api。

经过九九八十一难,终于可以重新进行生图。

下面可以看到测试成功

Cloudflare Workers AI 后端测试

1.4 提示词进化

这部分提示词是在 GPT-5.5 的辅助下逐轮调整的。

经过讨论,我们认为雨夜街头人像这个场景最适合进行测试提示词效果,观察变化

因为里面有黑暗,有光影,有人的表情有雨,有地面的反光、水渍

这对生图模型的控制是考验的

V1 第一版

简单描述了一下主体、环境

A man,然后再城市街道,以及一些常用的优化词(realistic、photorealistic什么的)

(其实这也比我自己写提示词强多了,gpt考虑要比我全面很多,也知道一些操作技巧)

A young man standing at a city intersection at night after heavy rain,
wet streets reflecting neon lights and car headlights, realistic urban
environment, natural pose, photorealistic, highly detailed.
V1 生成结果

可以看到生成结果已经还算不错了,我所想看到的东西都已经呈现了。

但是还有一些细节,比如背景模糊到不真实,莫名的红色光带明显不符合现实世界,还有雨天没撑伞的主体衣服和头发也太干燥了

V2 加入细节

我把上一版的点评交给gpt,让它帮我优化

它不仅仅解决了那些问题:地上的光影解决了,人物面部刻画很好,甚至是地上的那些水渍,都更能看清了

还添加了非常非常多的描写细节,把非常多的地方都控制到了。

我都无法想象,人如果没有真的出现在那个场景内,真的能写出这么多细致入微的调控吗

如果没有写到,那么很多细节又讲会变成抽卡,所以提示词生图确实也是一门学问

A young man standing alone at a busy city intersection at night just after
heavy rain, wearing a slightly wet dark jacket. The asphalt is covered with
rainwater, reflecting red traffic lights, warm storefront lights and passing
car headlights.

Candid street photography, eye-level shot, 35mm lens, shallow depth of field,
realistic perspective, natural cinematic lighting, subtle mist in the air,
raindrops visible on his jacket and hair, blurred pedestrians and cars moving
in the background.

Photorealistic, realistic skin texture, natural colors, high dynamic range,
documentary photography, highly detailed.
V2 生成结果

gpt:第二版补充了机位、焦段、景深、光线、背景运动和雨水细节。相比单纯堆叠"高清、真实",这些描述更接近可以被模型执行的画面条件。

但是还是被我抓到了一些细节问题,比如周边都是红灯,人物脸上也理应是红色的光芒,而这一版虽然人物光影和五官确实融合的不错,但是光不是红色的,还是略显怪异。还有什么毛病

V3:明确"真实"具体指什么

到上一版我真的只能挑出上面那一个毛病了,所以我同时告诉gpt,你自行再改进呢

没想到它还能蒸,硬是编出了800字小作文般的提示词

gpt:第三版不再只说"要真实",而是描述皮肤纹理、轻微不对称、传感器噪点、动态模糊、曝光和反射等细节,并明确排除塑料皮肤、过饱和霓虹和赛博朋克效果。约束更强,提示词也明显变长。

v3版完整提示词
An authentic documentary photograph of a young East Asian man standing at a
crowded city intersection late at night, moments after a heavy summer rainstorm.

He is wearing a simple dark jacket slightly soaked by the rain. His hair is
damp and naturally messy, with tiny raindrops visible on individual strands.
His face has realistic pores, subtle skin imperfections, slight redness around
the nose and cheeks, natural asymmetry, and a calm, slightly tired expression.
No makeup, no perfect skin.

The wet asphalt is unevenly covered with puddles, reflecting red traffic signals,
warm convenience-store lights, white car headlights and muted city signage.
A bus passes through the background while several pedestrians holding umbrellas
cross the street. Fine mist rises from the warm pavement and distant objects
gradually disappear into humid night air.

Shot as spontaneous street photography on a full-frame camera with a 35mm lens
at f/2.8, ISO 1600, 1/125s. Eye-level composition, physically realistic depth
of field, slight motion blur on moving pedestrians, subtle sensor noise, natural
highlight roll-off, realistic exposure and white balance.

The image should feel like a real photograph captured accidentally during an
ordinary rainy night rather than a staged cinematic scene. Imperfect composition,
natural body posture, realistic clothing folds, physically plausible reflections,
restrained colors, authentic urban atmosphere, extremely photorealistic,
documentary photography, RAW photo look.

Avoid CGI appearance, plastic skin, excessive sharpness, oversaturated neon
colors, artificial studio lighting, unrealistic reflections, perfect facial
symmetry, exaggerated bokeh, fantasy or cyberpunk aesthetics.

让我们看看最终效果吧

V3 生成结果

呃、这次生成的脸就一言难尽了。可能是gpt加入了轻微不对称这样的提示,导致生图模型很疑惑

包括背景里加入的人,很多明显姿势也是怪异。

这次最多优化的应该是灯光和水渍,还有主角的头发和衣服质感。

应该提示词越长不一定越好。还要看提示词长度和生图模型能力的匹配吧,这个应该是一个小模型,很少什么轻微不对称的参数,所以模型只能猜,最后的效果就未必好了。有效的改进来自更具体的可视条件,而不是不断添加形容词。

1.5 api生图调用心得

前面优化的过程里也算是写了很多心得吧

第一次发现原来hf功能这么丰富,之前一直对hf一知半解的,这次算是了解了很多

提示词工程,确实可以称之为“工程啊”。里面有非常多的门道可以考究,明确自己的需求,再到如何用语言描述需求,还有对于模型的考虑,都是人与生图模型交互的重要课题。

再延伸,我会想,会不会语言的边界还是太狭隘了,我们凭什么将脑中的需求或想象,转化为寥寥的语言,再让ai试图去理解我们的语言呢(诶是不是我跑偏了?

2. GitHub 个人主页搭建

2.1 GitHub README

通过同名仓库,我做了一个简单的 GitHub 个人介绍页

并借助 capsule-render 这个库做了动态波浪顶部横幅(感觉还挺炫的

欢迎访问:MieSheeeep(zhiyang wang)

QQ_1789123504788

2.2 GitHub Pages

之前也用AI做过一个 GitHub Pages 页面,放了个人介绍和一篇博客,发布后没怎么维护

而且我觉得这个网站还是有待改进,不是特别美观

但是碍于我的技术力,又很难自己做改进

等以后有时间或者需要了再做优化吧

页面地址:MieSheeeep.github.io

GitHub Pages 页面

3. 在博客园发一篇随笔

严肃书写了一篇随笔发布在博客园
连接如下,里面包含了作业所要求的内容
初临博客园随笔 - MieSheeeep - 博客园

A 附录

md撰写截图
image

posted @ 2026-09-09 00:05  MieSheeeep  阅读(36)  评论(0)    收藏  举报