软件工程第一次作业
软工第一次个人作业
| 这个作业属于哪个课程 | H202601软件工程与软件工程实践 |
|---|---|
| 这个作业要求在哪里 | 作业要求 |
| 这个作业的目标 | 1.学习huggingface里api调用2.构建自己的博客园及github内容3.进行自我评估 |
| 学号 | 102402145 |
一、huggingface的API的调用
1.1 API 调用操作
-
登录huggingface账号生成对应access token。
-
python app.py,启动 fastapi 后端服务,访问浏览器地址http://localhost:8000。 -
在网页提示词输入框填入提示词,点击生成图像,前端发送post请求,后端调用api,访问远端大模型生成图片
1.2提示词设计思路及修改过程
提示词: snapshot, university campus path, green trees, afternoon soft sunlight, pedestrian in distance, natural photo, realistic, slight depth of field, candid shot, 8k
提示词设计思路:
- 场景定位:snapshot, university campus path
- 光影设置:afternoon soft sunlight
- 摄影风格限定:natural photo, realistic, candid shot
- 镜头参数设置:8k,slight depth of field
提示词修改过程:
- 将blur background 改为 slight depth of field,表示轻微景深,避免过度虚化
- 加入8k,slight depth of field等镜头参数设置
1.3api调用效果展示

1.4核心代码展示
后端核心代码:
@app.get("/")
def index() -> FileResponse:
return FileResponse(STATIC_DIR / "index.html")
@app.post("/api/generate")
def generate(req: GenRequest) -> JSONResponse:
if not HF_TOKEN:
raise HTTPException(500, "未在 .env 中读取到 HF_TOKEN")
client = InferenceClient(provider=PROVIDER, api_key=HF_TOKEN, timeout=REQUEST_TIMEOUT_S)
started = time.time()
img = client.text_to_image(
req.prompt,
model=MODEL_ID,
width=1024,
height=1024,
num_inference_steps=28,
guidance_scale=3.5,
extra_body={"loras": [{"path": LORA_PATH, "scale": 1.0}]},
)
duration_ms = int((time.time() - started) * 1000)
name = f"{datetime.now().strftime('%Y%m%d-%H%M%S')}_{uuid.uuid4().hex[:8]}.png"
img.convert("RGB").save(OUTPUTS_DIR / name, format="PNG")
return JSONResponse(
{"image_url": f"/outputs/{name}", "duration_ms": duration_ms}
)
app.mount("/outputs", StaticFiles(directory=OUTPUTS_DIR), name="outputs")
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000, reload=False)
1.5心得体会
- 学会了基本的api调用流程,使用前后端分离框架构建服务端和web端。
- 提示词对于图像最终生成的结果影响极大,具体完善的提示词,可以让大模型生成内容更贴近我们需求。
二、 Github个人主页搭建
1.1个人页面展示
访问链接zsp-c

三、博客随笔发布
1.1博客编辑页面


浙公网安备 33010602011771号