Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -26,21 +26,21 @@ async def testv1(request: Request, background_tasks: BackgroundTasks):
|
|
26 |
body_data = await request.json()
|
27 |
|
28 |
print(body_data)
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
|
42 |
-
#
|
43 |
-
|
44 |
|
45 |
return {'data': 'Batch job is scheduled!'}
|
46 |
|
@@ -51,6 +51,55 @@ async def testv1(request: Request, background_tasks: BackgroundTasks):
|
|
51 |
return {'error': str(e)}
|
52 |
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
async def process_batch_job(dataset: Dict[str, Any], batch_job_id: str):
|
56 |
"""
|
@@ -74,10 +123,14 @@ async def process_batch_job(dataset: Dict[str, Any], batch_job_id: str):
|
|
74 |
|
75 |
openai_tasks = []
|
76 |
for ds in dataset.get('data'):
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
79 |
task = {
|
80 |
-
"custom_id": f"
|
81 |
"method": "POST",
|
82 |
"url": "/v1/chat/completions",
|
83 |
"body": {
|
@@ -87,10 +140,6 @@ async def process_batch_job(dataset: Dict[str, Any], batch_job_id: str):
|
|
87 |
"type": "json_object"
|
88 |
},
|
89 |
"messages": [
|
90 |
-
{
|
91 |
-
"role": "system",
|
92 |
-
"content": system_prompt
|
93 |
-
},
|
94 |
{
|
95 |
"role": "user",
|
96 |
"content": description
|
|
|
26 |
body_data = await request.json()
|
27 |
|
28 |
print(body_data)
|
29 |
+
# Create initial batch job record
|
30 |
+
save_data = {
|
31 |
+
'batch_job_id': f"batch_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}",
|
32 |
+
"batch_job_status": False,
|
33 |
+
"created_at": datetime.utcnow().isoformat()
|
34 |
+
}
|
35 |
|
36 |
+
response = (
|
37 |
+
supabase.table("batch_processing_details")
|
38 |
+
.insert(save_data)
|
39 |
+
.execute()
|
40 |
+
)
|
41 |
|
42 |
+
# Add processing to background tasks
|
43 |
+
background_tasks.add_task(process_batch_job, body_data, save_data['batch_job_id'])
|
44 |
|
45 |
return {'data': 'Batch job is scheduled!'}
|
46 |
|
|
|
51 |
return {'error': str(e)}
|
52 |
|
53 |
|
54 |
+
def receipt_radar_prompt(raw_text:str)->str:
|
55 |
+
insurance_response_structure = """
|
56 |
+
{
|
57 |
+
"insurance_type": "Classify it into 8 categories travel , health , term , vehicle, property,liability, life , buisness only .Try to find the closest possible based on the receipt text, if you don't understand the type classify it as others.",
|
58 |
+
"policy_details": {
|
59 |
+
"policyholder_name": "",
|
60 |
+
"policy_number": "",
|
61 |
+
"insurance_start_date": "",
|
62 |
+
"insurance_end_date": "",
|
63 |
+
"premium_amount": "",
|
64 |
+
"payment_frequency": ""
|
65 |
+
},
|
66 |
+
"coverage_details": {
|
67 |
+
"covered_items": {
|
68 |
+
"item_type": "",
|
69 |
+
"product_company": "",
|
70 |
+
"product_model": "",
|
71 |
+
"product_manufacturing_year": ""
|
72 |
+
},
|
73 |
+
"comprehensive_coverage_type_policy": "yes/no"
|
74 |
+
}
|
75 |
+
}
|
76 |
+
"""
|
77 |
+
travel_response_structure = """
|
78 |
+
travel_type(bus,train,airplane,taxi,bike,rickshaw classify in these categories only strictly),travel_company_name , departure_destination , arrival_destination , arrival_city(if you are not able to find the arrival city add the arrival destination into this field strictly. ), departure_date,arrival_date .If the arrival and departure dates are the same from receipt text given to you analyse it properly to check that, then only use the same date in both the fields .if you don't find any field mark it as null.
|
79 |
+
"""
|
80 |
+
hotel_data_points = """ hotel_type(hotel_stay , dine_in , dine_in + stay(use both keyword strictly)), hotel_brand_name , hotel_location , hotel_checkin_date , hotel_checkout_date. if you don't find any field mark it as null """
|
81 |
+
system_prompt = f"""Extract information from the following receipt OCR text and return a JSON object with these exact keys: brand, total_cost, location, purchase_category, brand_category, Date, currency, filename, payment_method, metadata.
|
82 |
+
Rules:
|
83 |
+
1. For total_cost, use the highest monetary value in the text.
|
84 |
+
2. For brand_category, choose the closest match from: ["Fashion and Apparel", "Jewelry and Watches", "Beauty and Personal Care", "Automobiles", "Real Estate", "Travel(it may contain reciepts of airlines , trains , taxi ,cruise ,etc)", "Hospitality(it will include reciepts of Hotels (stays) , restaurants , cafe's , bar's , Accommodation Services , Beverages Services (don't include food delivery service in hospitality))","Food Delivery Services(like swiggy , zomato,eatsure and any other you can analyse from receipt text)", "Home and Lifestyle", "Technology and Electronics", "Sports and Leisure", "Art and Collectibles", "Health and Wellness", "Stationery and Writing Instruments", "Children and Baby", "Pet Accessories", "Financial Services", "Insurance"]
|
85 |
+
3. Format Date as dd-mm-yyyy.Strictly return the date in the format dd-mm-yyyy.
|
86 |
+
4. metadata: For insurance receipts extract the data points given in the JSON and return the JSON with structure: \n """ + insurance_response_structure + """
|
87 |
+
5.metadata : For travel receipts(flight ,bus,train) extract these data points as a JSON object exactly""" + travel_response_structure + """
|
88 |
+
6. metadata : For hotel receipts extract these data points as a JSON object exactly""" + hotel_data_points + f"""
|
89 |
+
For non-insurance and non-travel , non-hotel receipts, return metadata as null.
|
90 |
+
4. Use currency codes (e.g., USD, EUR) instead of symbols.
|
91 |
+
5. Generate filename as 'PURCHASE_TYPE_BRAND_DATE' (e.g., 'clothing_gucci_20230715').
|
92 |
+
6. If a value is not found, return null.
|
93 |
+
7. If all values are null, return null.
|
94 |
+
Ensure the strictly that output is a valid JSON object containing strictly the above keys, without any explanations.
|
95 |
+
Here's the OCR text below analyse it and convert into json using keys provided in first line and using the rules provided in rules section:
|
96 |
+
Generate a JSON response in the following format without using the ```json block. Ensure the output is properly formatted as plain text JSON.
|
97 |
+
{raw_text}
|
98 |
+
"""
|
99 |
+
return system_prompt
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
|
104 |
async def process_batch_job(dataset: Dict[str, Any], batch_job_id: str):
|
105 |
"""
|
|
|
123 |
|
124 |
openai_tasks = []
|
125 |
for ds in dataset.get('data'):
|
126 |
+
message_id = ds.get('message_id')
|
127 |
+
user_id = ds.get('user_id')
|
128 |
+
receipt_text = ds.get('receipt_text')
|
129 |
+
email = ds.get('email')
|
130 |
+
|
131 |
+
prompt =
|
132 |
task = {
|
133 |
+
"custom_id": f"{message_id}-{user_id}-{email}",
|
134 |
"method": "POST",
|
135 |
"url": "/v1/chat/completions",
|
136 |
"body": {
|
|
|
140 |
"type": "json_object"
|
141 |
},
|
142 |
"messages": [
|
|
|
|
|
|
|
|
|
143 |
{
|
144 |
"role": "user",
|
145 |
"content": description
|