methestrikerx100 commited on
Commit
531fedc
·
verified ·
1 Parent(s): ee96ee3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ from PIL import Image
3
+ import numpy as np
4
+ import gradio as gr
5
+ from huggingface_hub import hf_hub_download
6
+
7
+ # Load the trained model
8
+ repo_id = "methestrikerx100/Mineral_Identifcation_Project"
9
+ filename = "Deployment.h5"
10
+
11
+ # Download the model file
12
+ model_path = hf_hub_download(repo_id=repo_id, filename=filename)
13
+
14
+ # Load the model
15
+ model = tf.keras.models.load_model(model_path)
16
+
17
+ # Define the class labels
18
+ class_labels = ['biotite', 'granite', 'olivine', 'plagioclase', 'staurolite']
19
+
20
+ # Define the function to make predictions
21
+ def classify_image(image):
22
+ # Preprocess the image
23
+ image = np.array(image)
24
+ image = Image.fromarray(image.astype(np.uint8), 'RGB')
25
+ image = image.resize((224, 224))
26
+ image = np.array(image) / 255.0
27
+ image = np.expand_dims(image, axis=0)
28
+
29
+ # Make prediction
30
+ prediction = model.predict(image)
31
+ class_idx = np.argmax(prediction)
32
+ prediction_scores = prediction[0]
33
+
34
+ # Convert prediction scores to percentages
35
+ prediction_scores_percentages = [f"{score * 100:.2f}%" for score in prediction_scores]
36
+
37
+ # Create formatted output strings
38
+ predicted_class_name = class_labels[class_idx]
39
+ predicted_scores = "\n".join([f"{label}: {score}" for label, score in zip(class_labels, prediction_scores_percentages)])
40
+
41
+ return predicted_class_name, predicted_scores
42
+
43
+ # Create the Gradio interface
44
+ with gr.Blocks() as demo:
45
+ with gr.Row():
46
+ image_input = gr.Image(elem_id="image_input", type="pil")
47
+ output_components = [
48
+ gr.Textbox(elem_id="predicted_class_name"),
49
+ gr.Textbox(elem_id="predicted_scores", lines=5)
50
+ ]
51
+ image_button = gr.Button("Classify Image")
52
+ image_button.click(classify_image, inputs=image_input, outputs=output_components)
53
+ demo.launch(share=True)