Skip to content

Commit da381be

Browse files
authored
Update app.py
1 parent d1405fb commit da381be

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

app.py

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,47 @@ def run_command(command):
8787
except subprocess.CalledProcessError as e:
8888
return f"Error: {e.stderr}"
8989

90-
def index_graph(root_dir):
90+
def index_graph(root_dir, progress=gr.Progress()):
9191
command = f"python -m graphrag.index --root {root_dir}"
9292
logging.info(f"Running indexing command: {command}")
93-
result = run_command(command)
93+
94+
# Create a queue to store the output
95+
output_queue = queue.Queue()
96+
97+
def run_command_with_output():
98+
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
99+
for line in iter(process.stdout.readline, ''):
100+
output_queue.put(line)
101+
process.stdout.close()
102+
process.wait()
103+
104+
# Start the command in a separate thread
105+
thread = threading.Thread(target=run_command_with_output)
106+
thread.start()
107+
108+
# Initialize progress
109+
progress(0, desc="Starting indexing...")
110+
111+
# Process the output and update progress
112+
full_output = []
113+
while thread.is_alive() or not output_queue.empty():
114+
try:
115+
line = output_queue.get_nowait()
116+
full_output.append(line)
117+
118+
# Update progress based on the output
119+
if "Processing file" in line:
120+
progress((0.5, None), desc="Processing files...")
121+
elif "Indexing completed" in line:
122+
progress(1, desc="Indexing completed")
123+
124+
yield "\n".join(full_output), update_logs()
125+
except queue.Empty:
126+
time.sleep(0.1)
127+
128+
thread.join()
94129
logging.info("Indexing completed")
95-
return result, update_logs()
130+
return "\n".join(full_output), update_logs()
96131

97132
def run_query(root_dir, method, query, history):
98133
command = f"python -m graphrag.query --root {root_dir} --method {method} \"{query}\""
@@ -708,7 +743,8 @@ def list_folder_contents(folder_path):
708743
with gr.Accordion("Indexing", open=True):
709744
root_dir = gr.Textbox(label="Root Directory", value=os.path.abspath("./ragtest"))
710745
index_btn = gr.Button("Run Indexing", variant="primary")
711-
index_output = gr.Textbox(label="Indexing Output", lines=5, visible=False)
746+
index_output = gr.Textbox(label="Indexing Output", lines=10, visible=True)
747+
index_progress = gr.Textbox(label="Indexing Progress", visible=True)
712748

713749
with gr.TabItem("Indexing Outputs"):
714750
output_folder_list = gr.Dropdown(label="Select Output Folder", choices=[], interactive=True)
@@ -767,7 +803,12 @@ def list_folder_contents(folder_path):
767803
)
768804
delete_btn.click(fn=delete_file, inputs=[file_list], outputs=[operation_status, file_list, log_output])
769805
save_btn.click(fn=save_file_content, inputs=[file_list, file_content], outputs=[operation_status, log_output])
770-
index_btn.click(fn=index_graph, inputs=[root_dir], outputs=[index_output, log_output])
806+
index_btn.click(
807+
fn=index_graph,
808+
inputs=[root_dir],
809+
outputs=[index_output, log_output],
810+
show_progress=True
811+
)
771812
refresh_folder_btn.click(fn=update_output_folder_list, outputs=[output_folder_list]).then(
772813
fn=update_logs,
773814
outputs=[log_output]

0 commit comments

Comments
 (0)