satpalsr commited on
Commit
b3c8ec7
1 Parent(s): 4efffa0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -18
app.py CHANGED
@@ -2,22 +2,27 @@ from transformers import AutoFeatureExtractor, RegNetForImageClassification
2
  import torch
3
  import gradio as gr
4
 
5
- feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/regnet-y-040")
6
- model = RegNetForImageClassification.from_pretrained("facebook/regnet-y-040")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- def inference(image):
9
- print("Type of image", type(image))
10
- inputs = feature_extractor(image, return_tensors="pt")
11
-
12
- with torch.no_grad():
13
- logits = model(**inputs).logits
14
-
15
- predicted_label = logits.argmax(-1).item()
16
- return model.config.id2label[predicted_label]
17
-
18
- title="RegNet-image-classification"
19
- description="This space uses RegNet Model with an image classification head on top (a linear layer on top of the pooled features). It predicts one of the 1000 ImageNet classes. Check [Docs](https://huggingface.co/docs/transformers/main/en/model_doc/regnet) for more details."
20
-
21
- examples=[['wolf.jpg'], ['ballon.jpg'], ['fountain.jpg']]
22
- iface = gr.Interface(inference, inputs=gr.inputs.Image(), outputs="text",title=title,description=description,examples=examples)
23
- iface.launch(enable_queue=True,cache_examples=True)
 
2
  import torch
3
  import gradio as gr
4
 
5
+ try:
6
+ feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/regnet-y-040")
7
+ model = RegNetForImageClassification.from_pretrained("facebook/regnet-y-040")
8
+
9
+ def inference(image):
10
+ print("Type of image", type(image))
11
+ inputs = feature_extractor(image, return_tensors="pt")
12
+
13
+ with torch.no_grad():
14
+ logits = model(**inputs).logits
15
+
16
+ predicted_label = logits.argmax(-1).item()
17
+ return model.config.id2label[predicted_label]
18
+
19
+ title="RegNet-image-classification"
20
+ description="This space uses RegNet Model with an image classification head on top (a linear layer on top of the pooled features). It predicts one of the 1000 ImageNet classes. Check [Docs](https://huggingface.co/docs/transformers/main/en/model_doc/regnet) for more details."
21
+
22
+ examples=[['wolf.jpg'], ['ballon.jpg'], ['fountain.jpg']]
23
+ iface = gr.Interface(inference, inputs=gr.inpu, outputs="text",title=title,description=description,examples=examples)
24
+ iface.launch(enable_queue=True,cache_examples=True)
25
 
26
+ except Exception as e:
27
+ print("Oops got an error: Create an issue/PR at github.com/satpalsr/space-repo")
28
+ print( "Error: %s" % str(e) )