软件工程第一次作业
| 这个作业属于哪个课程 | https://edu.cnblogs.com/campus/fzu/2026-01SoftwareEngineeringandSoftwareEngineeringPractice |
|---|---|
| 这个作业要求在哪里 | https://edu.cnblogs.com/campus/fzu/2026-01SoftwareEngineeringandSoftwareEngineeringPractice/homework/16716 |
| 这个作业的目标 | 1. huggingface-API的调用2. Github个人主页3.搭建梳理技能树、学习目标,并分析与 AI 工具的结合 |
| 学号 | 102402135 |
1. huggingface-API的调用
在hugging face官网注册个人账号并获取API,调用Flux模型生成一张最贴近真实世界的图像
(1)注册并获取Token
进入网站注册,在setting/access/Token创建Token,在代码代替环境变量 HF_TOKEN
(2) 编写代码(包含后端和前端交互)调用模型,实现输入提示词生成结果
import os
import base64
from io import BytesIO
from flask import Flask, request, jsonify, render_template_string
from huggingface_hub import InferenceClient
# ========================================
# 1. Hugging Face Token
# ========================================
TOKEN = " HF_TOKEN"
# ========================================
# 2. Hugging Face 客户端
# ========================================
client = InferenceClient(
provider="auto",
api_key=TOKEN
)
MODEL = "XLabs-AI/flux-RealismLora"
# ========================================
# 3. 创建 Flask
# ========================================
app = Flask(__name__)
# ========================================
# 4. 前端页面
# ========================================
HTML_PAGE = """
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI 图片生成器</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: Arial, "Microsoft YaHei", sans-serif;
background: linear-gradient(135deg, #f5f7fa, #e8ecf3);
min-height: 100vh;
color: #333;
}
.container {
width: 900px;
max-width: 92%;
margin: 50px auto;
background: white;
border-radius: 20px;
padding: 35px;
box-shadow: 0 10px 35px rgba(0, 0, 0, 0.12);
}
h1 {
text-align: center;
margin-bottom: 10px;
color: #333;
}
.subtitle {
text-align: center;
color: #777;
margin-bottom: 30px;
}
label {
display: block;
font-weight: bold;
margin-bottom: 10px;
}
textarea {
width: 100%;
height: 130px;
padding: 15px;
border: 1px solid #ddd;
border-radius: 12px;
resize: vertical;
font-size: 16px;
line-height: 1.6;
outline: none;
}
textarea:focus {
border-color: #667eea;
}
button {
width: 100%;
margin-top: 18px;
padding: 14px;
border: none;
border-radius: 10px;
background: #667eea;
color: white;
font-size: 17px;
cursor: pointer;
}
button:hover {
background: #5568d9;
}
button:disabled {
background: #aaa;
cursor: not-allowed;
}
#status {
text-align: center;
margin-top: 20px;
color: #666;
min-height: 24px;
}
.result-box {
margin-top: 30px;
text-align: center;
}
.result-box img {
max-width: 100%;
border-radius: 15px;
box-shadow: 0 5px 20px rgba(0, 0, 0, 0.15);
}
.model-info {
margin-top: 25px;
padding: 12px;
background: #f5f5f5;
border-radius: 10px;
font-size: 14px;
color: #666;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<h1>AI 图片生成器</h1>
<div class="subtitle">
Hugging Face · FLUX Realism LoRA
</div>
<label for="prompt">请输入图片提示词:</label>
<textarea id="prompt">A realistic photo of a young woman walking in a city street, natural lighting, realistic skin texture, 35mm photography, photorealistic, highly detailed</textarea>
<button id="generateBtn" onclick="generateImage()">
生成图片
</button>
<div id="status"></div>
<div class="result-box" id="result"></div>
<div class="model-info">
使用模型:XLabs-AI/flux-RealismLora
</div>
</div>
<script>
async function generateImage() {
const prompt = document.getElementById("prompt").value.trim();
const button = document.getElementById("generateBtn");
const status = document.getElementById("status");
const result = document.getElementById("result");
if (!prompt) {
alert("请输入提示词!");
return;
}
button.disabled = true;
button.innerText = "正在生成,请稍候...";
status.innerText = "正在调用 Hugging Face API...";
result.innerHTML = "";
try {
const response = await fetch("/generate", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
prompt: prompt
})
});
const data = await response.json();
if (data.success) {
status.innerText = "生成成功!";
result.innerHTML =
`<img src="data:image/png;base64,${data.image}" alt="AI生成图片">`;
} else {
status.innerText = "生成失败:" + data.error;
}
} catch (error) {
status.innerText = "请求失败:" + error;
} finally {
button.disabled = false;
button.innerText = "生成图片";
}
}
</script>
</body>
</html>
"""
# ========================================
# 5. 首页
# ========================================
@app.route("/")
def index():
return render_template_string(HTML_PAGE)
# ========================================
# 6. 图片生成 API
# ========================================
@app.route("/generate", methods=["POST"])
def generate():
try:
data = request.get_json()
prompt = data.get("prompt", "").strip()
if not prompt:
return jsonify({
"success": False,
"error": "提示词不能为空"
})
print("================================")
print("收到新的图片生成请求")
print("Prompt:", prompt)
print("模型:", MODEL)
print("正在调用 Hugging Face...")
# 调用 Hugging Face
image = client.text_to_image(
prompt=prompt,
model=MODEL
)
# 将图片转换成 Base64
buffer = BytesIO()
image.save(buffer, format="PNG")
image_base64 = base64.b64encode(
buffer.getvalue()
).decode("utf-8")
print("API 调用成功!")
print("图片生成完成")
return jsonify({
"success": True,
"image": image_base64
})
except Exception as e:
print("API 调用失败:")
print(type(e).__name__)
print(e)
return jsonify({
"success": False,
"error": str(e)
})
# ========================================
# 7. 启动 Flask
# ========================================
if __name__ == "__main__":
print("================================")
print("AI 图片生成器启动")
print("模型:", MODEL)
print("代理:127.0.0.1:7897")
print("网页地址:http://127.0.0.1:5000")
print("================================")
app.run(
host="127.0.0.1",
port=5000,
debug=False
)
(3)提示词设计思路和修改过程
[1]初版使用a young woman walking in a city street
只描述主体、动作和场景,给模型的自由度较高,但是人物缺少主题表达,色彩与构图都表达不好
[2]改进后使用A realistic photo of a young woman walking in a city street, natural lighting, realistic skin texture, 35mm photography, photorealistic,highly detailed
加入自然光、真实皮肤、35mm摄影、超写实、高细节等要求,控制更具体。写实、细节丰富、具有摄影感
(4)API调用记录

(5)前端生成图像


(6)体验和心得
学习了如何使用 Python 调用 Hugging Face 模型,并结合后端接口实现图片。生成提示词设计、模型选择和参数设置都会影响最终效果
2. Github个人主页搭建
(1)采用方案1
在仓库根目录的 README.md 中撰写个人介绍,作为个人主页展示
(2)主页内容
包含个人简介,兴趣爱好,学习的技能,未来发展规划等

3.博客园随笔
(1)能力评估(以ABC评级)
A类能力
C语言,包括基本语法应用如指针,结构体
B类能力
python,包括基础的语法和API与数据处理分析
java,包括异常处理,基础项目框架
C类能力
web开发,如前端框架,后端开发,javascript
(2)代码量与目标
目前代码量在9000行到12000行之间,希望通过课程可以达到25000行
(3)本课程中最期待学习的知识,以及希望获得的收获
希望能学习软件设计与实际应用,能后与同伴一起合作,合理规范的模拟今后的具体实践
(4)生成一份简要的软件工程课程学习指南(chatgpt)
软件工程课程学习指南
• 软件工程基础:了解软件生命周期、软件开发过程,以及需求、设计、编码、测试和维护等基本阶段。
• 需求分析:学习如何明确用户需求,编写需求规格说明书,并使用用例等方法描述系统功能。
• 系统设计:掌握软件架构、模块化设计、面向对象设计等基本思想,提高系统设计能力。
• 软件测试:了解黑盒测试、白盒测试以及单元测试等方法,掌握发现和定位软件缺陷的基本思路。
• 项目管理:学习进度管理、团队协作、版本控制等内容,了解 Git/GitHub 在软件开发中的应用。
• 学习目标:通过课程学习建立完整的软件开发流程意识,为以后进行实际项目开发打下基础。
分析:指南点名学习方向,要从了解软件本身再走向开发,设身处地考虑用户需求,要积极学习优秀资源如github并培养团队意识,这对我帮助巨大
4. 作业要求


浙公网安备 33010602011771号