Commit Β·
2d505ed
1
Parent(s): d27ed7a
updated python
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ from PIL import Image
|
|
| 7 |
import base64
|
| 8 |
import uvicorn
|
| 9 |
import os
|
|
|
|
| 10 |
|
| 11 |
from langchain_openai import ChatOpenAI
|
| 12 |
from langchain.prompts import ChatPromptTemplate
|
|
@@ -20,9 +21,9 @@ os.makedirs("receiptimages", exist_ok=True)
|
|
| 20 |
# Load trained YOLOv8n model
|
| 21 |
model = YOLO("receipt-scanner/models/best.pt")
|
| 22 |
|
| 23 |
-
# Init LangChain with
|
| 24 |
llm = ChatOpenAI(
|
| 25 |
-
model="gpt-
|
| 26 |
temperature=0,
|
| 27 |
)
|
| 28 |
|
|
@@ -39,7 +40,7 @@ Return your answer strictly as JSON with keys: venue, date, total, category.
|
|
| 39 |
""")
|
| 40 |
|
| 41 |
def run_langchain_with_image(image_path: str):
|
| 42 |
-
"""Send a cropped receipt to GPT
|
| 43 |
with open(image_path, "rb") as f:
|
| 44 |
b64_image = base64.b64encode(f.read()).decode("utf-8")
|
| 45 |
|
|
@@ -49,6 +50,12 @@ def run_langchain_with_image(image_path: str):
|
|
| 49 |
{"type": "image_url", "image_url": f"data:image/jpeg;base64,{b64_image}"}
|
| 50 |
]
|
| 51 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
return result.content
|
| 53 |
|
| 54 |
@app.post("/process-receipt")
|
|
@@ -82,6 +89,9 @@ async def process_receipt(file: UploadFile = File(...)):
|
|
| 82 |
"extracted": llm_result
|
| 83 |
})
|
| 84 |
|
|
|
|
|
|
|
|
|
|
| 85 |
return {"status": "success", "receipts": outputs}
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
|
|
|
| 7 |
import base64
|
| 8 |
import uvicorn
|
| 9 |
import os
|
| 10 |
+
import json
|
| 11 |
|
| 12 |
from langchain_openai import ChatOpenAI
|
| 13 |
from langchain.prompts import ChatPromptTemplate
|
|
|
|
| 21 |
# Load trained YOLOv8n model
|
| 22 |
model = YOLO("receipt-scanner/models/best.pt")
|
| 23 |
|
| 24 |
+
# Init LangChain with OpenAI (β
fixed model name)
|
| 25 |
llm = ChatOpenAI(
|
| 26 |
+
model="gpt-4o-mini", # use a valid model
|
| 27 |
temperature=0,
|
| 28 |
)
|
| 29 |
|
|
|
|
| 40 |
""")
|
| 41 |
|
| 42 |
def run_langchain_with_image(image_path: str):
|
| 43 |
+
"""Send a cropped receipt to GPT via LangChain and log details."""
|
| 44 |
with open(image_path, "rb") as f:
|
| 45 |
b64_image = base64.b64encode(f.read()).decode("utf-8")
|
| 46 |
|
|
|
|
| 50 |
{"type": "image_url", "image_url": f"data:image/jpeg;base64,{b64_image}"}
|
| 51 |
]
|
| 52 |
})
|
| 53 |
+
|
| 54 |
+
# π₯ Debug logging
|
| 55 |
+
print(f"πΌοΈ Sent {len(b64_image)} base64 chars to GPT for {image_path}")
|
| 56 |
+
print(f"π€ GPT raw response: {result}")
|
| 57 |
+
print(f"π Extracted content: {result.content}")
|
| 58 |
+
|
| 59 |
return result.content
|
| 60 |
|
| 61 |
@app.post("/process-receipt")
|
|
|
|
| 89 |
"extracted": llm_result
|
| 90 |
})
|
| 91 |
|
| 92 |
+
# π₯ Log the full outputs for debugging
|
| 93 |
+
print("π¦ Final API response:", json.dumps(outputs, indent=2))
|
| 94 |
+
|
| 95 |
return {"status": "success", "receipts": outputs}
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|