Spaces:
Sleeping
Sleeping
Commit ·
1bba663
1
Parent(s): 4be3615
feat : add history V1
Browse files- Dockerfile +23 -0
- app.py +90 -0
- requirements.txt +19 -0
- static/eth_history.csv +0 -0
Dockerfile
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 使用官方 Python 3.10 slim 映像
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# 設定工作目錄
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# 安裝系統依賴
|
| 8 |
+
RUN apt-get update && \
|
| 9 |
+
apt-get install -y git curl && \
|
| 10 |
+
rm -rf /var/lib/apt/lists/*
|
| 11 |
+
|
| 12 |
+
# 複製 requirements.txt 並安裝 Python 套件
|
| 13 |
+
COPY requirements.txt .
|
| 14 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 15 |
+
|
| 16 |
+
# 複製專案檔案
|
| 17 |
+
COPY . .
|
| 18 |
+
|
| 19 |
+
# 建立快取資料夾並開放權限
|
| 20 |
+
RUN mkdir -p /app/.cache && chmod -R 777 /app/.cache
|
| 21 |
+
|
| 22 |
+
# FastAPI 使用 uvicorn 啟動
|
| 23 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, Response, Form, Header, HTTPException, BackgroundTasks
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware # 匯入 FastAPI 的 CORS 中介軟體
|
| 4 |
+
|
| 5 |
+
from typing import Annotated # 推薦用於 Pydantic v2+
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
import json
|
| 10 |
+
import io
|
| 11 |
+
import os
|
| 12 |
+
from datetime import datetime
|
| 13 |
+
import uvicorn
|
| 14 |
+
from dotenv import load_dotenv # 匯入 dotenv 以載入 .env 環境變數檔案
|
| 15 |
+
|
| 16 |
+
STATIC_DIR = "static"
|
| 17 |
+
|
| 18 |
+
os.environ["TORCH_HOME"] = "./.cache"
|
| 19 |
+
os.environ["HF_HOME"] = "./.cache"
|
| 20 |
+
os.environ["TRANSFORMERS_CACHE"] = "./.cache"
|
| 21 |
+
os.makedirs("./.cache", exist_ok=True)
|
| 22 |
+
os.makedirs(STATIC_DIR, exist_ok=True)
|
| 23 |
+
|
| 24 |
+
load_dotenv()
|
| 25 |
+
|
| 26 |
+
# =====================
|
| 27 |
+
# 初始化 FastAPI
|
| 28 |
+
# =====================
|
| 29 |
+
app = FastAPI(title="Crypt Price")
|
| 30 |
+
|
| 31 |
+
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
| 32 |
+
# 設定 CORS (跨來源資源共用)
|
| 33 |
+
app.add_middleware(
|
| 34 |
+
CORSMiddleware,
|
| 35 |
+
allow_origins=["*"], # 允許所有來源
|
| 36 |
+
allow_credentials=True, # 允許憑證
|
| 37 |
+
allow_methods=["*"], # 允許所有 HTTP 方法
|
| 38 |
+
allow_headers=["*"], # 允許所有 HTTP 標頭
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# =====================
|
| 42 |
+
# API 路由
|
| 43 |
+
# =====================
|
| 44 |
+
@app.get("/")
|
| 45 |
+
def root():
|
| 46 |
+
return {"message": "Crypt Price API ready!"}
|
| 47 |
+
|
| 48 |
+
@app.get("/history")
|
| 49 |
+
async def cryptohistory():
|
| 50 |
+
try:
|
| 51 |
+
print("### start /cryptohistory !!")
|
| 52 |
+
|
| 53 |
+
file_path = os.path.join("static", "eth_history.csv")
|
| 54 |
+
|
| 55 |
+
# 讀取 CSV
|
| 56 |
+
df = pd.read_csv(file_path)
|
| 57 |
+
|
| 58 |
+
# 標準化欄位名稱
|
| 59 |
+
df.columns = df.columns.str.lower()
|
| 60 |
+
|
| 61 |
+
# 轉換日期格式
|
| 62 |
+
df["date"] = pd.to_datetime(df["date"]).dt.strftime("%Y-%m-%d")
|
| 63 |
+
|
| 64 |
+
# 建立回傳資料
|
| 65 |
+
results = []
|
| 66 |
+
for idx, row in df.iterrows():
|
| 67 |
+
results.append({
|
| 68 |
+
"id": idx + 1,
|
| 69 |
+
"date": row["date"],
|
| 70 |
+
"price": float(row["price"]),
|
| 71 |
+
"volume": float(row["volume"])
|
| 72 |
+
})
|
| 73 |
+
|
| 74 |
+
return {
|
| 75 |
+
"status": "success",
|
| 76 |
+
"data": results
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
except Exception as e:
|
| 80 |
+
return JSONResponse(
|
| 81 |
+
status_code=500,
|
| 82 |
+
content={
|
| 83 |
+
"status": "error",
|
| 84 |
+
"message": str(e)
|
| 85 |
+
}
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi[all]
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
torch
|
| 4 |
+
torchvision
|
| 5 |
+
pillow
|
| 6 |
+
numpy
|
| 7 |
+
pretrainedmodels
|
| 8 |
+
matplotlib
|
| 9 |
+
pytorch-msssim
|
| 10 |
+
opencv-python
|
| 11 |
+
tqdm
|
| 12 |
+
torchsummary
|
| 13 |
+
requests
|
| 14 |
+
google-genai
|
| 15 |
+
langchain
|
| 16 |
+
langchain-core
|
| 17 |
+
langchain-google-genai
|
| 18 |
+
python-dotenv
|
| 19 |
+
line-bot-sdk
|
static/eth_history.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|