File size: 4,011 Bytes
2013214
 
 
 
 
 
 
 
 
 
 
8ed5020
 
 
dc7b249
 
8ed5020
2013214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f839663
 
 
2013214
 
 
 
f839663
 
 
 
 
 
 
 
 
 
2013214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f839663
2013214
 
f839663
 
 
2013214
f839663
 
 
 
 
 
2013214
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, jsonify
import pandas as pd
from expense_forecast.Expense_Forecasting import get_input_data, forecast_expense, model_fit, df
from chatbot.chatbot import classify_transaction, chat
from speech_transcribe.speech_transcribe import transcribe_file
from budget_suggestion.budget_suggestion import budget_suggestion
from read_image.scan_bills import scan_bills
import os
from flask_cors import CORS

app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": [
    'http://localhost:3000', 
    'http://localhost:8080',
    'https://wealthmate.onrender.com',
    'https://kennethduong-wealthmate.hf.space'
]}})

@app.route('/monthly_expense_prediction', methods=['POST'])
def handle_predict():
    data = request.get_json()
    input_data = get_input_data(
        data['Income (VND)'], 
        data['Interest rate (%)'], 
        data['Inflation rate (%)'], 
        data['Holidays']
    )
    forecast = forecast_expense(model_fit, input_data, df)
    return jsonify({'forecasted_expense': forecast})

@app.route('/transaction_classification', methods=['POST'])
def handle_classify():
    data = request.get_json()
    result = classify_transaction(data['prompt'])
    return jsonify(result)

@app.route('/chat', methods=['POST'])
def chat_with_user():
    data = request.get_json()
    mood = data['mood']
    prompt = data['prompt']
    response = chat(mood, prompt)
    return jsonify({'response': response})

@app.route('/speech_transcribe', methods=['POST'])
def handle_transcribe_speech():
    if 'file' not in request.files:
        return jsonify({'error': 'No file part'}), 400
    file = request.files['file']
    if file.filename == '':
        return jsonify({'error': 'No selected file'}), 400
    
    mood = request.form.get('mood', 'neutral')
    
    temp_path = 'temp_audio.wav'
    file.save(temp_path)
    try:
        transcription = transcribe_file(temp_path)
        result = classify_transaction(transcription)
        
        message = chat(mood, transcription)
        result_dict = {
            "description": result['description'],
            "amount": result['amount'],
            "category": result['category'],
            "chat_response": message
        }
        return jsonify(result_dict)
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        if os.path.exists(temp_path):
            os.remove(temp_path)
            
@app.route('/suggest_budget', methods=['POST'])
def handle_suggest_budget():
    data = request.get_json()
    amount = data['income']
    suggestion_text = budget_suggestion(amount)
    budget_dict = {}
    items = suggestion_text.split(',')
    for item in items:
        if ':' in item:
            key, value = item.split(':')
            budget_dict[key.strip()] = value.strip()
    return jsonify(budget_dict)

@app.route('/scan_bills', methods=['POST'])
def handle_scan_bills():
    if 'file' not in request.files:
        return jsonify({'error': 'No file part'}), 400
    file = request.files['file']
    if file.filename == '':
        return jsonify({'error': 'No selected file'}), 400
    temp_path = 'temp_image.jpg'
    file.save(temp_path)
    mood = request.form.get('mood', 'neutral')
    try:
        result = scan_bills(temp_path)
        values = result.split(':')
        values = [v.strip() for v in values]
        
        result_dict = {}
        result_dict["description"] = values[0]
        result_dict["category"] = values[1]
        result_dict["amount"] = values[2]
        
        message = chat(mood, result)
        result_dict["chat_response"] = message
        return jsonify(result_dict)
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        if os.path.exists(temp_path):
            os.remove(temp_path)
            
@app.route('/hello-world', methods=['GET'])
def hello():
    return "Hello World!"
    
if __name__ == '__main__':
    # app.run(debug=True)
    app.run(host='0.0.0.0', port=7860)