Spaces:
Runtime error
Runtime error
Commit
·
896e60d
1
Parent(s):
da59cbe
update gradio server port
Browse files- Dockerfile +2 -4
- app.py +3 -9
Dockerfile
CHANGED
|
@@ -15,10 +15,8 @@ COPY ./requirements.txt /code/requirements.txt
|
|
| 15 |
|
| 16 |
RUN apt-get install -y python3 python3-pip
|
| 17 |
|
| 18 |
-
|
| 19 |
-
RUN pip install --upgrade -r /code/requirements.txt
|
| 20 |
-
|
| 21 |
|
| 22 |
COPY . .
|
| 23 |
|
| 24 |
-
CMD ["python3", "app.py"
|
|
|
|
| 15 |
|
| 16 |
RUN apt-get install -y python3 python3-pip
|
| 17 |
|
| 18 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
|
|
|
|
|
|
| 19 |
|
| 20 |
COPY . .
|
| 21 |
|
| 22 |
+
CMD ["python3", "app.py"]
|
app.py
CHANGED
|
@@ -11,14 +11,12 @@ class Pix2StructForRegression(nn.Module):
|
|
| 11 |
def __init__(self, sourcemodel_path, device):
|
| 12 |
super(Pix2StructForRegression, self).__init__()
|
| 13 |
self.model = Pix2StructVisionModel.from_pretrained(sourcemodel_path)
|
| 14 |
-
print("Pix2StructForRegression Model is Loaded...")
|
| 15 |
self.regression_layer1 = nn.Linear(768, 1536)
|
| 16 |
self.dropout1 = nn.Dropout(0.1)
|
| 17 |
self.regression_layer2 = nn.Linear(1536, 768)
|
| 18 |
self.dropout2 = nn.Dropout(0.1)
|
| 19 |
self.regression_layer3 = nn.Linear(768, 2)
|
| 20 |
self.device = device
|
| 21 |
-
print("Regression Layers are Loaded...")
|
| 22 |
|
| 23 |
def forward(self, *args, **kwargs):
|
| 24 |
outputs = self.model(*args, **kwargs)
|
|
@@ -32,16 +30,13 @@ class Pix2StructForRegression(nn.Module):
|
|
| 32 |
return regression_output
|
| 33 |
|
| 34 |
def load_state_dict_file(self, checkpoint_path, strict=True):
|
| 35 |
-
print("Loading Model Weights...")
|
| 36 |
state_dict = torch.load(checkpoint_path, map_location=self.device)
|
| 37 |
self.load_state_dict(state_dict, strict=strict)
|
| 38 |
-
print("Model Weights are Loaded...")
|
| 39 |
|
| 40 |
class Inference:
|
| 41 |
def __init__(self) -> None:
|
| 42 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 43 |
-
self.model, self.processor = self.load_model_and_processor("matcha-base", "model/pta-text-v0.1.pt")
|
| 44 |
-
print("Model and Processor are Loaded...")
|
| 45 |
|
| 46 |
def load_model_and_processor(self, model_name, checkpoint_path):
|
| 47 |
model = Pix2StructForRegression(sourcemodel_path=model_name, device=self.device)
|
|
@@ -84,7 +79,6 @@ class Inference:
|
|
| 84 |
|
| 85 |
def draw_circle_on_image(self, image, coordinates):
|
| 86 |
x, y = coordinates[0] * image.width, coordinates[1] * image.height
|
| 87 |
-
print(coordinates)
|
| 88 |
draw = ImageDraw.Draw(image)
|
| 89 |
radius = 5
|
| 90 |
draw.ellipse((x-radius, y-radius, x+radius, y+radius), fill="red")
|
|
@@ -99,7 +93,6 @@ class Inference:
|
|
| 99 |
|
| 100 |
def main():
|
| 101 |
inference = Inference()
|
| 102 |
-
print("Model and Processor are Loaded...")
|
| 103 |
# Gradio Interface
|
| 104 |
iface = gr.Interface(
|
| 105 |
fn=inference.process_image_and_draw_circle,
|
|
@@ -110,7 +103,8 @@ def main():
|
|
| 110 |
description="Upload an image and enter a prompt to see the model's prediction."
|
| 111 |
)
|
| 112 |
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
iface.launch()
|
| 115 |
if __name__ == "__main__":
|
| 116 |
main()
|
|
|
|
| 11 |
def __init__(self, sourcemodel_path, device):
|
| 12 |
super(Pix2StructForRegression, self).__init__()
|
| 13 |
self.model = Pix2StructVisionModel.from_pretrained(sourcemodel_path)
|
|
|
|
| 14 |
self.regression_layer1 = nn.Linear(768, 1536)
|
| 15 |
self.dropout1 = nn.Dropout(0.1)
|
| 16 |
self.regression_layer2 = nn.Linear(1536, 768)
|
| 17 |
self.dropout2 = nn.Dropout(0.1)
|
| 18 |
self.regression_layer3 = nn.Linear(768, 2)
|
| 19 |
self.device = device
|
|
|
|
| 20 |
|
| 21 |
def forward(self, *args, **kwargs):
|
| 22 |
outputs = self.model(*args, **kwargs)
|
|
|
|
| 30 |
return regression_output
|
| 31 |
|
| 32 |
def load_state_dict_file(self, checkpoint_path, strict=True):
|
|
|
|
| 33 |
state_dict = torch.load(checkpoint_path, map_location=self.device)
|
| 34 |
self.load_state_dict(state_dict, strict=strict)
|
|
|
|
| 35 |
|
| 36 |
class Inference:
|
| 37 |
def __init__(self) -> None:
|
| 38 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 39 |
+
self.model, self.processor = self.load_model_and_processor("google/matcha-base", "model/pta-text-v0.1.pt")
|
|
|
|
| 40 |
|
| 41 |
def load_model_and_processor(self, model_name, checkpoint_path):
|
| 42 |
model = Pix2StructForRegression(sourcemodel_path=model_name, device=self.device)
|
|
|
|
| 79 |
|
| 80 |
def draw_circle_on_image(self, image, coordinates):
|
| 81 |
x, y = coordinates[0] * image.width, coordinates[1] * image.height
|
|
|
|
| 82 |
draw = ImageDraw.Draw(image)
|
| 83 |
radius = 5
|
| 84 |
draw.ellipse((x-radius, y-radius, x+radius, y+radius), fill="red")
|
|
|
|
| 93 |
|
| 94 |
def main():
|
| 95 |
inference = Inference()
|
|
|
|
| 96 |
# Gradio Interface
|
| 97 |
iface = gr.Interface(
|
| 98 |
fn=inference.process_image_and_draw_circle,
|
|
|
|
| 103 |
description="Upload an image and enter a prompt to see the model's prediction."
|
| 104 |
)
|
| 105 |
|
| 106 |
+
iface.launch(server_name="0.0.0.0", port=7860)
|
| 107 |
+
|
| 108 |
|
|
|
|
| 109 |
if __name__ == "__main__":
|
| 110 |
main()
|