Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
#
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
import json
|
|
@@ -6,50 +6,91 @@ from datasets import load_dataset
|
|
| 6 |
from PIL import Image
|
| 7 |
import io
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
status = gr.Markdown()
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
if item.get("image"):
|
| 39 |
-
image = Image.open(io.BytesIO(item["image"]))
|
| 40 |
-
image_data = image
|
| 41 |
-
|
| 42 |
-
break
|
| 43 |
-
|
| 44 |
-
if found:
|
| 45 |
-
html_display = f"<div style='padding:10px;background:#f0f0f0'>{html_content[:500]}...</div>"
|
| 46 |
-
return image_data, html_display, f"✅ Found KIU {kiu_id}"
|
| 47 |
-
else:
|
| 48 |
-
return None, "Not found", f"❌ KIU {kiu_id} not found in first 100 items"
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
# app_working.py
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
import json
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
import io
|
| 8 |
|
| 9 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
+
DATASET_REPO = "alyex/karnak-data-app"
|
| 11 |
|
| 12 |
+
# Simple state
|
| 13 |
+
current_index = 0
|
| 14 |
+
manifest = []
|
| 15 |
+
|
| 16 |
+
def load_test_manifest():
|
| 17 |
+
"""Create a test manifest"""
|
| 18 |
+
return [
|
| 19 |
+
{"kiu_id": "00001", "instance_id": 0, "crop_coords": [0, 0, 100, 100]},
|
| 20 |
+
{"kiu_id": "00002", "instance_id": 0, "crop_coords": [0, 0, 100, 100]},
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
def get_current_data():
|
| 24 |
+
"""Get data for current item"""
|
| 25 |
+
global current_index, manifest
|
| 26 |
|
| 27 |
+
if not manifest:
|
| 28 |
+
manifest = load_test_manifest()
|
| 29 |
|
| 30 |
+
if current_index >= len(manifest):
|
| 31 |
+
return None, "No items", "No data", ""
|
|
|
|
| 32 |
|
| 33 |
+
item = manifest[current_index]
|
| 34 |
+
kiu_id = item["kiu_id"]
|
| 35 |
+
|
| 36 |
+
# Try to fetch from dataset
|
| 37 |
+
try:
|
| 38 |
+
dataset = load_dataset(DATASET_REPO, streaming=True, token=HF_TOKEN)
|
| 39 |
|
| 40 |
+
for data_item in dataset["train"].take(100):
|
| 41 |
+
if data_item["kiu_id"] == kiu_id:
|
| 42 |
+
html = data_item.get("html", "No HTML")
|
| 43 |
+
image = None
|
| 44 |
+
|
| 45 |
+
if data_item.get("image"):
|
| 46 |
+
try:
|
| 47 |
+
image = Image.open(io.BytesIO(data_item["image"]))
|
| 48 |
+
# Apply crop
|
| 49 |
+
x1, y1, x2, y2 = item["crop_coords"]
|
| 50 |
+
image = image.crop((x1, y1, x2, y2))
|
| 51 |
+
except:
|
| 52 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
html_display = f"<div style='padding:10px'>{html[:1000]}...</div>"
|
| 55 |
+
info = f"KIU {kiu_id} | Instance {item['instance_id']}"
|
| 56 |
+
|
| 57 |
+
return image, html_display, info, ""
|
| 58 |
+
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return None, f"Error: {str(e)}", f"KIU {kiu_id}", ""
|
| 61 |
+
|
| 62 |
+
return None, "Not found in dataset", f"KIU {kiu_id}", ""
|
| 63 |
+
|
| 64 |
+
with gr.Blocks(title="⚡ Working App") as demo:
|
| 65 |
+
gr.Markdown("# ⚡ Hieroglyph Annotation")
|
| 66 |
+
|
| 67 |
+
image_display = gr.Image(label="Line Instance")
|
| 68 |
+
html_display = gr.HTML(label="Reference")
|
| 69 |
+
info_display = gr.Markdown()
|
| 70 |
+
line_input = gr.Textbox(label="Line Number")
|
| 71 |
+
|
| 72 |
+
next_btn = gr.Button("Next")
|
| 73 |
+
prev_btn = gr.Button("Previous")
|
| 74 |
+
|
| 75 |
+
def update_display():
|
| 76 |
+
return get_current_data()
|
| 77 |
+
|
| 78 |
+
def next_item():
|
| 79 |
+
global current_index
|
| 80 |
+
if current_index < len(manifest) - 1:
|
| 81 |
+
current_index += 1
|
| 82 |
+
return get_current_data()
|
| 83 |
+
|
| 84 |
+
def prev_item():
|
| 85 |
+
global current_index
|
| 86 |
+
if current_index > 0:
|
| 87 |
+
current_index -= 1
|
| 88 |
+
return get_current_data()
|
| 89 |
+
|
| 90 |
+
# Initial load
|
| 91 |
+
demo.load(update_display, outputs=[image_display, html_display, info_display, line_input])
|
| 92 |
|
| 93 |
+
next_btn.click(next_item, outputs=[image_display, html_display, info_display, line_input])
|
| 94 |
+
prev_btn.click(prev_item, outputs=[image_display, html_display, info_display, line_input])
|
| 95 |
|
| 96 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|