Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -148,35 +148,35 @@ Use the correct currency and amounts for each invoice field.
|
|
| 148 |
For each line item, provide the Description, Tax Percentage, Tax Amount, and Amount.
|
| 149 |
If certain values are missing or not applicable, leave them empty or set them as null where necessary.
|
| 150 |
This JSON format will be used to store and manage invoices in a structured and uniform way. Please ensure only return JSON format. No extra content should not provide."""
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
|
| 181 |
# Dependency to check API Key
|
| 182 |
def verify_api_key(api_key: str = Header(...)):
|
|
@@ -194,7 +194,7 @@ def ocr_from_s3(api_key: str = Depends(verify_api_key),file_key: str = Query(...
|
|
| 194 |
"""
|
| 195 |
try:
|
| 196 |
# Fetch file from S3
|
| 197 |
-
file_data, content_type = fetch_file_from_s3_file(file_key)
|
| 198 |
|
| 199 |
extracted_text = []
|
| 200 |
base64Data = base64.b64encode(file_data).decode('utf-8')
|
|
@@ -244,8 +244,10 @@ def ocr_from_s3(api_key: str = Depends(verify_api_key),file_key: str = Query(...
|
|
| 244 |
summary = summarize_text(full_text)
|
| 245 |
|
| 246 |
return {
|
|
|
|
| 247 |
"file_key": file_key,
|
| 248 |
"file_type": content_type,
|
|
|
|
| 249 |
"base64DataResp":base64DataResp,
|
| 250 |
"extracted_text": full_text,
|
| 251 |
"summary": summary
|
|
|
|
| 148 |
For each line item, provide the Description, Tax Percentage, Tax Amount, and Amount.
|
| 149 |
If certain values are missing or not applicable, leave them empty or set them as null where necessary.
|
| 150 |
This JSON format will be used to store and manage invoices in a structured and uniform way. Please ensure only return JSON format. No extra content should not provide."""
|
| 151 |
+
try:
|
| 152 |
+
response = openai.ChatCompletion.create(
|
| 153 |
+
model="gpt-4o-mini",
|
| 154 |
+
messages=[
|
| 155 |
+
{"role": "system", "content": system_prompt},
|
| 156 |
+
{"role": "user", "content": f"{text}"}
|
| 157 |
+
],
|
| 158 |
+
temperature=0.5,
|
| 159 |
+
max_tokens=16384
|
| 160 |
+
)
|
| 161 |
+
content = response.choices[0].message.content.strip()
|
| 162 |
+
print("Before content:", content)
|
| 163 |
+
cleaned_content = re.sub(r'^.*```json\n', '', content) # Remove '```json\n' at the beginning
|
| 164 |
+
cleaned_content = re.sub(r'\n```$', '', cleaned_content) # Remove '\n```' at the end
|
| 165 |
+
|
| 166 |
+
# Step 2: Parse the cleaned content as JSON
|
| 167 |
+
#parsed_content = json.loads(cleaned_content)
|
| 168 |
+
|
| 169 |
+
# Step 3: Print the parsed JSON object
|
| 170 |
+
try:
|
| 171 |
+
parsed_content = json.loads(cleaned_content)
|
| 172 |
+
return parsed_content
|
| 173 |
+
except json.JSONDecodeError as e:
|
| 174 |
+
print("Error parsing JSON:", e)
|
| 175 |
+
# Optionally, print the cleaned content to debug
|
| 176 |
+
print("Cleaned content:", cleaned_content)
|
| 177 |
+
return None
|
| 178 |
+
except Exception as e:
|
| 179 |
+
return f"Error in summarization: {str(e)}"
|
| 180 |
|
| 181 |
# Dependency to check API Key
|
| 182 |
def verify_api_key(api_key: str = Header(...)):
|
|
|
|
| 194 |
"""
|
| 195 |
try:
|
| 196 |
# Fetch file from S3
|
| 197 |
+
file_data, content_type,document_type,entityrefkey = fetch_file_from_s3_file(file_key)
|
| 198 |
|
| 199 |
extracted_text = []
|
| 200 |
base64Data = base64.b64encode(file_data).decode('utf-8')
|
|
|
|
| 244 |
summary = summarize_text(full_text)
|
| 245 |
|
| 246 |
return {
|
| 247 |
+
"entityrefkey":entityrefkey
|
| 248 |
"file_key": file_key,
|
| 249 |
"file_type": content_type,
|
| 250 |
+
"document_type":document_type,
|
| 251 |
"base64DataResp":base64DataResp,
|
| 252 |
"extracted_text": full_text,
|
| 253 |
"summary": summary
|