Abid Ali Awan commited on
Commit
18b2e82
·
1 Parent(s): e29548e

Add logging functionality and configuration to app.py; update file handling and supported formats

Browse files
Files changed (2) hide show
  1. .gitignore +2 -0
  2. src/app.py +83 -31
.gitignore CHANGED
@@ -195,3 +195,5 @@ cython_debug/
195
 
196
  ./temp
197
  .gradio
 
 
 
195
 
196
  ./temp
197
  .gradio
198
+ .claude
199
+ CLAUDE.md
src/app.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
 
2
  from pathlib import Path
3
- from typing import *
4
  from urllib.parse import urlparse
5
 
6
  import convertapi
@@ -8,8 +9,46 @@ import gradio as gr
8
  from dotenv import load_dotenv
9
 
10
  load_dotenv()
 
 
 
 
 
 
 
 
11
  convertapi.api_credentials = os.getenv("CONVERTAPI_TOKEN")
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  # Function to convert a local file
15
  def convert_local_file(local_file_path: str, output_format: str) -> str:
@@ -24,10 +63,28 @@ def convert_local_file(local_file_path: str, output_format: str) -> str:
24
  str: The path to the converted file or an error message.
25
  """
26
  try:
 
 
 
27
  if not local_file_path or not os.path.exists(local_file_path):
28
- return "Invalid or non-existent local file path."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  # Convert the local file
 
31
  result = convertapi.convert(output_format, {"File": local_file_path})
32
 
33
  # Derive filename from local path
@@ -35,16 +92,19 @@ def convert_local_file(local_file_path: str, output_format: str) -> str:
35
 
36
  # Save the converted file to a temporary location
37
  output_filename = f"{input_filename}.{output_format}"
38
- output_path = Path("temp") / output_filename
39
  output_path.parent.mkdir(exist_ok=True)
40
 
41
  result.file.save(str(output_path))
 
42
 
43
  # Return the file path string on success
44
  return str(output_path)
45
 
46
  except Exception as e:
47
- return f"Error converting file: {str(e)}"
 
 
48
 
49
 
50
  # Function to convert a file from a URL
@@ -60,13 +120,24 @@ def convert_from_url(document_url: str, output_format: str) -> str:
60
  str: The path to the converted file or an error message.
61
  """
62
  try:
 
 
 
63
  if not document_url or not (
64
  document_url.lower().startswith("http://")
65
  or document_url.lower().startswith("https://")
66
  ):
67
- return "Invalid or unsupported URL format."
 
 
 
 
 
 
 
68
 
69
  # Convert the file from a URL
 
70
  result = convertapi.convert(output_format, {"File": document_url})
71
 
72
  # Attempt to derive a filename from the URL for saving
@@ -80,16 +151,19 @@ def convert_from_url(document_url: str, output_format: str) -> str:
80
 
81
  # Save the converted file to a temporary location
82
  output_filename = f"{input_filename}.{output_format}"
83
- output_path = Path("temp") / output_filename
84
  output_path.parent.mkdir(exist_ok=True)
85
 
86
  result.file.save(str(output_path))
 
87
 
88
  # Return the file path string on success
89
  return str(output_path)
90
 
91
  except Exception as e:
92
- return f"Error converting file from URL: {str(e)}"
 
 
93
 
94
 
95
  # Create Gradio interfaces
@@ -98,18 +172,7 @@ local_file_demo = gr.Interface(
98
  inputs=[
99
  gr.File(label="Upload File"),
100
  gr.Dropdown(
101
- choices=[
102
- "docx",
103
- "pdf",
104
- "txt",
105
- "png",
106
- "jpg",
107
- "jpeg",
108
- "xlsx",
109
- "pptx",
110
- "html",
111
- "rtf",
112
- ],
113
  label="Output Format",
114
  value="pdf",
115
  ),
@@ -127,18 +190,7 @@ url_demo = gr.Interface(
127
  value="",
128
  ),
129
  gr.Dropdown(
130
- choices=[
131
- "docx",
132
- "pdf",
133
- "txt",
134
- "png",
135
- "jpg",
136
- "jpeg",
137
- "xlsx",
138
- "pptx",
139
- "html",
140
- "rtf",
141
- ],
142
  label="Output Format",
143
  value="pdf",
144
  ),
 
1
  import os
2
+ import logging
3
  from pathlib import Path
4
+ from typing import Optional
5
  from urllib.parse import urlparse
6
 
7
  import convertapi
 
9
  from dotenv import load_dotenv
10
 
11
  load_dotenv()
12
+
13
+ # Setup logging
14
+ logging.basicConfig(
15
+ level=logging.INFO,
16
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
17
+ )
18
+ logger = logging.getLogger(__name__)
19
+
20
  convertapi.api_credentials = os.getenv("CONVERTAPI_TOKEN")
21
 
22
+ if not convertapi.api_credentials:
23
+ logger.error("CONVERTAPI_TOKEN not found in environment variables")
24
+ raise ValueError("CONVERTAPI_TOKEN is required")
25
+
26
+ # Configuration constants
27
+ MAX_FILE_SIZE = int(os.getenv("MAX_FILE_SIZE", 10 * 1024 * 1024)) # 10MB default
28
+ TEMP_DIR = os.getenv("TEMP_DIR", "temp")
29
+ LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
30
+
31
+ # Update logging level from config
32
+ logger.setLevel(getattr(logging, LOG_LEVEL.upper()))
33
+
34
+ # Get all supported formats from ConvertAPI
35
+ def get_supported_formats():
36
+ try:
37
+ # Common formats that ConvertAPI supports
38
+ return [
39
+ "pdf", "docx", "doc", "txt", "rtf", "odt", "html", "epub",
40
+ "png", "jpg", "jpeg", "gif", "bmp", "tiff", "webp", "svg",
41
+ "xlsx", "xls", "csv", "ods",
42
+ "pptx", "ppt", "odp",
43
+ "mp4", "avi", "mov", "wmv", "flv", "webm",
44
+ "mp3", "wav", "flac", "aac", "ogg"
45
+ ]
46
+ except Exception:
47
+ # Fallback to basic formats
48
+ return ["pdf", "docx", "txt", "png", "jpg", "xlsx", "pptx"]
49
+
50
+ SUPPORTED_FORMATS = get_supported_formats()
51
+
52
 
53
  # Function to convert a local file
54
  def convert_local_file(local_file_path: str, output_format: str) -> str:
 
63
  str: The path to the converted file or an error message.
64
  """
65
  try:
66
+ logger.info(f"Starting conversion of {local_file_path} to {output_format}")
67
+
68
+ # Input validation
69
  if not local_file_path or not os.path.exists(local_file_path):
70
+ error_msg = "Invalid or non-existent local file path."
71
+ logger.error(error_msg)
72
+ return error_msg
73
+
74
+ if output_format not in SUPPORTED_FORMATS:
75
+ error_msg = f"Unsupported output format: {output_format}"
76
+ logger.error(error_msg)
77
+ return error_msg
78
+
79
+ # Check file size
80
+ file_size = os.path.getsize(local_file_path)
81
+ if file_size > MAX_FILE_SIZE:
82
+ error_msg = f"File too large: {file_size / (1024*1024):.1f}MB (max {MAX_FILE_SIZE / (1024*1024):.0f}MB)"
83
+ logger.error(error_msg)
84
+ return error_msg
85
 
86
  # Convert the local file
87
+ logger.info(f"Converting file: {local_file_path}")
88
  result = convertapi.convert(output_format, {"File": local_file_path})
89
 
90
  # Derive filename from local path
 
92
 
93
  # Save the converted file to a temporary location
94
  output_filename = f"{input_filename}.{output_format}"
95
+ output_path = Path(TEMP_DIR) / output_filename
96
  output_path.parent.mkdir(exist_ok=True)
97
 
98
  result.file.save(str(output_path))
99
+ logger.info(f"Conversion successful: {output_path}")
100
 
101
  # Return the file path string on success
102
  return str(output_path)
103
 
104
  except Exception as e:
105
+ error_msg = f"Error converting file: {str(e)}"
106
+ logger.error(error_msg)
107
+ return error_msg
108
 
109
 
110
  # Function to convert a file from a URL
 
120
  str: The path to the converted file or an error message.
121
  """
122
  try:
123
+ logger.info(f"Starting URL conversion of {document_url} to {output_format}")
124
+
125
+ # Input validation
126
  if not document_url or not (
127
  document_url.lower().startswith("http://")
128
  or document_url.lower().startswith("https://")
129
  ):
130
+ error_msg = "Invalid or unsupported URL format."
131
+ logger.error(error_msg)
132
+ return error_msg
133
+
134
+ if output_format not in SUPPORTED_FORMATS:
135
+ error_msg = f"Unsupported output format: {output_format}"
136
+ logger.error(error_msg)
137
+ return error_msg
138
 
139
  # Convert the file from a URL
140
+ logger.info(f"Converting file from URL: {document_url}")
141
  result = convertapi.convert(output_format, {"File": document_url})
142
 
143
  # Attempt to derive a filename from the URL for saving
 
151
 
152
  # Save the converted file to a temporary location
153
  output_filename = f"{input_filename}.{output_format}"
154
+ output_path = Path(TEMP_DIR) / output_filename
155
  output_path.parent.mkdir(exist_ok=True)
156
 
157
  result.file.save(str(output_path))
158
+ logger.info(f"URL conversion successful: {output_path}")
159
 
160
  # Return the file path string on success
161
  return str(output_path)
162
 
163
  except Exception as e:
164
+ error_msg = f"Error converting file from URL: {str(e)}"
165
+ logger.error(error_msg)
166
+ return error_msg
167
 
168
 
169
  # Create Gradio interfaces
 
172
  inputs=[
173
  gr.File(label="Upload File"),
174
  gr.Dropdown(
175
+ choices=SUPPORTED_FORMATS,
 
 
 
 
 
 
 
 
 
 
 
176
  label="Output Format",
177
  value="pdf",
178
  ),
 
190
  value="",
191
  ),
192
  gr.Dropdown(
193
+ choices=SUPPORTED_FORMATS,
 
 
 
 
 
 
 
 
 
 
 
194
  label="Output Format",
195
  value="pdf",
196
  ),