Wealthmate / app.py
kennethduong's picture
update scan bills and speech function
f839663
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)