Spaces:
Running
Running
Commit
·
f839663
1
Parent(s):
41e8fd4
update scan bills and speech function
Browse files
app.py
CHANGED
@@ -49,11 +49,23 @@ def handle_transcribe_speech():
|
|
49 |
file = request.files['file']
|
50 |
if file.filename == '':
|
51 |
return jsonify({'error': 'No selected file'}), 400
|
|
|
|
|
|
|
52 |
temp_path = 'temp_audio.wav'
|
53 |
file.save(temp_path)
|
54 |
try:
|
55 |
transcription = transcribe_file(temp_path)
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
except Exception as e:
|
58 |
return jsonify({'error': str(e)}), 500
|
59 |
finally:
|
@@ -82,12 +94,19 @@ def handle_scan_bills():
|
|
82 |
return jsonify({'error': 'No selected file'}), 400
|
83 |
temp_path = 'temp_image.jpg'
|
84 |
file.save(temp_path)
|
|
|
85 |
try:
|
86 |
result = scan_bills(temp_path)
|
87 |
-
|
88 |
-
|
|
|
89 |
result_dict = {}
|
90 |
-
result_dict[
|
|
|
|
|
|
|
|
|
|
|
91 |
return jsonify(result_dict)
|
92 |
except Exception as e:
|
93 |
return jsonify({'error': str(e)}), 500
|
|
|
49 |
file = request.files['file']
|
50 |
if file.filename == '':
|
51 |
return jsonify({'error': 'No selected file'}), 400
|
52 |
+
|
53 |
+
mood = request.form.get('mood', 'neutral')
|
54 |
+
|
55 |
temp_path = 'temp_audio.wav'
|
56 |
file.save(temp_path)
|
57 |
try:
|
58 |
transcription = transcribe_file(temp_path)
|
59 |
+
result = classify_transaction(transcription)
|
60 |
+
|
61 |
+
message = chat(mood, transcription)
|
62 |
+
result_dict = {
|
63 |
+
"description": result['description'],
|
64 |
+
"amount": result['amount'],
|
65 |
+
"category": result['category'],
|
66 |
+
"chat_response": message
|
67 |
+
}
|
68 |
+
return jsonify(result_dict)
|
69 |
except Exception as e:
|
70 |
return jsonify({'error': str(e)}), 500
|
71 |
finally:
|
|
|
94 |
return jsonify({'error': 'No selected file'}), 400
|
95 |
temp_path = 'temp_image.jpg'
|
96 |
file.save(temp_path)
|
97 |
+
mood = request.form.get('mood', 'neutral')
|
98 |
try:
|
99 |
result = scan_bills(temp_path)
|
100 |
+
values = result.split(':')
|
101 |
+
values = [v.strip() for v in values]
|
102 |
+
|
103 |
result_dict = {}
|
104 |
+
result_dict["description"] = values[0]
|
105 |
+
result_dict["category"] = values[1]
|
106 |
+
result_dict["amount"] = values[2]
|
107 |
+
|
108 |
+
message = chat(mood, result)
|
109 |
+
result_dict["chat_response"] = message
|
110 |
return jsonify(result_dict)
|
111 |
except Exception as e:
|
112 |
return jsonify({'error': str(e)}), 500
|
chatbot/__pycache__/chatbot.cpython-312.pyc
CHANGED
Binary files a/chatbot/__pycache__/chatbot.cpython-312.pyc and b/chatbot/__pycache__/chatbot.cpython-312.pyc differ
|
|
chatbot/__pycache__/moods.cpython-312.pyc
CHANGED
Binary files a/chatbot/__pycache__/moods.cpython-312.pyc and b/chatbot/__pycache__/moods.cpython-312.pyc differ
|
|
chatbot/chatbot.py
CHANGED
@@ -24,31 +24,67 @@ def extract_amount(prompt):
|
|
24 |
if match.group(2):
|
25 |
amount *= 1000
|
26 |
return amount
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
def classify_transaction(prompt):
|
30 |
if not prompt:
|
31 |
return {'amount': None, 'category': TransactionCategory.UNKNOWN.value}
|
32 |
amount = extract_amount(prompt)
|
33 |
-
|
34 |
f" Dựa vào mô tả giao dịch từ người dùng {prompt}, hãy xác định xem đây thuộc loại giao dịch nào trong danh sách này: {categories} "
|
35 |
"Ví dụ, nếu mô tả là 'Ăn sáng 20k' thì có nghĩa là đây là giao dịch thuộc loại 'Ăn uống' với số tiền là 20.000đ "
|
36 |
"Nếu không xác định được, hãy cho nó vào danh mục 'Các chi phí khác' "
|
37 |
"Nếu hoàn toàn không phải bất cứ giao dịch thu hoặc chi nào, hãy trả lời 'Không xác định' "
|
38 |
-
"Chú ý: chỉ trả về tên loại giao dịch chứ không trả về số tiền hay bất cứ từ nào khác "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
)
|
40 |
try:
|
41 |
response = category_classification_model.generate_content(
|
42 |
-
contents=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
generation_config={
|
44 |
"temperature": 0.7,
|
45 |
"max_output_tokens": 100
|
46 |
}
|
47 |
)
|
48 |
category = response.text.strip()
|
|
|
49 |
except Exception as e:
|
50 |
category = f'Lỗi trong quá trình xử lý: {e}'
|
51 |
return {
|
|
|
52 |
"amount" : amount,
|
53 |
"category" : category
|
54 |
}
|
|
|
24 |
if match.group(2):
|
25 |
amount *= 1000
|
26 |
return amount
|
27 |
+
|
28 |
+
llm_amount_prompt = (
|
29 |
+
f"Người dùng đã mô tả giao dịch là: {prompt} "
|
30 |
+
"Bạn hãy trả về số tiền cho giao dịch này, ví dụ như 'Ăn sáng 20k' thì bạn trả về '20000' "
|
31 |
+
"Hoặc 'bốn mươi nghìn' thì bạn trả về '40000' "
|
32 |
+
"Nếu không xác định được, hãy cho nó vào danh mục 'Các chi phí khác' "
|
33 |
+
"Nếu hoàn toàn không phải bất cứ giao dịch thu hoặc chi nào, hãy trả lời 'Không xác định' "
|
34 |
+
"Chú ý: chỉ trả về số tiền chứ không trả về từ nào khác, Ví dụ: '20000' "
|
35 |
+
)
|
36 |
+
try:
|
37 |
+
response = category_classification_model.generate_content(
|
38 |
+
contents=llm_amount_prompt,
|
39 |
+
generation_config={
|
40 |
+
"temperature": 0.7,
|
41 |
+
"max_output_tokens": 100
|
42 |
+
}
|
43 |
+
)
|
44 |
+
amount = response.text.strip()
|
45 |
+
except Exception as e:
|
46 |
+
amount = f'Lỗi trong quá trình xử lý: {e}'
|
47 |
+
return amount
|
48 |
|
49 |
def classify_transaction(prompt):
|
50 |
if not prompt:
|
51 |
return {'amount': None, 'category': TransactionCategory.UNKNOWN.value}
|
52 |
amount = extract_amount(prompt)
|
53 |
+
llm_category_prompt = (
|
54 |
f" Dựa vào mô tả giao dịch từ người dùng {prompt}, hãy xác định xem đây thuộc loại giao dịch nào trong danh sách này: {categories} "
|
55 |
"Ví dụ, nếu mô tả là 'Ăn sáng 20k' thì có nghĩa là đây là giao dịch thuộc loại 'Ăn uống' với số tiền là 20.000đ "
|
56 |
"Nếu không xác định được, hãy cho nó vào danh mục 'Các chi phí khác' "
|
57 |
"Nếu hoàn toàn không phải bất cứ giao dịch thu hoặc chi nào, hãy trả lời 'Không xác định' "
|
58 |
+
"Chú ý: chỉ trả về tên loại giao dịch chứ không trả về số tiền hay bất cứ từ nào khác, Ví dụ: 'Ăn uống' "
|
59 |
+
)
|
60 |
+
llm_description_prompt = (
|
61 |
+
f"Người dùng đã mô tả giao dịch là: {prompt} "
|
62 |
+
"Bạn hãy trả về mô tả cho giao dịch này, ví dụ như 'Ăn sáng 20k' thì bạn trả về 'Ăn sáng' "
|
63 |
+
"Nếu không xác định được, hãy cho nó vào danh mục 'Các chi phí khác' "
|
64 |
+
"Nếu hoàn toàn không phải bất cứ giao dịch thu hoặc chi nào, hãy trả lời 'Không xác định' "
|
65 |
+
"Chú ý: chỉ trả về mô tả giao dịch chứ không trả về số tiền hay bất cứ từ nào khác, Ví dụ: 'Ăn sáng' "
|
66 |
)
|
67 |
try:
|
68 |
response = category_classification_model.generate_content(
|
69 |
+
contents=llm_category_prompt,
|
70 |
+
generation_config={
|
71 |
+
"temperature": 0.7,
|
72 |
+
"max_output_tokens": 100
|
73 |
+
}
|
74 |
+
)
|
75 |
+
response_description = category_classification_model.generate_content(
|
76 |
+
contents=llm_description_prompt,
|
77 |
generation_config={
|
78 |
"temperature": 0.7,
|
79 |
"max_output_tokens": 100
|
80 |
}
|
81 |
)
|
82 |
category = response.text.strip()
|
83 |
+
description = response_description.text.strip()
|
84 |
except Exception as e:
|
85 |
category = f'Lỗi trong quá trình xử lý: {e}'
|
86 |
return {
|
87 |
+
"description" : description,
|
88 |
"amount" : amount,
|
89 |
"category" : category
|
90 |
}
|
expense_forecast/__pycache__/Expense_Forecasting.cpython-312.pyc
CHANGED
Binary files a/expense_forecast/__pycache__/Expense_Forecasting.cpython-312.pyc and b/expense_forecast/__pycache__/Expense_Forecasting.cpython-312.pyc differ
|
|
read_image/__pycache__/scan_bills.cpython-312.pyc
CHANGED
Binary files a/read_image/__pycache__/scan_bills.cpython-312.pyc and b/read_image/__pycache__/scan_bills.cpython-312.pyc differ
|
|
read_image/scan_bills.py
CHANGED
@@ -14,9 +14,10 @@ genai.configure(api_key=API_KEY)
|
|
14 |
scan_model = genai.GenerativeModel(
|
15 |
model_name='gemini-2.0-flash',
|
16 |
system_instruction='Hãy giúp tôi quét hóa đơn và trích xuất thông tin từ hóa đơn này'
|
17 |
-
f'Hãy giúp tôi phân loại giao dịch từ danh sách có sẵn: {categories} và cho tôi biết số tiền của giao dịch đó'
|
18 |
-
'Ví
|
19 |
-
'Chỉ trả lời theo dạng: Tên danh mục: số tiền
|
|
|
20 |
)
|
21 |
|
22 |
def scan_bills(image_path):
|
|
|
14 |
scan_model = genai.GenerativeModel(
|
15 |
model_name='gemini-2.0-flash',
|
16 |
system_instruction='Hãy giúp tôi quét hóa đơn và trích xuất thông tin từ hóa đơn này'
|
17 |
+
f'Hãy giúp tôi phân loại giao dịch từ danh sách có sẵn: {categories} và cho tôi biết số tiền, mô tả của giao dịch đó'
|
18 |
+
'Ví dụ của phản hồi: Ăn bánh cuốn: Ăn uống: 200000'
|
19 |
+
'Chỉ trả lời theo dạng: Mô tả: Tên danh mục: số tiền'
|
20 |
+
'Hãy trả lời đúng theo mẫu trên và không thừa bất cứ từ nào khác'
|
21 |
)
|
22 |
|
23 |
def scan_bills(image_path):
|