Update app.py
Browse files
app.py
CHANGED
|
@@ -52,12 +52,18 @@ def extract_text_from_excel(file_obj: Union[str, os.PathLike, BinaryIO]) -> str:
|
|
| 52 |
"""Extract text from Excel file which can be a path, file-like object, or Gradio file object."""
|
| 53 |
all_text = []
|
| 54 |
try:
|
| 55 |
-
# Handle
|
| 56 |
-
if hasattr(file_obj, 'name'):
|
| 57 |
file_path = file_obj.name
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
file_path = file_obj
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
xls = pd.ExcelFile(file_path)
|
| 62 |
except Exception as e:
|
| 63 |
raise ValueError(f"β Error reading Excel file: {e}")
|
|
@@ -73,7 +79,6 @@ def extract_text_from_excel(file_obj: Union[str, os.PathLike, BinaryIO]) -> str:
|
|
| 73 |
continue
|
| 74 |
|
| 75 |
return "\n".join(all_text)
|
| 76 |
-
|
| 77 |
def split_text_into_chunks(text: str, max_tokens: int = MAX_CHUNK_TOKENS, max_chunks: int = 30) -> List[str]:
|
| 78 |
effective_max = max_tokens - PROMPT_OVERHEAD
|
| 79 |
lines, chunks, curr_chunk, curr_tokens = text.split("\n"), [], [], 0
|
|
@@ -143,6 +148,16 @@ def stream_report(agent, input_file: Union[str, BinaryIO], full_output: str) ->
|
|
| 143 |
yield "β Please upload a valid Excel file.", None, ""
|
| 144 |
return
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
text = extract_text_from_excel(input_file)
|
| 147 |
chunks = split_text_into_chunks(text)
|
| 148 |
|
|
@@ -184,7 +199,6 @@ def stream_report(agent, input_file: Union[str, BinaryIO], full_output: str) ->
|
|
| 184 |
|
| 185 |
except Exception as e:
|
| 186 |
yield f"β Error: {str(e)}", None, ""
|
| 187 |
-
|
| 188 |
def create_ui(agent):
|
| 189 |
with gr.Blocks(css="""
|
| 190 |
body {
|
|
|
|
| 52 |
"""Extract text from Excel file which can be a path, file-like object, or Gradio file object."""
|
| 53 |
all_text = []
|
| 54 |
try:
|
| 55 |
+
# Handle different file input types
|
| 56 |
+
if hasattr(file_obj, 'name'): # Gradio file object
|
| 57 |
file_path = file_obj.name
|
| 58 |
+
elif isinstance(file_obj, (str, os.PathLike)): # Path string or Path object
|
| 59 |
+
file_path = file_obj
|
| 60 |
+
else: # File-like object
|
| 61 |
file_path = file_obj
|
| 62 |
|
| 63 |
+
# Verify file exists
|
| 64 |
+
if not os.path.exists(file_path):
|
| 65 |
+
raise ValueError(f"File not found at path: {file_path}")
|
| 66 |
+
|
| 67 |
xls = pd.ExcelFile(file_path)
|
| 68 |
except Exception as e:
|
| 69 |
raise ValueError(f"β Error reading Excel file: {e}")
|
|
|
|
| 79 |
continue
|
| 80 |
|
| 81 |
return "\n".join(all_text)
|
|
|
|
| 82 |
def split_text_into_chunks(text: str, max_tokens: int = MAX_CHUNK_TOKENS, max_chunks: int = 30) -> List[str]:
|
| 83 |
effective_max = max_tokens - PROMPT_OVERHEAD
|
| 84 |
lines, chunks, curr_chunk, curr_tokens = text.split("\n"), [], [], 0
|
|
|
|
| 148 |
yield "β Please upload a valid Excel file.", None, ""
|
| 149 |
return
|
| 150 |
|
| 151 |
+
# Verify file exists before processing
|
| 152 |
+
if hasattr(input_file, 'name'):
|
| 153 |
+
if not os.path.exists(input_file.name):
|
| 154 |
+
yield "β The uploaded file could not be found. Please try uploading again.", None, ""
|
| 155 |
+
return
|
| 156 |
+
elif isinstance(input_file, str):
|
| 157 |
+
if not os.path.exists(input_file):
|
| 158 |
+
yield "β The specified file path does not exist.", None, ""
|
| 159 |
+
return
|
| 160 |
+
|
| 161 |
text = extract_text_from_excel(input_file)
|
| 162 |
chunks = split_text_into_chunks(text)
|
| 163 |
|
|
|
|
| 199 |
|
| 200 |
except Exception as e:
|
| 201 |
yield f"β Error: {str(e)}", None, ""
|
|
|
|
| 202 |
def create_ui(agent):
|
| 203 |
with gr.Blocks(css="""
|
| 204 |
body {
|