Suhani-2407 commited on
Commit
0962fd1
·
verified ·
1 Parent(s): c35e9aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -10
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import os
2
  os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Disable GPU warnings
3
-
4
  import gradio as gr
5
  import tensorflow as tf
6
  import numpy as np
@@ -11,14 +10,25 @@ model = tf.keras.models.load_model("MobileNet_model.h5")
11
  class_names = ["Fake", "Low", "Medium", "High"]
12
 
13
  def predict_image(img):
14
- img = img.resize((128, 128))
15
- img_array = np.array(img) / 255.0
16
- img_array = np.expand_dims(img_array, axis=0)
17
- predictions = model.predict(img_array)
18
- class_index = np.argmax(predictions, axis=1)[0]
19
- confidence_scores = {class_names[i]: float(predictions[0][i]) for i in range(len(class_names))}
20
- return {"Predicted Class": class_names[class_index], "Confidence Scores": confidence_scores}
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- iface = gr.Interface(fn=predict_image, inputs="image", outputs="json")
23
- iface.launch(server_name="0.0.0.0", server_port=7860) # Fix for Hugging Face
24
 
 
1
  import os
2
  os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Disable GPU warnings
 
3
  import gradio as gr
4
  import tensorflow as tf
5
  import numpy as np
 
10
  class_names = ["Fake", "Low", "Medium", "High"]
11
 
12
  def predict_image(img):
13
+ if img is None:
14
+ return {"error": "No image provided"}
15
+ try:
16
+ img = img.resize((128, 128))
17
+ img_array = np.array(img) / 255.0
18
+ img_array = np.expand_dims(img_array, axis=0)
19
+ predictions = model.predict(img_array)
20
+ class_index = np.argmax(predictions, axis=1)[0]
21
+ confidence_scores = {class_names[i]: float(predictions[0][i]) for i in range(len(class_names))}
22
+ return {"Predicted Class": class_names[class_index], "Confidence Scores": confidence_scores}
23
+ except Exception as e:
24
+ return {"error": str(e)}
25
+
26
+ iface = gr.Interface(
27
+ fn=predict_image,
28
+ inputs=gr.Image(type="pil"),
29
+ outputs="json"
30
+ )
31
 
32
+ # Add show_error=True to enable verbose error reporting
33
+ iface.launch(server_name="0.0.0.0", server_port=7860, show_error=True) # Fix for Hugging Face
34