软工第一次作业

这个作业属于哪个课程 软件工程
这个作业要求在哪里 软件工程第一次作业
这个作业的目标 完成HuggingFace Flux模型API调用、搭建GitHub个人主页,梳理个人技术能力,建立软件工程学习规划
学号 102401212

1. 准备工作

  • 已准备一个GitHub账号,链接为:https://github.com/sl666slsl
  • 已准备一个博客园账号
  • 已关注三位老师并加入班级

2. 调用 huggingface-API

  • 实验简介

在Hugging Face官网注册账号,获取API Token,调用 XLabs-AI/flux-RealismLora 模型,基于基础模型代码,增加前端交互页面,实现网页输入提示词、生成写实图片。

模型地址: XLabs-AI/flux-RealismLora

  • 完整代码

项目文件结构

点击查看代码
project/
├─ app.py
└─ templates/
   └─ index.html

app.py(后端代码)

点击查看代码
from flask import Flask, render_template, request, jsonify
import requests, base64

app = Flask(__name__)
HF_TOKEN = "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"
API_URL = "https://api-inference.huggingface.co/models/XLabs-AI/flux-RealismLora"
headers = {"Authorization": f"Bearer {HF_TOKEN}"}

def query_model(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.content

@app.route('/')
def index():
    return render_template("index.html")

@app.route("/generate", methods=["POST"])
def generate():
    json_data = request.get_json()
    prompt = json_data.get("prompt", "")
    neg_prompt = json_data.get("negative", "")

    payload = {
        "inputs": prompt,
        "parameters": {
            "negative_prompt": neg_prompt
        }
    }
    image_bytes = query_model(payload)
    img_b64 = base64.b64encode(image_bytes).decode("utf-8")
    return jsonify({"image": img_b64, "prompt": prompt})

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

templates/index.html(前端交互页面)

点击查看代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Flux RealismLora 图像生成</title>
    <style>
        body {max-width: 850px; margin: 30px auto; font-family: "Microsoft Yahei";}
        textarea {width:100%; padding:8px;font-size:16px;}
        button {padding:10px 20px; background:#2385bb; color:#fff; border:none; border-radius:4px; font-size:16px;cursor:pointer;}
        #result {margin-top:24px;}
        img {max-width:100%;border-radius:4px;}
    </style>
</head>
<body>
    <h2>Flux RealismLora 写实图像生成器</h2>
    <div>
        <h4>正向提示词 Prompt</h4>
        <textarea id="prompt" rows="4">A quiet university campus lake at sunset, natural warm light, real photograph, DSLR, 8k, ultra realistic, fine texture, sharp focus</textarea>
    </div>
    <div style="margin-top:12px">
        <h4>负面提示词 Negative Prompt</h4>
        <textarea id="negPrompt" rows="3">cartoon, painting, anime, sketch, blur, deformed, ugly, low resolution, watermark</textarea>
    </div>
    <br>
    <button onclick="runGenerate()">开始生成图片</button>
    <div id="result"></div>

<script>
async function runGenerate(){
    const prompt = document.getElementById("prompt").value;
    const neg = document.getElementById("negPrompt").value;
    const resultDiv = document.getElementById("result");
    resultDiv.innerText = "正在请求HuggingFace API,模型较大,请等待...";
    try{
        const res = await fetch("/generate",{
            method:"POST",
            headers:{"Content-Type":"application/json"},
            body: JSON.stringify({prompt:prompt, negative:neg})
        })
        const data = await res.json();
        resultDiv.innerHTML = `<h3>✅生成成功</h3><p>提示词:${data.prompt}</p><img src="data:image/jpeg;base64,${data.image}">`
    }catch(err){
        resultDiv.innerText = "调用失败:"+err;
    }
}
</script>
</body>
</html>
  • 提示词设计思路和修改迭代过程

最终正向提示词

photograph of college campus path in morning, natural soft sunlight, realistic texture, DSLR photo, 8k, ultra detailed, sharp focus

中文释义:早晨的大学校园小路照片,自然柔和的光线,逼真的质感,单反相机拍摄,8K分辨率,超详细的细节,清晰的焦点

最终负面提示词

blurry,cartoon,painting,ugly,deformed,low quality,anime,sketch

中文释义:模糊,卡通,绘画,丑陋,畸形,低画质,动漫,素描

提示词设计思路和修改过程

  1. 初稿版本: college campus road
    只简单描述场景,生成画面细节匮乏,光影虚假,树木与路面结构扭曲,真实感不足。
  2. 第二次修改: photo of college campus road, morning
    增加 photo 与时间描述,画面轮廓变得正常,但缺少摄影质感,整体像AI渲染图,没有真实照片的肌理。
  3. 第三次优化:增加光线、画质关键词
    photograph of college campus path in morning, natural soft sunlight, DSLR photo
    画面光影变得柔和自然,场景氛围感提升,但是依然偶尔出现手绘、畸形物体。
  4. 最终版本
    在正向词基础上补充 realistic texture,8k,ultra detailed,sharp focus 提升细节;同时添加负面提示词,过滤卡通、素描、模糊、畸形等瑕疵,最终生成这张秋日校园林荫道写实照片,贴近真实相机拍摄效果。
    43d7f170-0d25-42c8-bca2-61e138973f1d
  • API调用完整操作步骤
  1. 打开huggingface官网注册账号,进入Settings → Access Tokens,新建Token,复制保存。
    image

  2. 打开模型页面 XLabs-AI/flux-RealismLora ,确认模型可通过Inference API调用。

  3. 新建项目文件夹,创建 app.py ,新建 templates 目录并放入 index.html 。

  4. 安装依赖包: pip install flask requests

  5. 在代码中将 HF_TOKEN 替换为自己的HuggingFace令牌。

  6. 终端运行 python app.py ,浏览器访问 http://127.0.0.1:5000

  7. 在网页填写正负提示词,点击按钮提交请求,等待API返回图片。
    13c82d5f-136a-4dc1-af59-a8cb764e1729

  • 体验与心得

本次实验通过API远程调用大模型,体会到前后端分离的简易Web项目开发流程。HuggingFace推理存在等待时间长、网络不稳定的问题;提示词对图像质量影响巨大,需要不断调试优化。同时我认识到软件工程思想在小型项目中的价值:把界面、业务逻辑、数据请求分开,方便后续修改和维护。

3.Github个人主页搭建
我选择通过 GitHub 个人资料 README 的方式搭建个人主页。
c388780d-078f-4823-9f2f-3a5e54205937
主页地址:https://github.com/sl666slsl

4.在博客园发一篇随笔
随笔地址:https://www.cnblogs.com/sl66/p/22934837

5.博文编辑页面的截图
image

posted @ 2026-09-11 14:53  sl66  阅读(7)  评论(0)    收藏  举报