File size: 5,035 Bytes
e7bb669
 
 
 
 
 
 
 
 
 
 
a9649a7
e7bb669
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60a0933
3e54e68
e7bb669
 
 
3e54e68
e7bb669
 
 
 
 
 
 
 
 
 
 
 
3e54e68
 
 
 
 
e7bb669
3e54e68
e7bb669
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0199204
e7bb669
 
 
 
 
 
 
 
0199204
 
e7bb669
 
 
 
 
 
1142191
e7bb669
 
60a0933
e7bb669
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import os
import tempfile
from decimal import Decimal
from typing import List, Optional

import pandas as pd
from fastapi import FastAPI, HTTPException, UploadFile, File
from pydantic import BaseModel

from common import prepare_components_series, fit_and_forecast, current_month_snapshot
from advice import _load as advice_load, _gen as advice_gen, _to_bullets, clean_ru
from receipt_total_api import extract_info

app = FastAPI()

class Transaction(BaseModel):
    date: str
    amount: Decimal
    type: str
    category: Optional[str] = None
    description: Optional[str] = None


class ForecastRequest(BaseModel):
    granularity: str
    steps: int
    model: Optional[str] = None
    transactions: List[Transaction]


class ForecastResponse(BaseModel):
    period_end: List[str]
    income_forecast: List[float]
    expense_forecast: List[float]


class AdviceRequest(BaseModel):
    question: Optional[str] = None
    transactions: List[Transaction] = []


class AdviceResponse(BaseModel):
    advice: str


class ReceiptResponse(BaseModel):
    total: Optional[float]


advice_tokenizer = None
advice_model = None


@app.on_event("startup")
def load_models():
    global advice_tokenizer, advice_model
    advice_tokenizer, advice_model = advice_load()


from datetime import datetime 

@app.post("/forecast", response_model=ForecastResponse)
def forecast(req: ForecastRequest):
    if not req.transactions:
        raise HTTPException(status_code=400, detail="transactions is empty")

    df = pd.DataFrame([t.dict() for t in req.transactions])

    gran = (req.granularity or "month").lower()
    freq = "A-DEC" if gran.startswith("y") else "M"
    steps = int(req.steps or 1)
    method = (req.model or "auto").lower()

    inc, exp, _ = prepare_components_series(df, freq=freq)
    inc_fc = fit_and_forecast(inc, steps, freq, method=method)
    exp_fc = fit_and_forecast(exp, steps, freq, method=method)

    period_end = [
        d.strftime("%Y-%m-%dT%H:%M:%SZ")
        for d in inc_fc.index.to_pydatetime().tolist()
    ]

    return ForecastResponse(
        period_end=period_end,
        income_forecast=[float(x) for x in inc_fc.values.tolist()],
        expense_forecast=[float(x) for x in exp_fc.values.tolist()],
    )


@app.post("/advice", response_model=AdviceResponse)
def advice(req: AdviceRequest):
    tx = [t.dict() for t in req.transactions] if req.transactions else []
    df = pd.DataFrame(tx) if tx else None
    snap = current_month_snapshot(df) if df is not None and not df.empty else {}

    if snap:
        ctx = [
            f"Месяц: {snap['month']}",
            f"Доход: {snap['income_total']:.0f}",
            f"Расход: {abs(snap['expense_total']):.0f}",
            f"Нетто: {snap['net']:.0f}",
        ]
        if snap.get("top_expense_categories"):
            ctx.append("Топ статей расходов:")
            for cat, val in snap["top_expense_categories"]:
                ctx.append(f"- {cat}: {abs(val):.0f}")
        context = "\n".join(ctx)
    else:
        context = "Данных за текущий месяц нет."

    question = (req.question or "").strip()

    system_msg = (
        "Ты финансовый помощник. Отвечай по-русски. "
        "Верни ТОЛЬКО список из 5–7 конкретных шагов экономии с цифрами (лимиты, проценты, частота). "
        "Каждая строка должна начинаться с символов \"- \". Никаких вступлений."
    )
    messages = [
        {"role": "system", "content": system_msg},
        {
            "role": "user",
            "content": (
                f"Мои данные за текущий месяц:\n{context}\n\nВопрос: {question}\n"
                "Начни ответ сразу со строки, которая начинается с \"- \". Верни только список."
            ),
        },
    ]

    raw = advice_gen(messages, advice_tokenizer, advice_model, det=True)
    text = _to_bullets(clean_ru(raw))

    if text.count("\n") + 1 < 3:
        raw2 = advice_gen(messages, advice_tokenizer, advice_model, det=False)
        text2 = _to_bullets(clean_ru(raw2))
        if text2:
            text = text2

    return AdviceResponse(advice=text)



@app.post("/receipt-total-file", response_model=ReceiptResponse)
async def receipt_total_file(file: UploadFile = File(...)):

    suffix = os.path.splitext(file.filename or "")[1] or ".jpg"
    with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
        contents = await file.read()
        tmp.write(contents)
        tmp_path = tmp.name

    try:
        total = extract_total(tmp_path)
        return ReceiptResponse(total=total)
    finally:
        try:
            os.remove(tmp_path)
        except OSError:
            pass

if __name__ == "__main__":
    import uvicorn

    uvicorn.run("app:app", host="0.0.0.0", port=7860, workers=1)