kim1688 commited on
Commit
866fc85
·
verified ·
1 Parent(s): b0b78da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py CHANGED
@@ -8,9 +8,53 @@ os.environ["KERAS_BACKEND"] = "tensorflow"
8
  print("loading file")
9
  model = keras.saving.load_model("hf://kim1688/casting_defect_resnet50")
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
 
 
12
 
13
 
 
14
  def upload_images(image_paths):
15
  df = pd.DataFrame(columns=["Index", "File", "Result"])
16
  for i in range(len(image_paths)):
@@ -32,5 +76,6 @@ demo = gr.Interface(
32
  gr.File(file_count="multiple", file_types=[".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff"]),
33
  gr.DataFrame(headers=["Index", "File", "Result"])
34
  )
 
35
 
36
  demo.launch(share=True, debug=True)
 
8
  print("loading file")
9
  model = keras.saving.load_model("hf://kim1688/casting_defect_resnet50")
10
 
11
+ def upload_images(image_paths):
12
+ docs.clear()
13
+ df = pd.DataFrame(columns=["Index", "File", "Result"])
14
+ for i in range(len(image_paths)):
15
+ df.loc[i] = [str(i+1), image_paths[i].split("/")[-1], predict(image_paths[i])]
16
+ docs.append([str(i+1), image_paths[i].split("/")[-1], predict(image_paths[i])])
17
+ return [df, gr.Button(visible=True), gr.DownloadButton(label="Download report", visible=True)]
18
+
19
+ # Function to preprocess image and predict
20
+ def predict(image_path):
21
+ img = keras.utils.load_img(image_path, target_size=(300, 300))
22
+ img_array = keras.utils.img_to_array(img)
23
+ img_array = keras.ops.expand_dims(img_array, 0)
24
+ prediction = model.predict(img_array)
25
+ class_names = ["Defective", "Ok"] # Class 0: def, Class 1: ok
26
+ predicted_class = class_names[1] if prediction > 0.5 else class_names[0]
27
+ return predicted_class
28
+
29
+ def generate_docs():
30
+ document = Document()
31
+ document.add_heading("Casting Report", 0)
32
+ table = document.add_table(rows=1, cols=3)
33
+ hdr_cells = table.rows[0].cells
34
+ hdr_cells[0].text = "Index"
35
+ hdr_cells[1].text = "File"
36
+ hdr_cells[2].text = "Result"
37
+ for i in range(len(docs)):
38
+ row_cells = table.add_row().cells
39
+ row_cells[0].text = docs[i][0]
40
+ row_cells[1].text = docs[i][1]
41
+ row_cells[2].text = docs[i][2]
42
+ document.save("casting_report.docx")
43
+ return [gr.UploadButton(visible=True), gr.DownloadButton(visible=True)]
44
+
45
+
46
+ with gr.Blocks() as demo:
47
+ with gr.Column():
48
+ f = gr.File(file_count="multiple", file_types=[".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff"])
49
+ u = gr.Button("Upload files", visible=True)
50
+ d = gr.DownloadButton("Download report", visible=True)
51
+ r = gr.DataFrame(headers=["Index", "File", "Result"])
52
 
53
+ u.click(upload_images, f, [r, u, d])
54
+ d.click(generate_docs, None, [u, d])
55
 
56
 
57
+ """
58
  def upload_images(image_paths):
59
  df = pd.DataFrame(columns=["Index", "File", "Result"])
60
  for i in range(len(image_paths)):
 
76
  gr.File(file_count="multiple", file_types=[".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff"]),
77
  gr.DataFrame(headers=["Index", "File", "Result"])
78
  )
79
+ """
80
 
81
  demo.launch(share=True, debug=True)