1aguschin commited on
Commit
d7bf453
·
verified ·
1 Parent(s): cb8eacb

Upload folder using huggingface_hub

Browse files
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Gradio Deploy
3
- emoji: 🐢
4
- colorFrom: purple
5
- colorTo: purple
6
  sdk: gradio
7
  sdk_version: 5.12.0
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: gradio-deploy
3
+ app_file: gradio_app_image_classification.py
 
 
4
  sdk: gradio
5
  sdk_version: 5.12.0
 
 
6
  ---
 
 
gradio_app_image_classification.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import torch
4
+ from torchvision import transforms, models
5
+ import requests
6
+
7
+ # Load pre-trained ResNet model
8
+ model = models.resnet50(pretrained=True)
9
+ model.eval()
10
+
11
+ # Download ImageNet class labels
12
+ LABELS_URL = "https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt"
13
+ response = requests.get(LABELS_URL)
14
+ LABELS = response.text.split("\n")
15
+
16
+ # Image preprocessing
17
+ preprocess = transforms.Compose([
18
+ transforms.Resize(256),
19
+ transforms.CenterCrop(224),
20
+ transforms.ToTensor(),
21
+ transforms.Normalize(
22
+ mean=[0.485, 0.456, 0.406],
23
+ std=[0.229, 0.224, 0.225]
24
+ )
25
+ ])
26
+
27
+ def classify_image(image):
28
+ # Convert to PIL Image if needed
29
+ if not isinstance(image, Image.Image):
30
+ image = Image.fromarray(image)
31
+
32
+ # Preprocess image
33
+ input_tensor = preprocess(image)
34
+ input_batch = input_tensor.unsqueeze(0)
35
+
36
+ # Make prediction
37
+ with torch.no_grad():
38
+ output = model(input_batch)
39
+
40
+ # Get predicted class
41
+ _, predicted_idx = torch.max(output, 1)
42
+ predicted_label = LABELS[predicted_idx.item()]
43
+
44
+ return predicted_label
45
+
46
+ # Create Gradio interface
47
+ iface = gr.Interface(
48
+ fn=classify_image,
49
+ inputs=gr.Image(),
50
+ outputs=gr.Text(label="Predicted Class"),
51
+ title="Image Classification",
52
+ description="Upload an image to classify it using ResNet50"
53
+ )
54
+
55
+ # Launch the app
56
+ iface.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ transformers
2
+ gradio
3
+ torch
4
+ llama-cpp-python
5
+ streamlit
6
+ watchdog
7
+ python-telegram-bot
8
+ matplotlib
9
+ tiktoken
10
+ torchvision