Sreekanth Tangirala commited on
Commit
9c8dfb8
·
1 Parent(s): 22d9eee

remove pth from tracking

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. app.py +55 -32
  3. upload_to_hub.py +122 -0
.gitignore CHANGED
@@ -30,6 +30,7 @@ env.bak/
30
  venv.bak/
31
 
32
  # PyTorch specific
 
33
  *.pt
34
  *.pkl
35
  *.onnx
 
30
  venv.bak/
31
 
32
  # PyTorch specific
33
+ *.pth
34
  *.pt
35
  *.pkl
36
  *.onnx
app.py CHANGED
@@ -1,41 +1,64 @@
1
- import gradio as gr
2
  import torch
3
- import torchvision.transforms as transforms
4
  from PIL import Image
5
- from torchvision.models import resnet50
6
- import torch.nn as nn
7
 
8
- # Load model
9
- model = resnet50(pretrained=False)
10
- model.fc = nn.Linear(model.fc.in_features, 10)
11
- model.load_state_dict(torch.load('best_model.pth'))
12
- model.eval()
13
-
14
- # Define classes (for CIFAR-10)
15
- classes = ['airplane', 'automobile', 'bird', 'cat', 'deer',
16
- 'dog', 'frog', 'horse', 'ship', 'truck']
 
 
 
 
 
17
 
18
- def predict(image):
19
- transform = transforms.Compose([
20
- transforms.Resize(224),
21
- transforms.ToTensor(),
22
- transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
23
- ])
24
 
25
- img_tensor = transform(image).unsqueeze(0)
 
 
 
 
 
 
 
 
 
26
 
 
27
  with torch.no_grad():
28
- outputs = model(img_tensor)
29
- _, predicted = outputs.max(1)
30
-
31
- return classes[predicted.item()]
 
 
 
 
32
 
33
- # Create Gradio interface
34
- iface = gr.Interface(
35
- fn=predict,
36
- inputs=gr.Image(type="pil"),
37
- outputs=gr.Label(num_top_classes=1),
38
- examples=[["example1.jpg"], ["example2.jpg"]]
39
- )
 
 
 
 
 
 
 
 
 
40
 
41
- iface.launch()
 
 
1
+ from transformers import AutoModelForImageClassification, AutoFeatureExtractor
2
  import torch
 
3
  from PIL import Image
 
 
4
 
5
+ def load_model_from_hub(repo_id: str):
6
+ """
7
+ Load model from Hugging Face Hub
8
+
9
+ Args:
10
+ repo_id: The repository ID (e.g., 'username/model-name')
11
+ Returns:
12
+ model: The loaded model
13
+ processor: The feature extractor/processor
14
+ """
15
+ # Load model and processor from Hub
16
+ model = AutoModelForImageClassification.from_pretrained(repo_id)
17
+ processor = AutoFeatureExtractor.from_pretrained(repo_id)
18
+ return model, processor
19
 
20
+ def predict(image_path: str, model, processor):
21
+ """
22
+ Make prediction using the loaded model
 
 
 
23
 
24
+ Args:
25
+ image_path: Path to input image
26
+ model: Loaded model
27
+ processor: Feature extractor/processor
28
+ Returns:
29
+ prediction: Model prediction
30
+ """
31
+ # Load and preprocess image
32
+ image = Image.open(image_path)
33
+ inputs = processor(images=image, return_tensors="pt")
34
 
35
+ # Make prediction
36
  with torch.no_grad():
37
+ outputs = model(**inputs)
38
+ predictions = outputs.logits.softmax(-1)
39
+
40
+ return predictions
41
+
42
+ # Example usage in your Flask/FastAPI app
43
+ from flask import Flask, request
44
+ app = Flask(__name__)
45
 
46
+ # Load model at startup
47
+ model, processor = load_model_from_hub("srtangirala/resnet50-exp")
48
+
49
+ @app.route('/predict', methods=['POST'])
50
+ def predict_endpoint():
51
+ if 'file' not in request.files:
52
+ return {'error': 'No file provided'}, 400
53
+
54
+ file = request.files['file']
55
+ image_path = "temp_image.jpg" # You might want to generate a unique filename
56
+ file.save(image_path)
57
+
58
+ predictions = predict(image_path, model, processor)
59
+
60
+ # Convert predictions to list and return
61
+ return {'predictions': predictions.tolist()[0]}
62
 
63
+ if __name__ == '__main__':
64
+ app.run(debug=True)
upload_to_hub.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import HfApi, login
2
+ import torch
3
+ from transformers import AutoModelForImageClassification, AutoFeatureExtractor
4
+ import json
5
+ import os
6
+
7
+ def upload_model_to_hub(
8
+ model_path: str,
9
+ repo_name: str,
10
+ token: str,
11
+ num_labels: int,
12
+ label2id: dict,
13
+ id2label: dict,
14
+ model_architecture: str = "resnet50",
15
+ task: str = "image-classification",
16
+ ):
17
+ """
18
+ Upload a PyTorch model to Hugging Face Hub with proper configuration
19
+ """
20
+ # Login to Hugging Face
21
+ login(token=token)
22
+ api = HfApi()
23
+
24
+ # Create the repository
25
+ repo_url = api.create_repo(
26
+ repo_id=repo_name,
27
+ exist_ok=True,
28
+ private=False
29
+ )
30
+
31
+ # Create config.json
32
+ config = {
33
+ "architectures": ["ResNetForImageClassification"],
34
+ "model_type": "resnet",
35
+ "num_labels": num_labels,
36
+ "id2label": id2label,
37
+ "label2id": label2id,
38
+ "num_channels": 3,
39
+ "hidden_sizes": [2048],
40
+ "image_size": [224, 224]
41
+ }
42
+
43
+ # Create feature extractor config
44
+ feature_extractor = {
45
+ "image_mean": [0.485, 0.456, 0.406],
46
+ "image_std": [0.229, 0.224, 0.225],
47
+ "do_normalize": True,
48
+ "do_resize": True,
49
+ "size": 224,
50
+ "resample": 2
51
+ }
52
+
53
+ # Upload config files
54
+ api.upload_file(
55
+ path_or_fileobj=json.dumps(config).encode(),
56
+ path_in_repo="config.json",
57
+ repo_id=repo_name,
58
+ commit_message="Upload model config"
59
+ )
60
+
61
+ api.upload_file(
62
+ path_or_fileobj=json.dumps(feature_extractor).encode(),
63
+ path_in_repo="preprocessor_config.json",
64
+ repo_id=repo_name,
65
+ commit_message="Upload preprocessor config"
66
+ )
67
+
68
+ # Upload the model file
69
+ api.upload_file(
70
+ path_or_fileobj=model_path,
71
+ path_in_repo="pytorch_model.bin",
72
+ repo_id=repo_name,
73
+ commit_message="Upload model weights"
74
+ )
75
+
76
+ # Create and upload model card
77
+ model_card = f"""
78
+ ---
79
+ language: en
80
+ tags:
81
+ - pytorch
82
+ - {model_architecture}
83
+ - {task}
84
+ ---
85
+
86
+ # Model Card for {repo_name}
87
+
88
+ This model is a fine-tuned version of {model_architecture} for {task}.
89
+ """
90
+
91
+ api.upload_file(
92
+ path_or_fileobj=model_card.encode(),
93
+ path_in_repo="README.md",
94
+ repo_id=repo_name,
95
+ commit_message="Upload model card"
96
+ )
97
+
98
+ print(f"Model uploaded successfully to: https://huggingface.co/{repo_name}")
99
+
100
+ if __name__ == "__main__":
101
+ # Get Hugging Face token from environment variable
102
+ token = os.getenv("HF_TOKEN")
103
+ if not token:
104
+ raise ValueError("Please set the HF_TOKEN environment variable")
105
+
106
+ # Example label mappings - replace with your actual labels
107
+ label2id = {
108
+ "class1": 0,
109
+ "class2": 1,
110
+ # ... add all your classes
111
+ }
112
+ id2label = {str(v): k for k, v in label2id.items()}
113
+
114
+ # Upload the model
115
+ upload_model_to_hub(
116
+ model_path="best_model.pth",
117
+ repo_name="srtangirala/resnet50-exp",
118
+ token=token,
119
+ num_labels=len(label2id),
120
+ label2id=label2id,
121
+ id2label=id2label
122
+ )