Eliot0110 commited on
Commit
3f7d970
·
1 Parent(s): 8fa71e2

fix: 移除app.py中的直接模型加载代码

Browse files

- 删除旧的测试代码中的直接模型实例化
- 模型加载现在通过TravelAssistant类管理
- 修复启动时的认证错误

Files changed (1) hide show
  1. app.py +39 -45
app.py CHANGED
@@ -1,45 +1,39 @@
1
- from transformers import AutoProcessor, Gemma3nForConditionalGeneration
2
- from PIL import Image
3
- import requests
4
- import torch
5
-
6
- model_id = "google/gemma-3n-e2b-it"
7
-
8
- model = Gemma3nForConditionalGeneration.from_pretrained(model_id, device="cuda", torch_dtype=torch.bfloat16,).eval()
9
-
10
- processor = AutoProcessor.from_pretrained(model_id)
11
-
12
- messages = [
13
- {
14
- "role": "system",
15
- "content": [{"type": "text", "text": "You are a helpful assistant."}]
16
- },
17
- {
18
- "role": "user",
19
- "content": [
20
- {"type": "image", "image": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"},
21
- {"type": "text", "text": "Describe this image in detail."}
22
- ]
23
- }
24
- ]
25
-
26
- inputs = processor.apply_chat_template(
27
- messages,
28
- add_generation_prompt=True,
29
- tokenize=True,
30
- return_dict=True,
31
- return_tensors="pt",
32
- ).to(model.device, dtype=torch.bfloat16)
33
-
34
- input_len = inputs["input_ids"].shape[-1]
35
-
36
- with torch.inference_mode():
37
- generation = model.generate(**inputs, max_new_tokens=100, do_sample=False)
38
- generation = generation[0][input_len:]
39
-
40
- decoded = processor.decode(generation, skip_special_tokens=True)
41
- print(decoded)
42
-
43
- # **Overall Impression:** The image is a close-up shot of a vibrant garden scene,
44
- # focusing on a cluster of pink cosmos flowers and a busy bumblebee.
45
- # It has a slightly soft, natural feel, likely captured in daylight.
 
1
+ # app.py - 正确的版本
2
+ from fastapi import FastAPI, HTTPException
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ from fastapi.responses import JSONResponse
5
+ from utils.validators import ChatRequest, ChatResponse
6
+ from utils.logger import log
7
+ from modules.travel_assistant import TravelAssistant
8
+ import traceback
9
+
10
+ # --- FastAPI 应用设置 ---
11
+ app = FastAPI(
12
+ title="Modular Travel Assistant API",
13
+ description="一个采用模块化和分层架构的旅行助手API",
14
+ version="2.0.0",
15
+ docs_url="/docs",
16
+ redoc_url="/redoc"
17
+ )
18
+
19
+ app.add_middleware(
20
+ CORSMiddleware,
21
+ allow_origins=["*"],
22
+ allow_credentials=True,
23
+ allow_methods=["*"],
24
+ allow_headers=["*"],
25
+ )
26
+
27
+ # --- 全局服务实例 ---
28
+ try:
29
+ log.info("🔄 开始初始化 Travel Assistant 服务...")
30
+ assistant = TravelAssistant() # 这里才是正确的模型加载方式
31
+ SERVICE_READY = True
32
+ log.info("🚀 FastAPI 应用启动成功,服务已就绪。")
33
+ except Exception as e:
34
+ assistant = None
35
+ SERVICE_READY = False
36
+ log.critical(f"💥 FATAL: 服务初始化失败: {e}")
37
+ log.critical(f"💥 错误详情: {traceback.format_exc()}")
38
+
39
+ # ... 其余的API端点代码