Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import importlib
|
| 3 |
from PIL import Image
|
| 4 |
import json
|
|
|
|
| 5 |
import spaces
|
| 6 |
|
| 7 |
# === Model Mapping ===
|
|
@@ -142,25 +143,28 @@ def format_pretty_view(output):
|
|
| 142 |
def process_single_image(model_name, image_file):
|
| 143 |
runner = load_model_runner(model_name)
|
| 144 |
image = Image.open(image_file.name).convert("RGB")
|
| 145 |
-
|
| 146 |
|
|
|
|
| 147 |
parsed_json = result.get("json")
|
| 148 |
raw_text = result.get("raw", "")
|
| 149 |
|
| 150 |
if parsed_json:
|
| 151 |
-
json_output =
|
| 152 |
pretty_output = format_pretty_view(parsed_json)
|
| 153 |
|
| 154 |
-
|
| 155 |
-
with
|
| 156 |
-
json.dump(parsed_json,
|
| 157 |
-
json_path = tmp.name
|
| 158 |
else:
|
| 159 |
json_output = "(No valid JSON extracted)"
|
| 160 |
pretty_output = "(No structured content extracted)\n\n⚠️ Raw Model Output:\n" + raw_text
|
| 161 |
-
json_path = None
|
| 162 |
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
# === Gradio UI
|
| 166 |
iface = gr.Interface(
|
|
|
|
| 2 |
import importlib
|
| 3 |
from PIL import Image
|
| 4 |
import json
|
| 5 |
+
import tempfile
|
| 6 |
import spaces
|
| 7 |
|
| 8 |
# === Model Mapping ===
|
|
|
|
| 143 |
def process_single_image(model_name, image_file):
|
| 144 |
runner = load_model_runner(model_name)
|
| 145 |
image = Image.open(image_file.name).convert("RGB")
|
| 146 |
+
base_name = os.path.splitext(os.path.basename(image_file.name))[0]
|
| 147 |
|
| 148 |
+
result = runner(image)
|
| 149 |
parsed_json = result.get("json")
|
| 150 |
raw_text = result.get("raw", "")
|
| 151 |
|
| 152 |
if parsed_json:
|
| 153 |
+
json_output = json.dumps(parsed_json, indent=2)
|
| 154 |
pretty_output = format_pretty_view(parsed_json)
|
| 155 |
|
| 156 |
+
tmp_path = os.path.join(tempfile.gettempdir(), f"{base_name}_output.json")
|
| 157 |
+
with open(tmp_path, "w", encoding="utf-8") as f:
|
| 158 |
+
json.dump(parsed_json, f, indent=2)
|
|
|
|
| 159 |
else:
|
| 160 |
json_output = "(No valid JSON extracted)"
|
| 161 |
pretty_output = "(No structured content extracted)\n\n⚠️ Raw Model Output:\n" + raw_text
|
|
|
|
| 162 |
|
| 163 |
+
tmp_path = os.path.join(tempfile.gettempdir(), f"{base_name}_output.txt")
|
| 164 |
+
with open(tmp_path, "w", encoding="utf-8") as f:
|
| 165 |
+
f.write(raw_text)
|
| 166 |
+
|
| 167 |
+
return image, json_output, pretty_output, tmp_path
|
| 168 |
|
| 169 |
# === Gradio UI
|
| 170 |
iface = gr.Interface(
|