zixuanvtzx commited on
Commit
8faccbf
·
verified ·
1 Parent(s): 2e21f56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -11
app.py CHANGED
@@ -5,7 +5,8 @@ import cv2
5
  import numpy as np
6
  import re
7
  import csv
8
- import io
 
9
 
10
  # Synonyms for features
11
  feature_synonyms = {
@@ -88,21 +89,18 @@ def ocr_extract(image):
88
  # Parse the extracted text
89
  parsed_data = parse_text(text)
90
 
91
- # Prepare CSV data as in-memory file
92
- output = io.StringIO()
93
- writer = csv.DictWriter(output, fieldnames=parsed_data.keys())
94
  writer.writeheader()
95
  writer.writerow(parsed_data)
96
- csv_str = output.getvalue()
97
- output.close()
98
 
99
- csv_bytes = io.BytesIO(csv_str.encode("utf-8"))
100
- csv_bytes.name = "extracted_bill.csv"
101
-
102
- # Prepare display text
103
  display_text = "\n".join(f"{k}: {v}" for k, v in parsed_data.items())
104
 
105
- return display_text, csv_bytes
106
 
107
  iface = gr.Interface(
108
  fn=ocr_extract,
 
5
  import numpy as np
6
  import re
7
  import csv
8
+ import tempfile
9
+ import os
10
 
11
  # Synonyms for features
12
  feature_synonyms = {
 
89
  # Parse the extracted text
90
  parsed_data = parse_text(text)
91
 
92
+ # Create a temp file to save CSV
93
+ tmpfile = tempfile.NamedTemporaryFile(delete=False, suffix=".csv", mode="w", encoding="utf-8")
94
+ writer = csv.DictWriter(tmpfile, fieldnames=parsed_data.keys())
95
  writer.writeheader()
96
  writer.writerow(parsed_data)
97
+ tmpfile_path = tmpfile.name
98
+ tmpfile.close()
99
 
100
+ # Prepare display text as before
 
 
 
101
  display_text = "\n".join(f"{k}: {v}" for k, v in parsed_data.items())
102
 
103
+ return display_text, tmpfile_path # Return path string, not BytesIO
104
 
105
  iface = gr.Interface(
106
  fn=ocr_extract,