第一次作业

软件工程第一次作业

课程 <202601 软件工程>
作业要求 <2026秋软件工程个人作业(第一次)>
作业目标 1. 完成GitHub、博客园、HuggingFace账号注册与信息完善。
2. 完成两项实践:Flux模型API调用及前端交互实现;搭建GitHub个人主页,梳理技能与三年发展规划。
3. 在博客园发布随笔,复盘自身能力、明确课程学习目标,并分析AI生成的软件工程学习指南。
学号 102401312

一、准备工作

1.GitHub账号:yolo-yi

2.博客园账号:-Yolo

3.已关注吴越钟老师、张明圣和王奇蕊助教,并加入 202601 福州大学软件工程班级博客

二、Hugging Face API 调用与 FLUX 写实图像生成

Hugging Face 账号与 API 创建

api

创建 Conda 实验环境

本实验使用 Anaconda 创建独立的 Python 环境。

进入已经创建好的环境:

conda activate flux

安装需要的 Python 库:

pip install -U huggingface_hub pillow flask

主要依赖如下:

huggingface_hub
pillow
flask

Hugging Face API 调用

在实现前端之前,首先单独测试 Hugging Face API 是否能够正常完成图像生成。

创建文件:

test_api.py

核心代码如下:

import os
from huggingface_hub import InferenceClient

# 从系统环境变量中获取 Hugging Face Token
token = os.environ["HF_TOKEN"]

# 创建 API 客户端
client = InferenceClient(
    provider="fal-ai",
    api_key=token
)

# 图像生成 Prompt
prompt = """
A photorealistic street in Tokyo at night,
people walking on the street,
realistic photography,
natural lighting,
highly detailed
"""

print("正在调用 Hugging Face API...")
print("模型:XLabs-AI/flux-RealismLora")

try:

    image = client.text_to_image(
        prompt,
        model="XLabs-AI/flux-RealismLora"
    )

    image.save("result.png")

    print("API 调用成功!")
    print("图片已保存:result.png")

except Exception as e:

    print("API 调用失败:")
    print(e)

运行程序:

python test_api.py

API 调用成功的终端显示

API 调用成功

前端交互系统设计

为了实现交互式图像生成,本实验使用 Flask 搭建 Web 后端,并通过 HTML 页面接收用户输入的 Prompt。

项目文件结构如下:

flux_homework/
│
├── app.py
├── test_api.py
│
├── templates/
│   └── index.html
│
└── static/
    └── generated/

Flask 后端实现

核心代码如下:

import os
import time

from flask import Flask, render_template, request
from huggingface_hub import InferenceClient


app = Flask(__name__)

client = InferenceClient(
    provider="fal-ai",
    api_key=os.environ["HF_TOKEN"]
)


@app.route("/", methods=["GET", "POST"])
def index():

    image_url = None
    prompt = ""

    if request.method == "POST":

        prompt = request.form.get("prompt", "")

        print("=" * 50)
        print("收到 Prompt:")
        print(prompt)

        try:

            print("正在调用 XLabs-AI/flux-RealismLora...")

            image = client.text_to_image(
                prompt,
                model="XLabs-AI/flux-RealismLora"
            )

            filename = f"image_{int(time.time())}.png"

            save_path = os.path.join(
                "static",
                "generated",
                filename
            )

            image.save(save_path)

            image_url = f"/static/generated/{filename}"

            print("API 调用成功!")

        except Exception as e:

            print("API 调用失败:")
            print(e)

    return render_template(
        "index.html",
        image_url=image_url,
        prompt=prompt
    )


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

采用官方代码用于调用模型:

image = client.text_to_image(
    prompt,
    model="XLabs-AI/flux-RealismLora"
)

将图片保存到:

static/generated/

HTML 前端实现

前端主要提供:

  • Prompt 输入框;
  • “生成图片”按钮;
  • 生成结果展示区域。

页面核心代码如下:

<form method="POST">

    <label>
        <strong>Prompt:</strong>
    </label>

    <br><br>

    <textarea
        name="prompt"
        placeholder="请输入图像描述..."
        required>{{ prompt }}</textarea>

    <button type="submit">
        生成图片
    </button>

</form>

{% if image_url %}

<div class="result">

    <h2>生成结果</h2>

    <img src="{{ image_url }}">

</div>

{% endif %}

启动程序:

python app.py

随后在浏览器访问:

http://127.0.0.1:5000

前端初始页面

前端交互页面

Prompt 设计与修改过程

本次作业要求生成一张尽可能贴近真实世界的图像,为了观察 Prompt 对生成结果的影响,共进行了三轮 Prompt 优化。

第一版 Prompt

第一版仅描述了基本场景:

A street in at night.

设计思路

第一版 Prompt 主要用于测试模型对于基本场景信息的理解能力,没有加入过多风格控制词。

生成结果

prompt1

第二版 Prompt

第二版修改为:

A photorealistic street at night,
people walking with umbrellas,
realistic photography,
highly detailed.

设计思路

加入 photorealisticrealistic photography,明确要求模型生成照片写实风格图像。

加入:

people walking with umbrellas

补充人物与场景信息。

加入:

highly detailed

增强画面的细节表现。

生成结果

prompt2


第三版 Prompt

最终 Prompt 设计为:

A photorealistic street at night,
wet pavement reflecting colorful neon lights,
people walking with transparent umbrellas,
realistic skin and clothing textures,
natural facial features,
cinematic lighting,
natural shadows,
professional street photography,
35mm lens,
shallow depth of field,
highly detailed,
realistic photography.

Prompt 结构分析

最终 Prompt 可以划分为以下几个部分。

1. 主体场景

A photorealistic street in at night

用于明确:

  • 夜晚;
  • 街道;
  • 写实风格

2. 环境细节

wet pavement reflecting colorful neon lights

通过湿润路面和霓虹灯倒影增强真实环境表现。

3. 人物描述

people walking with transparent umbrellas

增加真实生活场景中的人物活动。

4. 材质描述

realistic skin and clothing textures

用于增强人物皮肤和衣物材质表现。

5. 人脸真实性

natural facial features

要求模型尽量生成人类自然的面部特征。

6. 光照

cinematic lighting
natural shadows

增强画面中的光照层次,同时保留自然阴影效果。

7. 摄影语言

professional street photography
35mm lens
shallow depth of field

模拟真实摄影设备与摄影方式。

8. 整体质量

highly detailed
realistic photography

进一步强化图像细节与真实照片风格。

最终生成结果

prompt3

心得

通过本次实验,我完成了从 Hugging Face API 调用到 Web 前端交互式图像生成的完整流程,对大模型 API 的基本使用方式有了更加直观的理解。

首先,在程序实现方面,我学习了如何使用 Hugging Face 的 InferenceClient 调用远程图像生成模型,并通过环境变量保存 API Token,避免将敏感信息直接写入源代码。

其次,在前后端交互方面,我使用 Flask 搭建了一个简单的 Web 服务。用户可以在浏览器中输入 Prompt,后端接收到 Prompt 后调用模型,最后将生成结果返回到网页。这一过程让我更加清楚地理解了 AI 模型如何与实际应用程序结合。

在 Prompt 设计方面,我发现提示词会显著影响最终图像的效果。最开始只使用简单场景描述时,模型虽然可以生成对应内容,但是图片的真实感和细节表现不够稳定。随着逐步加入 photorealistic、材质、光照、人物特征以及摄影镜头等描述,模型生成的图片越来越接近真实摄影效果。

总体而言,本次实验不仅完成了 Hugging Face API 的调用,还实现了 AI 图像生成模型与 Web 前端的结合,同时加深了我对于 Prompt Engineering、LoRA 和生成式人工智能应用开发流程的理解。

三、Github个人主页搭建

地址:GitHub Pages个人主页

image

四、博客园随笔

随笔

博文编辑页面截图

image

posted @ 2026-09-09 22:40  ..Yolo  阅读(9)  评论(0)    收藏  举报