Svngoku commited on
Commit
468fb8d
·
verified ·
1 Parent(s): 8272d7b

Refactor the codebase

Browse files
Files changed (1) hide show
  1. app.py +21 -7
app.py CHANGED
@@ -40,6 +40,14 @@ def call_ocr_api(document):
40
  def call_chat_complete(model, messages, **kwargs):
41
  return client.chat.complete(model=model, messages=messages, **kwargs)
42
 
 
 
 
 
 
 
 
 
43
  # OCR with PDF URL
44
  def ocr_pdf_url(pdf_url):
45
  logger.info(f"Processing PDF URL: {pdf_url}")
@@ -57,18 +65,20 @@ def ocr_pdf_url(pdf_url):
57
 
58
  # OCR with Uploaded PDF
59
  def ocr_uploaded_pdf(pdf_file):
60
- logger.info(f"Processing uploaded PDF: {pdf_file.name}")
61
  temp_path = None
62
  try:
 
 
63
  # Use tempfile to handle uploaded file securely
64
  with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
65
- temp_file.write(pdf_file.read())
66
  temp_path = temp_file.name
67
  uploaded_pdf = client.files.upload(
68
  file={"file_name": temp_path, "content": open(temp_path, "rb")},
69
  purpose="ocr"
70
  )
71
- signed_url = client.files.get_signed_url(file_id=uploaded_pdf.id, expiry=7200) # Increased to 2 hours
72
  ocr_response = call_ocr_api({"type": "document_url", "document_url": signed_url.url})
73
  try:
74
  markdown = ocr_response.pages[0].markdown
@@ -100,11 +110,13 @@ def ocr_image_url(image_url):
100
 
101
  # OCR with Uploaded Image
102
  def ocr_uploaded_image(image_file):
103
- logger.info(f"Processing uploaded image: {image_file.name}")
104
  temp_path = None
105
  try:
 
 
106
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
107
- temp_file.write(image_file.read())
108
  temp_path = temp_file.name
109
  encoded_image = encode_image(temp_path)
110
  if "Error" in encoded_image:
@@ -164,11 +176,13 @@ class StructuredOCR(BaseModel):
164
  ocr_contents: dict
165
 
166
  def structured_ocr(image_file):
167
- logger.info(f"Processing structured OCR for image: {image_file.name}")
168
  temp_path = None
169
  try:
 
 
170
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
171
- temp_file.write(image_file.read())
172
  temp_path = temp_file.name
173
  image_path = Path(temp_path)
174
  encoded_image = encode_image(temp_path)
 
40
  def call_chat_complete(model, messages, **kwargs):
41
  return client.chat.complete(model=model, messages=messages, **kwargs)
42
 
43
+ # Helper function to get file content (handles both string paths and file-like objects)
44
+ def get_file_content(file_input):
45
+ if isinstance(file_input, str): # Gradio 3.x: file path
46
+ with open(file_input, "rb") as f:
47
+ return f.read()
48
+ else: # Gradio 4.x or file-like object
49
+ return file_input.read()
50
+
51
  # OCR with PDF URL
52
  def ocr_pdf_url(pdf_url):
53
  logger.info(f"Processing PDF URL: {pdf_url}")
 
65
 
66
  # OCR with Uploaded PDF
67
  def ocr_uploaded_pdf(pdf_file):
68
+ logger.info(f"Processing uploaded PDF: {getattr(pdf_file, 'name', 'unknown')}")
69
  temp_path = None
70
  try:
71
+ # Get file content (handles both string and file-like objects)
72
+ content = get_file_content(pdf_file)
73
  # Use tempfile to handle uploaded file securely
74
  with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
75
+ temp_file.write(content)
76
  temp_path = temp_file.name
77
  uploaded_pdf = client.files.upload(
78
  file={"file_name": temp_path, "content": open(temp_path, "rb")},
79
  purpose="ocr"
80
  )
81
+ signed_url = client.files.get_signed_url(file_id=uploaded_pdf.id, expiry=7200) # 2 hours
82
  ocr_response = call_ocr_api({"type": "document_url", "document_url": signed_url.url})
83
  try:
84
  markdown = ocr_response.pages[0].markdown
 
110
 
111
  # OCR with Uploaded Image
112
  def ocr_uploaded_image(image_file):
113
+ logger.info(f"Processing uploaded image: {getattr(image_file, 'name', 'unknown')}")
114
  temp_path = None
115
  try:
116
+ # Get file content (handles both string and file-like objects)
117
+ content = get_file_content(image_file)
118
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
119
+ temp_file.write(content)
120
  temp_path = temp_file.name
121
  encoded_image = encode_image(temp_path)
122
  if "Error" in encoded_image:
 
176
  ocr_contents: dict
177
 
178
  def structured_ocr(image_file):
179
+ logger.info(f"Processing structured OCR for image: {getattr(image_file, 'name', 'unknown')}")
180
  temp_path = None
181
  try:
182
+ # Get file content (handles both string and file-like objects)
183
+ content = get_file_content(image_file)
184
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
185
+ temp_file.write(content)
186
  temp_path = temp_file.name
187
  image_path = Path(temp_path)
188
  encoded_image = encode_image(temp_path)