a-ragab-h-m commited on
Commit
c4c7edc
ยท
verified ยท
1 Parent(s): f8b0dae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -11
app.py CHANGED
@@ -1,9 +1,13 @@
1
  import gradio as gr
2
  import subprocess
3
  import os
 
4
 
5
  logs = []
6
  inference_logs = []
 
 
 
7
 
8
  def run_training():
9
  global logs
@@ -17,7 +21,7 @@ def run_training():
17
  )
18
  for line in process.stdout:
19
  logs.append(line)
20
- yield "\n".join(logs[-50:]) # ุนุฑุถ ุขุฎุฑ 50 ุณุทุฑ
21
 
22
  def run_inference():
23
  global inference_logs
@@ -36,7 +40,7 @@ def run_inference():
36
  output_lines.append(line)
37
 
38
  # ุญูุธ ุงู„ู†ุชุงุฆุฌ
39
- with open("/home/user/data/inference_results.txt", "a") as f:
40
  f.write("=== New Inference Run ===\n")
41
  f.writelines(output_lines)
42
  f.write("\n")
@@ -44,15 +48,24 @@ def run_inference():
44
  yield "\n".join(inference_logs[-50:])
45
 
46
  def list_saved_files():
47
- data_dir = "/home/user/data"
48
- if not os.path.exists(data_dir):
49
- return "๐Ÿ“ No saved files found."
50
-
51
  files = os.listdir(data_dir)
52
  if not files:
53
- return "๐Ÿ“‚ No files found in /data."
54
-
55
- return "\n".join(f"๐Ÿ“„ {f}" for f in files)
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  with gr.Blocks(title="VRP Solver Interface") as demo:
58
  gr.Markdown("# ๐Ÿš› VRP Solver: Transformer + RL + OR-Tools")
@@ -63,13 +76,22 @@ with gr.Blocks(title="VRP Solver Interface") as demo:
63
  start_btn.click(fn=run_training, outputs=output)
64
 
65
  with gr.Tab("๐Ÿ” Inference"):
66
- infer_btn = gr.Button("Run Inference")
 
 
 
67
  infer_output = gr.Textbox(label="Inference Output", lines=15)
 
68
  infer_btn.click(fn=run_inference, outputs=infer_output)
 
69
 
70
  with gr.Tab("๐Ÿ“ Show Saved Files"):
71
  list_btn = gr.Button("List /data Files")
72
  file_output = gr.Textbox(label="Saved Files", lines=10)
73
- list_btn.click(fn=list_saved_files, outputs=file_output)
 
 
 
 
74
 
75
  demo.launch()
 
1
  import gradio as gr
2
  import subprocess
3
  import os
4
+ import shutil
5
 
6
  logs = []
7
  inference_logs = []
8
+ data_dir = "/home/user/data"
9
+
10
+ os.makedirs(data_dir, exist_ok=True) # ุชุฃูƒุฏ ู…ู† ูˆุฌูˆุฏ ุงู„ู…ุฌู„ุฏ
11
 
12
  def run_training():
13
  global logs
 
21
  )
22
  for line in process.stdout:
23
  logs.append(line)
24
+ yield "\n".join(logs[-50:]) # ุขุฎุฑ 50 ุณุทุฑ ูู‚ุท
25
 
26
  def run_inference():
27
  global inference_logs
 
40
  output_lines.append(line)
41
 
42
  # ุญูุธ ุงู„ู†ุชุงุฆุฌ
43
+ with open(os.path.join(data_dir, "inference_results.txt"), "a") as f:
44
  f.write("=== New Inference Run ===\n")
45
  f.writelines(output_lines)
46
  f.write("\n")
 
48
  yield "\n".join(inference_logs[-50:])
49
 
50
  def list_saved_files():
 
 
 
 
51
  files = os.listdir(data_dir)
52
  if not files:
53
+ return [], "๐Ÿ“‚ No files found in /data."
54
+ return files, "\n".join(f"๐Ÿ“„ {f}" for f in files)
55
+
56
+ def upload_files(file):
57
+ if file is None:
58
+ return "No file uploaded."
59
+ dest_path = os.path.join(data_dir, os.path.basename(file.name))
60
+ shutil.copy(file.name, dest_path)
61
+ return f"โœ… Uploaded to {dest_path}"
62
+
63
+ def download_file(filename):
64
+ file_path = os.path.join(data_dir, filename)
65
+ if os.path.exists(file_path):
66
+ return file_path
67
+ else:
68
+ return None
69
 
70
  with gr.Blocks(title="VRP Solver Interface") as demo:
71
  gr.Markdown("# ๐Ÿš› VRP Solver: Transformer + RL + OR-Tools")
 
76
  start_btn.click(fn=run_training, outputs=output)
77
 
78
  with gr.Tab("๐Ÿ” Inference"):
79
+ with gr.Row():
80
+ infer_btn = gr.Button("Run Inference")
81
+ upload_input = gr.File(label="Upload File for Inference (.json or .pt)")
82
+ upload_status = gr.Textbox(label="Upload Status")
83
  infer_output = gr.Textbox(label="Inference Output", lines=15)
84
+
85
  infer_btn.click(fn=run_inference, outputs=infer_output)
86
+ upload_input.change(fn=upload_files, inputs=upload_input, outputs=upload_status)
87
 
88
  with gr.Tab("๐Ÿ“ Show Saved Files"):
89
  list_btn = gr.Button("List /data Files")
90
  file_output = gr.Textbox(label="Saved Files", lines=10)
91
+ file_dropdown = gr.Dropdown(choices=[], label="Select File to Download")
92
+ download_btn = gr.File(label="Download File")
93
+
94
+ list_btn.click(fn=list_saved_files, outputs=[file_dropdown, file_output])
95
+ file_dropdown.change(fn=download_file, inputs=file_dropdown, outputs=download_btn)
96
 
97
  demo.launch()