2026秋软件工程个人作业(第一次)
| 这个作业属于哪个课程 | https://edu.cnblogs.com/campus/fzu/202601SofwareEngineering |
|---|---|
| 这个作业要求在哪里 | https://edu.cnblogs.com/campus/fzu/202601SofwareEngineering/homework/15712 |
| 这个作业的目标 | 1.加入班级 2.huggingface-API调用 3.Github个人主页搭建 4.博客园发一篇随笔 |
| 学号 | 102401605 |
1. huggingface-API的调用
API调用操作步骤
1.注册hugging face账号,在access token里面创建新的token
2.在桌面创建后缀为.py的后端,填入Token、设置网络代理端口,并指定模型
3.在桌面创建后缀为.html的前端,编写用于输入文字和显示图片的网页界面
3.在终端启动后端后打开前端开始生成图片
API调用成功记录

前段页面截图
提示词:A highly realistic documentary photograph: a white golden hamster drinking water

第二次添加了“al-white”和俯视图的要求,但实际上没有实现。
提示词:A highly realistic documentary photograph: an all-white golden hamster drinking water through a straw, side top-down view

后端代码
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import Response
from huggingface_hub import InferenceClient
from pydantic import BaseModel
import io
import logging
import time
# 设置日志,方便截图成功记录
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
app = FastAPI()
# 允许前端跨域访问
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# ⚠️⚠️⚠️ 替换为你的 Hugging Face Token ⚠️⚠️⚠️
HF_TOKEN = "YOUR_TOKEN_HERE"
client = InferenceClient(
provider="fal-ai",
api_key=HF_TOKEN,
)
# 定义前端传来的数据格式(解决422错误的关键)
class PromptRequest(BaseModel):
prompt: str
@app.post("/generate")
async def generate_image(request: PromptRequest):
prompt = request.prompt
logging.info(f"收到生成请求,提示词: {prompt}")
start_time = time.time()
try:
# 调用模型生成图片
image = client.text_to_image(
prompt=prompt,
model="XLabs-AI/flux-RealismLora",
width=1024,
height=1024,
)
# 将图片转为字节流传给前端
img_byte_arr = io.BytesIO()
image.save(img_byte_arr, format='JPEG')
img_byte_arr = img_byte_arr.getvalue()
cost_time = round(time.time() - start_time, 2)
logging.info(f"✅ 调用成功!HTTP状态码: 200 OK, 耗时: {cost_time}秒, 图片大小: {len(img_byte_arr)} bytes")
return Response(content=img_byte_arr, media_type="image/jpeg")
except Exception as e:
logging.error(f"❌ 调用失败: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)
前段代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Flux 写实图像生成器</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; background-color: #f4f4f9; }
.container { background: white; padding: 40px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); display: inline-block; width: 80%; max-width: 800px; }
h1 { color: #333; font-size: 28px; }
textarea { width: 90%; height: 80px; font-size: 18px; padding: 10px; margin-top: 10px; border: 2px solid #ccc; border-radius: 5px; }
button { font-size: 20px; padding: 15px 30px; margin-top: 20px; background-color: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer; }
button:hover { background-color: #0056b3; }
button:disabled { background-color: #ccc; }
#status { font-size: 18px; color: #555; margin-top: 15px; }
#resultImage { max-width: 100%; margin-top: 20px; border-radius: 8px; display: none; }
</style>
</head>
<body>
<div class="container">
<h1>🎨 写实图像生成器 (Flux RealismLora)</h1>
<p style="font-size: 18px; color: #666;">输入你对图片的描述,点击生成(建议用英文,生成需要约30秒)</p>
<textarea id="promptInput" placeholder="例如:A majestic mountain lake at sunrise, photorealistic"></textarea>
<br>
<button id="generateBtn" onclick="generateImage()">开始生成图片</button>
<div id="status"></div>
<img id="resultImage" alt="生成的图片">
</div>
<script>
async function generateImage() {
const prompt = document.getElementById('promptInput').value;
const btn = document.getElementById('generateBtn');
const status = document.getElementById('status');
const img = document.getElementById('resultImage');
if (!prompt.trim()) {
status.innerText = "⚠️ 请先输入图片描述!";
return;
}
btn.disabled = true;
status.innerText = "⏳ 正在调用模型生成图片,请耐心等待(约30秒)...";
img.style.display = "none";
try {
const response = await fetch('http://127.0.0.1:8000/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ prompt: prompt })
});
if (!response.ok) {
throw new Error(`服务器错误: ${response.status}`);
}
const blob = await response.blob();
const imageUrl = URL.createObjectURL(blob);
img.src = imageUrl;
img.style.display = "block";
status.innerText = "✅ 生成成功!";
} catch (error) {
status.innerText = "❌ 生成失败: " + error.message;
} finally {
btn.disabled = false;
}
}
</script>
</body>
</html>
心得和体验
这次huggingfaceApi调用经历了网络代理导致的SSL报错和免费额度耗尽的402报错,让我深刻体会到调用海外API时网络代理是基础,锻炼了解决问题的动手能力,深化了对调用模型的认知,丰富了实践的经验。
2. Github个人主页搭建
方案一

3. 随笔
一、技能树梳理与自我评估
已掌握的专业知识与能力
- 掌握C语言基础,能够完成简单程序编写
- 了解C++基础语法,面向对象相关内容掌握得不太扎实
- 学过汇编语言基础指令
- 学习过数据结构理论知识,代码实现比较生疏
感兴趣的技术方向
- 后端开发,希望学习相关框架的基础使用
- AI应用领域,想要了解大模型接口调用方式
- 基础网页交互开发
还欠缺的能力
- 算法练习量不足,数据结构代码动手能力弱
- 几乎没有项目经历,不熟悉Git团队协作方式
- 不了解项目部署、测试相关知识,代码规范性有待提高
二、当前代码量与目标
- 目前累计代码量:约3000行,大多为课堂作业,自主练习较少
- 本学期结束目标代码量:争取达到5000行,提升代码编写熟练度
三、课程最期待学习的知识与收获
- 希望掌握软件工程基础流程,了解软件开发完整流程
- 学会Git工具,体验小组合作开发项目
- 学会将零散代码整合为完整项目,改善只会写代码片段的问题
四、软件工程课程学习指南
以下内容由豆包生成:
软件工程课程简要学习指南
一、学习目标
理解软件工程核心思想:用工程化方法规范软件全生命周期开发,不只写代码,还要学会需求分析、设计、测试、协作与项目管理,产出可靠、可维护的软件。
二、核心重点知识
- 软件生命周期:需求→概要设计→详细设计→编码→测试→部署维护
- 开发模型:瀑布模型、增量、迭代、敏捷(Scrum),分清各自适用场景与优缺点
- 需求工程:需求获取、需求规格说明书,区分功能性 / 非功能性需求
- 软件设计:模块化、高内聚低耦合;UML 基础(用例图、类图、时序图)
- 软件测试:单元测试、集成测试、系统测试;黑盒 / 白盒测试,缺陷管理
- 项目管理:版本控制、任务拆分、风险、团队协作
三、实操练习建议
- 配合 Git 管理代码,做好版本提交记录
- 小组项目优先练习需求文档、设计文档撰写,不要直接上手编码
- 写单元测试,养成边开发边测试的习惯
- 学习画 UML 图,练习把业务需求转化为模型
四、学习方法
- 课堂:重点理解为什么要做,而不是死记概念
- 课后:结合小型项目实践,把理论落地
- 复盘:学会评审代码、发现缺陷,评估项目存在的问题
五、预期收获
掌握软件工程基础理论,能参与小组软件开发项目;会撰写简单开发文档、绘制基础 UML 图;建立规范编码、测试、版本管理的工程思维,理解团队协作开发流程。
分析:这份软件工程学习指南整体合理,覆盖课程核心知识点,但内容较为通用,个性化不足。
4. Markdown编辑页面

浙公网安备 33010602011771号