软件工程第一次作业
| 这个作业属于哪个课程 | https://edu.cnblogs.com/campus/fzu/2026-01SoftwareEngineeringandSoftwareEngineeringPractice |
|---|---|
| 这个作业要求在哪里 | https://edu.cnblogs.com/campus/fzu/2026-01SoftwareEngineeringandSoftwareEngineeringPractice/homework/16716 |
| 这个作业的目标 | 熟悉API调用与前端交互,搭建GitHub主页,撰写学习随笔 |
| 学号 | 102401525 |
一.准备工作
在开始本次作业前,我已完成了以下准备工作:
GitHub账号:已在官网注册并完善了个人信息(头像、昵称、简介等)。
博客园账号:已注册并申请开通了博客,设置了个人昵称、头像与博客样式。
关注老师与助教:已关注任课老师(黄兆武)以及助教(李怡涵、焦圣蒙)的博客。
加入班级博客:已加入H202601软件工程与软件工程实践班级,并确认已实名制。
二、 Hugging Face API 调用(任务1)
1.后端核心代码(app.py 关键部分)
from huggingface_hub import InferenceClient
import os
创建推理客户端
client = InferenceClient(
provider="fal-ai", # 使用 fal-ai 作为推理服务提供商
api_key=os.environ["HF_TOKEN"], # 从环境变量读取 API Token
)
调用 text_to_image 生成图像
image = client.text_to_image(
prompt="A young woman standing on a city street at golden hour...",
model="XLabs-AI/flux-RealismLora", # 指定模型 ID
width=1024,
height=768,
)
保存生成的图片
image.save("outputs/generated.png")
2.Flux RealismLora 是专门针对摄影写实风格微调的 LoRA,提示词设计遵循以下原则:
主体明确:先说清楚画面主体是谁 / 是什么
场景环境:描述在哪里、什么时间
光线条件:光线方向、色温、氛围
摄影参数:镜头焦段、景深、相机型号
风格修饰:photorealistic、film grain 等增强写实感
提示词迭代过程
第一版(太简单,效果不理想):
a beautiful woman
问题:描述太模糊,AI 不知道画什么风格、什么场景,生成结果偏卡通感
第二版(加入场景但不够专业):
a woman on the street, sunny day, realistic
问题:有了基本场景,但缺少摄影专业术语,画面质感不够真实
第三版(完善光线和摄影参数):
A young woman standing on a city street, wearing a trench coat,
natural daylight, photorealistic, portrait photography
问题:有进步,但缺少时间氛围和镜头感,画面比较平
最终版(完整描述,效果最好):
A young woman standing on a city street at golden hour,
wearing a beige trench coat, natural lighting, photorealistic,
85mm portrait photography, shallow depth of field,
shot on Sony A7IV, film grain
改进点:
golden hour(黄金时刻):指定日落前的暖光时段,增加氛围感
85mm portrait photography:指定人像焦段,背景虚化自然
shallow depth of field:浅景深,突出主体
shot on Sony A7IV:指定相机型号,增强照片真实感
film grain:胶片颗粒感,减少 AI 生成的塑料感
其他测试提示词
场景
提示词
效果说明
咖啡馆
A cozy coffee shop interior, warm afternoon sunlight through the window, wooden tables, steam rising from a cup of coffee, photorealistic, cinematic lighting
暖色调氛围好,细节丰富
山景
A misty mountain landscape at sunrise, layers of peaks fading into the fog, pine trees on the ridge, golden morning light, photorealistic landscape photography
层次分明,雾气效果自然
匠人特写
Close-up portrait of an elderly craftsman's hands, weathered skin, holding a wooden carving tool, warm workshop lighting, shallow depth of field, macro photography
皮肤纹理真实,光影细腻
API 调用体验与心得
整体体验
优点:
接入简单:使用 huggingface_hub 的 InferenceClient,只需要几行代码就能调用模型,不需要自己部署 GPU 环境
效果出色:Flux + RealismLora 生成的图像写实度很高,人物皮肤、光线、景深都非常接近真实摄影
前端交互友好:通过 Flask 做中间层,前端只需要发 HTTP 请求,不需要处理复杂的模型调用逻辑

三.github主页的创建(任务2)


四.博客园随笔(任务3)


浙公网安备 33010602011771号