Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
225 lines (180 loc) · 8.83 KB
/
Copy pathapp.py
File metadata and controls
225 lines (180 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import storybear
demo = storybear.create_app()
demo.launch(max_file_size="15MB")
# import gradio as gr
# from pathlib import Path
# from time import sleep
# import numpy as np
# import pandas as pd
# from storybear.data import download_example_data
# from storybear.data_structures import ReportRecord, PlotRecord
# DATA_EXAMPLES = [
# ["hf://datasets/phihung/titanic/train.csv"],
# ["hf://datasets/latticetower/nrps_modules_asdb4.0/nrps_modules_info_cleaned.csv"]
# ]
# instances = {}
# def initialize_instance(request: gr.Request):
# tempdir = Path("temp")
# tempdir.mkdir(exist_ok=True)
# csv = tempdir / "smth.csv"
# download_example_data(csv)
# df = pd.read_csv(csv)
# instances[request.session_hash] = {'data_frame': df, 'report': None}
# return "Session initialized!"
# def cleanup_instance(request: gr.Request):
# if request.session_hash in instances:
# del instances[request.session_hash]
# def call_clear_checkboxes(*input):
# print("call clear", len(input))
# return [False]*len(input)
# class StageProcessor:
# def __init__(self, named_stages_list):
# self.named_stages_list = named_stages_list
# #self.stage_name = stage_name
# #self.stage_func = stage_func
# def __len__(self):
# return len(self.named_stages_list)
# def __getitem__(self, i):
# stage_name, stage_func = self.named_stages_list[i]
# def process_step(input_block, request: gr.Request, progress=gr.Progress()):
# if not input_block:
# return False
# # stage_name, stage_func = stage_info
# # increment_btn.click(increment_counter, inputs=None, outputs=counter)
# # print("before:", self.stage_name, self.current_value)
# # self.current_value = self.stage_func(self.current_value)
# if request.session_hash in instances:
# # Process data
# #instance = instances[request.session_hash]
# #instances[request.session_hash] = stage_func(instance)
# data = instances[request.session_hash]
# report = data['report']
# if report is None:
# new_report = stage_func(data['data_frame'])
# else:
# new_report = stage_func(report)
# instances[request.session_hash]['report'] = new_report
# # print("after:", self.stage_name, self.current_value)
# print(input_block)
# text = ""
# for i in progress.tqdm(np.arange(5)):
# text += f"{i}"
# sleep(0.01)
# return True
# def restart_pipeline(request: gr.Request, progress=gr.Progress()):
# print("restart")
# # clear_checkboxes_button.click()
# return process_step(True, request, progress=progress)
# if i == 0:
# return restart_pipeline
# return process_step
# def finish_job(self, input_block, request: gr.Request):
# # update state of associated components based on session
# print(input_block)
# if request.session_hash in instances:
# report = instances[request.session_hash]['report']
# if report is None:
# print("Report is None")
# return "Report is None"
# print("Finished! Session hash is ok, didn't make it to the report yet")
# # TODO: actual processing of the results should be here
# return "Finished! Session hash is ok, didn't make it to the report yet"
# return f"Finished! Session hash is not available, no data"
# def build_ui(named_stages_list):
# num_stages = len(named_stages_list)
# pipeline_blocks = []
# with gr.Blocks(title="storybear") as demo:
# gr.Markdown("## STORYBEAR: from science to fairytale via agent-assisted storytelling")
# status_output = gr.Textbox(label="Status")
# # demo_button.click(click_demo, [], pipeline_blocks[0])
# with gr.Row("Parent container"):
# with gr.Column():
# df_hf_path = gr.Textbox(label="Select data table", lines=3, value=DATA_EXAMPLES[0])
# demo_button = gr.Button("Click the button to launch on demo dataset")
# gr.Examples(DATA_EXAMPLES, df_hf_path, label="Select dataset")
# with gr.Column():
# with gr.Accordion("Pipeline status dashboard"):
# gr.Markdown((
# "Each of the checkboxes correspond to one of the pipeline stages. "
# "The checkbox is checked when the stage is finished."
# "Current stage is executed with the progress bar. \n"
# "To rerun the pipeline, first click on 'Clear button' below"
# ))
# for i, (step_name, step_func) in enumerate(named_stages_list):
# # for i in range(num_steps):
# pipeline_blocks.append(
# gr.Checkbox(value=False, label=f"Stage {i}: {step_name}", interactive=False)
# )
# clear_checkboxes_button = gr.ClearButton()
# with gr.Column("Parent container") as container:
# @gr.render(inputs=[pipeline_blocks[-1]])
# def show_demo_view(count, request: gr.Request):
# print(count)
# if request.session_hash in instances:
# report = instances[request.session_hash]['report']
# with gr.Row("Header line"):
# blocks = [
# gr.Markdown(f"# {report.header}"),
# gr.Markdown(f"## {report.lead}"),
# ]
# for i, record in enumerate(report.plot_record_list):
# text = gr.Label(record.caption)
# im = gr.Image(record.plot_path)
# blocks.append(gr.Row(f"Row_{i}", [text, im]))
# clear_checkboxes_button.click(call_clear_checkboxes, pipeline_blocks, pipeline_blocks)
# stage_processor = StageProcessor(named_stages_list)
# for i in range(num_stages - 1):
# input_block = pipeline_blocks[i]
# output_block = pipeline_blocks[i+1]
# # stage_name, stage_func = named_step_list[i+1]
# process_step = stage_processor[i+1]
# input_block.change(process_step, [input_block], [output_block])
# output_block.change(stage_processor.finish_job, [output_block], [status_output])
# output_block.change(show_demo_view, [output_block], [])
# #stage_name, stage_func = named_step_list[0]
# restart_pipeline = stage_processor[0]
# #restart_pipeline = StageProcessor(stage_name, stage_func).restart_pipeline
# demo_button.click(restart_pipeline, [], [pipeline_blocks[0]])
# demo.load(initialize_instance, inputs=None, outputs=status_output)
# # Clean up instance when page is closed/refreshed
# demo.unload(cleanup_instance)
# return demo
# def create_app(use_llm=False, use_vlm=False, remote=False):
# inference = "storybear.remote_inference" if remote else "storybear.local_inference"
# if use_llm:
# # from storybear.local_inference import it2t_summary_func
# import importlib
# inf = importlib.import_module(inference)
# it2t_summary_func = inf.it2t_summary_func
# # Foodie uses the optimized pairwise compare call when the backend has one.
# it2t_compare_func = getattr(inf, "it2t_compare_func", it2t_summary_func)
# captionist_it2t_func = it2t_summary_func
# foodie_it2t_func = it2t_compare_func
# editor_it2t_func = it2t_summary_func
# else:
# captionist_it2t_func = None
# foodie_it2t_func = None
# editor_it2t_func = None
# if use_vlm:
# # from storybear.local_inference import flux_i2i_func
# import importlib
# flux_i2i_func = importlib.import_module(inference).flux_i2i_func
# artist_i2i_func = flux_i2i_func
# else:
# artist_i2i_func = None
# from storybear.pipeline import StorybearPipeline
# pipeline = StorybearPipeline(
# captionist_it2t_func=captionist_it2t_func,
# foodie_it2t_func=foodie_it2t_func,
# editor_it2t_func=editor_it2t_func,
# artist_i2i_func=artist_i2i_func,
# )
# named_stages_list = pipeline.get_stages()
# # current_value = gr.State([0])
# # pipeline_blocks = []
# # with gr.Blocks(title="storybear") as demo:
# # gr.Markdown("simple ui")
# demo = build_ui(named_stages_list)
# return demo
# demo = create_app()
# demo.launch()