File size: 1,099 Bytes
78ca765
 
 
b1db959
78ca765
 
 
b1db959
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78ca765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import gradio as gr
import os
import json
from google.cloud import storage
from fastai.vision.all import load_learner, PILImage


#Setting up GCP client
credentials_content = os.environ['gcp_cam']
with open('gcp_key.json', 'w') as f:
    f.write(credentials_content)

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'gcp_key.json'
bucket_name = os.environ['gcp_bucket']
pkl_blob = 'paulinus/cameroon_food.pkl'
local_pkl = 'cameroon_food.pkl'

client = storage.Client()
bucket = client.bucket(bucket_name)
blob = bucket.blob(pkl_blob)
blob.download_to_filename(local_pkl)

#Load model
learn = load_learner(local_pkl)



def predict(img):
    pred_class, pred_idx, outputs = learn.predict(PILImage.create(img))
    prob = outputs[pred_idx].item()
    return f"Class: {pred_class}, Probability: {prob:.4f}"

#Build Gradio interface
iface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="file"),
    outputs=gr.Textbox(),
    title="Cameroonian Meal Identifier",
    description="Upload a meal image and get the predicted class."
)

# Launch the app
if __name__ == "__main__":
    iface.launch()