anna-tch commited on
Commit
62ae541
·
1 Parent(s): a83bf0d

Add application file

Browse files
Files changed (1) hide show
  1. app.py +127 -14
app.py CHANGED
@@ -1,17 +1,130 @@
1
- from data_handler import load_dataset, load_progress
2
- from ui_handler import create_ui
3
-
4
- # Load dataset
5
- dataset = load_dataset()
6
- if dataset is None:
7
- raise RuntimeError("Failed to load dataset. Check your Hugging Face token.")
8
-
9
- # Load annotation progress
10
- current_index = load_progress()
11
-
12
- # Create UI and launch
13
- demo = create_ui(dataset, current_index)
14
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  # import gradio as gr
17
  # import datasets
 
1
+ import gradio as gr
2
+ from datasets import load_dataset
3
+ from datasets import Dataset
4
+ import os
5
+ import csv
6
+
7
+ # Configuration
8
+ DATASET_NAME = "anna-tch/generation-results" # Update this
9
+ PROGRESS_FILE = "progress.csv"
10
+ HF_TOKEN = os.getenv("HF_TOKEN") # Set your Hugging Face token
11
+
12
+ # Load dataset and prepare initial state
13
+ def load_data():
14
+ dataset = load_dataset(DATASET_NAME, use_auth_token=HF_TOKEN)['train']
15
+ unannotated = [i for i, ex in enumerate(dataset) if ex['annotation'] is None]
16
+ return {
17
+ "dataset": dataset,
18
+ "unannotated_indices": unannotated,
19
+ "current_index": 0,
20
+ "annotations": {}
21
+ }
22
+
23
+ def save_progress(annotation_data):
24
+ with open(PROGRESS_FILE, 'a', newline='') as f:
25
+ writer = csv.writer(f)
26
+ writer.writerow([
27
+ annotation_data['id'],
28
+ annotation_data['grammar'],
29
+ annotation_data['coherence']
30
+ ])
31
+
32
+ def update_dataset(state):
33
+ state["dataset"].push_to_hub(DATASET_NAME, token=HF_TOKEN)
34
+
35
+ def get_current_example(state):
36
+ idx = state["unannotated_indices"][state["current_index"]]
37
+ example = state["dataset"][idx]
38
+ return example, idx
39
+
40
+ def update_display(state):
41
+ example, idx = get_current_example(state)
42
+ annotation = state["annotations"].get(idx, {"grammar": None, "coherence": None})
43
+ return (
44
+ example["generated_text"],
45
+ annotation["grammar"],
46
+ annotation["coherence"],
47
+ f"Example {state['current_index'] + 1} of {len(state['unannotated_indices'])}"
48
+ )
49
+
50
+ def next_example(state):
51
+ if state["current_index"] < len(state["unannotated_indices"]) - 1:
52
+ state["current_index"] += 1
53
+ return update_display(state), state
54
+
55
+ def prev_example(state):
56
+ if state["current_index"] > 0:
57
+ state["current_index"] -= 1
58
+ return update_display(state), state
59
+
60
+ def submit(grammar, coherence, state):
61
+ example, idx = get_current_example(state)
62
+
63
+ # Save annotation
64
+ annotation = {
65
+ "id": idx,
66
+ "grammar": grammar,
67
+ "coherence": coherence
68
+ }
69
+ state["annotations"][idx] = annotation
70
+ save_progress(annotation)
71
+
72
+ # Update dataset
73
+ state["dataset"] = state["dataset"].map(
74
+ lambda ex, idx: {"annotation": {"grammar": grammar, "coherence": coherence}
75
+ if idx == annotation["id"] else ex},
76
+ with_indices=True
77
+ )
78
+ update_dataset(state)
79
+
80
+ # Move to next example
81
+ return next_example(state)
82
+
83
+ with gr.Blocks() as app:
84
+ state = gr.State(load_data)
85
+
86
+ gr.Markdown("## Text Annotation Tool")
87
+
88
+ with gr.Row():
89
+ counter = gr.Markdown()
90
+ text_display = gr.Textbox(label="Generated Text", interactive=False)
91
+ with gr.Row():
92
+ grammar = gr.Radio(choices=[1, 2, 3, 4, 5], label="Grammar Score")
93
+ coherence = gr.Radio(choices=[1, 2, 3, 4, 5], label="Coherence Score")
94
+
95
+ with gr.Row():
96
+ prev_btn = gr.Button("Previous")
97
+ next_btn = gr.Button("Next")
98
+ submit_btn = gr.Button("Submit")
99
+
100
+ # Event handlers
101
+ prev_btn.click(
102
+ prev_example,
103
+ inputs=[state],
104
+ outputs=[text_display, grammar, coherence, counter, state]
105
+ )
106
+
107
+ next_btn.click(
108
+ next_example,
109
+ inputs=[state],
110
+ outputs=[text_display, grammar, coherence, counter, state]
111
+ )
112
+
113
+ submit_btn.click(
114
+ submit,
115
+ inputs=[grammar, coherence, state],
116
+ outputs=[text_display, grammar, coherence, counter, state]
117
+ )
118
+
119
+ # Initial load
120
+ app.load(
121
+ update_display,
122
+ inputs=[state],
123
+ outputs=[text_display, grammar, coherence, counter]
124
+ )
125
+
126
+ if __name__ == "__main__":
127
+ app.launch()
128
 
129
  # import gradio as gr
130
  # import datasets