Spaces:
Runtime error
Runtime error
Sandy2636
commited on
Commit
ยท
6a6e280
1
Parent(s):
4b54b36
Update space
Browse files
app.py
CHANGED
@@ -4,12 +4,12 @@ import base64
|
|
4 |
import os
|
5 |
import json
|
6 |
import mimetypes
|
7 |
-
|
8 |
# --- Configuration ---
|
9 |
# IMPORTANT: Set your OPENROUTER_API_KEY as an environment variable
|
10 |
# For example, in your terminal: export OPENROUTER_API_KEY='your_key_here'
|
11 |
-
OPENROUTER_API_KEY = "
|
12 |
-
IMAGE_MODEL = "opengvlab/internvl3-14b
|
13 |
OPENROUTER_API_URL = "https://openrouter.ai/api/v1/chat/completions"
|
14 |
|
15 |
# --- Application State ---
|
@@ -114,7 +114,7 @@ def process_single_image_with_openrouter(image_path, doc_type):
|
|
114 |
|
115 |
if "choices" in result and result["choices"]:
|
116 |
content_text = result["choices"][0]["message"]["content"]
|
117 |
-
|
118 |
# Try to clean up and parse JSON (models sometimes wrap in markdown)
|
119 |
clean_content = content_text.strip()
|
120 |
if clean_content.startswith("```json"):
|
@@ -123,7 +123,7 @@ def process_single_image_with_openrouter(image_path, doc_type):
|
|
123 |
clean_content = clean_content[:-3]
|
124 |
elif clean_content.startswith("`") and clean_content.endswith("`"): # Single backtick
|
125 |
clean_content = clean_content[1:-1]
|
126 |
-
|
127 |
try:
|
128 |
parsed_json = json.loads(clean_content)
|
129 |
# Ensure document_type_provided is in the root, even if LLM missed it
|
@@ -165,11 +165,11 @@ def add_document_to_batch_ui(image_filepath, doc_type_selection):
|
|
165 |
# It should be used relatively quickly. For long-lived state,
|
166 |
# you might copy the file or read its content.
|
167 |
current_batch.append({"path": image_filepath, "type": doc_type_selection, "filename": filename})
|
168 |
-
|
169 |
# Prepare display for Dataframe: list of lists
|
170 |
batch_display_data = [[item["filename"], item["type"]] for item in current_batch]
|
171 |
return batch_display_data, f"Added '{filename}' as '{doc_type_selection}'."
|
172 |
-
|
173 |
# Return current state if inputs are invalid
|
174 |
batch_display_data = [[item["filename"], item["type"]] for item in current_batch]
|
175 |
return batch_display_data, "Failed to add: Image or document type missing."
|
@@ -191,13 +191,6 @@ def process_batch_ui():
|
|
191 |
status_msg = f"Processing document {i+1}/{len(current_batch)}: {item_to_process['filename']} ({item_to_process['type']})..."
|
192 |
print(status_msg)
|
193 |
# yield None, status_msg # This would require process_batch_ui to be a generator for live updates
|
194 |
-
|
195 |
-
# Ensure the file path is valid; Gradio's temp files should be okay here
|
196 |
-
# if not os.path.exists(item_to_process["path"]):
|
197 |
-
# error_res = {"error": f"File not found: {item_to_process['filename']}. It might have been a temporary file that was removed.", "document_type_provided": item_to_process['type']}
|
198 |
-
# all_results.append(error_res)
|
199 |
-
# status_updates.append(f"Error: File {item_to_process['filename']} not found.")
|
200 |
-
# continue
|
201 |
|
202 |
extracted_data = process_single_image_with_openrouter(item_to_process["path"], item_to_process["type"])
|
203 |
all_results.append(extracted_data)
|
@@ -235,16 +228,12 @@ def process_batch_ui():
|
|
235 |
grouped_by_person[doc_id]["documents"].append(result_item)
|
236 |
else:
|
237 |
unidentified_docs.append(result_item)
|
238 |
-
|
239 |
final_structured_output = {
|
240 |
"summary": f"Processed {len(current_batch)} documents.",
|
241 |
"grouped_by_person": list(grouped_by_person.values()) if grouped_by_person else [], # Convert dict to list for easier iteration in JSON
|
242 |
"unidentified_documents_or_errors": unidentified_docs
|
243 |
}
|
244 |
-
|
245 |
-
# Do not clear batch here, let user do it.
|
246 |
-
# current_batch = [] # Clears batch after processing
|
247 |
-
# batch_display_data = []
|
248 |
|
249 |
final_status = "Batch processing complete. " + " | ".join(status_updates)
|
250 |
print(final_status)
|
@@ -270,7 +259,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
270 |
"5. Click 'Process Batch and Extract Information' to send documents to the AI.\n"
|
271 |
"6. View the extracted information in JSON format below."
|
272 |
)
|
273 |
-
|
274 |
if not OPENROUTER_API_KEY:
|
275 |
gr.Markdown(
|
276 |
"<h3 style='color:red;'>โ ๏ธ Warning: `OPENROUTER_API_KEY` environment variable is not detected. "
|
@@ -304,16 +293,16 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
304 |
batch_dataframe = gr.Dataframe(
|
305 |
headers=["Filename", "Document Type"],
|
306 |
datatype=["str", "str"],
|
307 |
-
row_count=(0, "dynamic"),
|
308 |
col_count=(2, "fixed"),
|
309 |
-
wrap=True
|
310 |
-
height=380
|
311 |
)
|
312 |
clear_batch_button = gr.Button("๐๏ธ Clear Entire Batch", variant="stop")
|
313 |
|
314 |
gr.Markdown("### Step 3: Process Batch")
|
315 |
process_button = gr.Button("๐ Process Batch and Extract Information", variant="primary")
|
316 |
-
|
317 |
status_message_textbox = gr.Textbox(label="Processing Status", interactive=False, lines=2)
|
318 |
|
319 |
gr.Markdown("### Step 4: View Results")
|
@@ -344,5 +333,5 @@ if __name__ == "__main__":
|
|
344 |
print("Please set it before running the application, e.g.:")
|
345 |
print(" export OPENROUTER_API_KEY='your_openrouter_key_here'")
|
346 |
print("The application will launch, but API calls will fail.")
|
347 |
-
|
348 |
demo.launch()
|
|
|
4 |
import os
|
5 |
import json
|
6 |
import mimetypes
|
7 |
+
|
8 |
# --- Configuration ---
|
9 |
# IMPORTANT: Set your OPENROUTER_API_KEY as an environment variable
|
10 |
# For example, in your terminal: export OPENROUTER_API_KEY='your_key_here'
|
11 |
+
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
12 |
+
IMAGE_MODEL = "opengvlab/internvl3-14b"
|
13 |
OPENROUTER_API_URL = "https://openrouter.ai/api/v1/chat/completions"
|
14 |
|
15 |
# --- Application State ---
|
|
|
114 |
|
115 |
if "choices" in result and result["choices"]:
|
116 |
content_text = result["choices"][0]["message"]["content"]
|
117 |
+
|
118 |
# Try to clean up and parse JSON (models sometimes wrap in markdown)
|
119 |
clean_content = content_text.strip()
|
120 |
if clean_content.startswith("```json"):
|
|
|
123 |
clean_content = clean_content[:-3]
|
124 |
elif clean_content.startswith("`") and clean_content.endswith("`"): # Single backtick
|
125 |
clean_content = clean_content[1:-1]
|
126 |
+
|
127 |
try:
|
128 |
parsed_json = json.loads(clean_content)
|
129 |
# Ensure document_type_provided is in the root, even if LLM missed it
|
|
|
165 |
# It should be used relatively quickly. For long-lived state,
|
166 |
# you might copy the file or read its content.
|
167 |
current_batch.append({"path": image_filepath, "type": doc_type_selection, "filename": filename})
|
168 |
+
|
169 |
# Prepare display for Dataframe: list of lists
|
170 |
batch_display_data = [[item["filename"], item["type"]] for item in current_batch]
|
171 |
return batch_display_data, f"Added '{filename}' as '{doc_type_selection}'."
|
172 |
+
|
173 |
# Return current state if inputs are invalid
|
174 |
batch_display_data = [[item["filename"], item["type"]] for item in current_batch]
|
175 |
return batch_display_data, "Failed to add: Image or document type missing."
|
|
|
191 |
status_msg = f"Processing document {i+1}/{len(current_batch)}: {item_to_process['filename']} ({item_to_process['type']})..."
|
192 |
print(status_msg)
|
193 |
# yield None, status_msg # This would require process_batch_ui to be a generator for live updates
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
|
195 |
extracted_data = process_single_image_with_openrouter(item_to_process["path"], item_to_process["type"])
|
196 |
all_results.append(extracted_data)
|
|
|
228 |
grouped_by_person[doc_id]["documents"].append(result_item)
|
229 |
else:
|
230 |
unidentified_docs.append(result_item)
|
231 |
+
|
232 |
final_structured_output = {
|
233 |
"summary": f"Processed {len(current_batch)} documents.",
|
234 |
"grouped_by_person": list(grouped_by_person.values()) if grouped_by_person else [], # Convert dict to list for easier iteration in JSON
|
235 |
"unidentified_documents_or_errors": unidentified_docs
|
236 |
}
|
|
|
|
|
|
|
|
|
237 |
|
238 |
final_status = "Batch processing complete. " + " | ".join(status_updates)
|
239 |
print(final_status)
|
|
|
259 |
"5. Click 'Process Batch and Extract Information' to send documents to the AI.\n"
|
260 |
"6. View the extracted information in JSON format below."
|
261 |
)
|
262 |
+
|
263 |
if not OPENROUTER_API_KEY:
|
264 |
gr.Markdown(
|
265 |
"<h3 style='color:red;'>โ ๏ธ Warning: `OPENROUTER_API_KEY` environment variable is not detected. "
|
|
|
293 |
batch_dataframe = gr.Dataframe(
|
294 |
headers=["Filename", "Document Type"],
|
295 |
datatype=["str", "str"],
|
296 |
+
row_count=(0, "dynamic"),
|
297 |
col_count=(2, "fixed"),
|
298 |
+
wrap=True
|
299 |
+
# Removed: height=380 from here
|
300 |
)
|
301 |
clear_batch_button = gr.Button("๐๏ธ Clear Entire Batch", variant="stop")
|
302 |
|
303 |
gr.Markdown("### Step 3: Process Batch")
|
304 |
process_button = gr.Button("๐ Process Batch and Extract Information", variant="primary")
|
305 |
+
|
306 |
status_message_textbox = gr.Textbox(label="Processing Status", interactive=False, lines=2)
|
307 |
|
308 |
gr.Markdown("### Step 4: View Results")
|
|
|
333 |
print("Please set it before running the application, e.g.:")
|
334 |
print(" export OPENROUTER_API_KEY='your_openrouter_key_here'")
|
335 |
print("The application will launch, but API calls will fail.")
|
336 |
+
|
337 |
demo.launch()
|