Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,62 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
def
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
fn=answer_question,
|
| 64 |
-
inputs=[
|
| 65 |
-
gr.Image(type="pil", label="📷 上传作业图片"),
|
| 66 |
-
gr.Textbox(
|
| 67 |
-
label="💬 你的问题",
|
| 68 |
-
placeholder="例如:这道题的答案是多少?请逐步解答。",
|
| 69 |
-
lines=2,
|
| 70 |
-
),
|
| 71 |
-
],
|
| 72 |
-
outputs=gr.Textbox(label="🤖 AI 回答", lines=8),
|
| 73 |
-
title="📚 作业识图 AI (CPU 版)",
|
| 74 |
-
description="上传作业照片,输入问题,AI 会尝试识别并给出答案。",
|
| 75 |
-
theme=gr.themes.Soft(),
|
| 76 |
-
)
|
| 77 |
-
|
| 78 |
-
if __name__ == "__main__":
|
| 79 |
-
demo.launch(
|
| 80 |
-
server_name="0.0.0.0",
|
| 81 |
-
server_port=7860,
|
| 82 |
-
share=True,
|
| 83 |
-
)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form, Depends, HTTPException
|
| 2 |
+
from fastapi.staticfiles import StaticFiles
|
| 3 |
+
from fastapi.responses import FileResponse
|
| 4 |
+
import auth, storage, ai_search
|
| 5 |
+
|
| 6 |
+
app = FastAPI(title="云学习平台")
|
| 7 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 8 |
+
|
| 9 |
+
@app.on_event("startup")
|
| 10 |
+
def startup():
|
| 11 |
+
auth.init_db()
|
| 12 |
+
storage.ensure_bucket()
|
| 13 |
+
|
| 14 |
+
# ── 用户认证 ──
|
| 15 |
+
@app.post("/auth/register")
|
| 16 |
+
def register(username: str = Form(...), password: str = Form(...)):
|
| 17 |
+
conn = auth.sqlite3.connect(auth.DB_PATH)
|
| 18 |
+
try:
|
| 19 |
+
conn.execute(
|
| 20 |
+
"INSERT INTO users (username, password_hash, created_at) VALUES (?,?,?)",
|
| 21 |
+
(username, auth.hash_password(password), auth.datetime.now().isoformat())
|
| 22 |
+
)
|
| 23 |
+
conn.commit()
|
| 24 |
+
except auth.sqlite3.IntegrityError:
|
| 25 |
+
raise HTTPException(400, "用户名已存在")
|
| 26 |
+
finally:
|
| 27 |
+
conn.close()
|
| 28 |
+
token = auth.create_access_token({"sub": username})
|
| 29 |
+
return {"access_token": token, "token_type": "bearer"}
|
| 30 |
+
|
| 31 |
+
@app.post("/auth/login")
|
| 32 |
+
def login(username: str = Form(...), password: str = Form(...)):
|
| 33 |
+
conn = auth.sqlite3.connect(auth.DB_PATH)
|
| 34 |
+
row = conn.execute("SELECT password_hash FROM users WHERE username=?", (username,)).fetchone()
|
| 35 |
+
conn.close()
|
| 36 |
+
if not row or not auth.verify_password(password, row[0]):
|
| 37 |
+
raise HTTPException(401, "用户名或密码错误")
|
| 38 |
+
token = auth.create_access_token({"sub": username})
|
| 39 |
+
return {"access_token": token, "token_type": "bearer"}
|
| 40 |
+
|
| 41 |
+
# ── 文件上传 ──
|
| 42 |
+
@app.post("/files/upload")
|
| 43 |
+
async def upload(file: UploadFile = File(...), user: str = Depends(auth.get_current_user)):
|
| 44 |
+
content = await file.read()
|
| 45 |
+
path = storage.upload_file(content, file.filename, user, file.content_type or "application/octet-stream")
|
| 46 |
+
return {"path": path, "filename": file.filename}
|
| 47 |
+
|
| 48 |
+
@app.get("/files/list")
|
| 49 |
+
def list_files(user: str = Depends(auth.get_current_user)):
|
| 50 |
+
return {"files": storage.list_user_files(user)}
|
| 51 |
+
|
| 52 |
+
# ── AI 识图搜索 ──
|
| 53 |
+
@app.post("/ai/search-by-image")
|
| 54 |
+
async def search_by_image(file: UploadFile = File(...), user: str = Depends(auth.get_current_user)):
|
| 55 |
+
image_bytes = await file.read()
|
| 56 |
+
description = await ai_search.analyze_image(image_bytes)
|
| 57 |
+
results = await ai_search.search_in_library(description, user, storage)
|
| 58 |
+
return {"ai_description": description, "results": results}
|
| 59 |
+
|
| 60 |
+
@app.get("/")
|
| 61 |
+
def root():
|
| 62 |
+
return FileResponse("static/index.html")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|