alyex commited on
Commit
df66e3b
·
verified ·
1 Parent(s): 8fd2b13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -39
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app_minimal.py - TEST VERSION
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
- HF_DATASET_REPO = "alyex/karnak-data-app"
 
10
 
11
- with gr.Blocks(title="⚡ Minimal Test") as demo:
12
- gr.Markdown("# 🧪 Testing Dataset Access")
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- kiu_input = gr.Textbox(label="Enter KIU ID", value="00001")
15
- fetch_btn = gr.Button("Fetch Data")
16
 
17
- image_output = gr.Image(label="Image")
18
- html_output = gr.HTML(label="HTML Preview")
19
- status = gr.Markdown()
20
 
21
- def fetch_data(kiu_id):
22
- kiu_id = kiu_id.zfill(5)
 
 
 
 
23
 
24
- try:
25
- # Load dataset
26
- dataset = load_dataset(HF_DATASET_REPO, streaming=True)
27
-
28
- # Find the KIU
29
- found = False
30
- html_content = ""
31
- image_data = None
32
-
33
- for item in dataset["train"].take(100): # Limit search
34
- if item["kiu_id"] == kiu_id:
35
- found = True
36
- html_content = item.get("html", "No HTML")
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
- except Exception as e:
51
- return None, f"Error: {str(e)}", f"❌ Failed: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
- fetch_btn.click(fetch_data, inputs=[kiu_input], outputs=[image_output, html_output, status])
 
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)