zdou0830 commited on
Commit
81248a2
1 Parent(s): 4a4402a
Files changed (3) hide show
  1. Dockerfile +1 -1
  2. main.py +28 -4
  3. requirements.txt +1 -0
Dockerfile CHANGED
@@ -10,4 +10,4 @@ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
10
 
11
  COPY . .
12
 
13
- CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
10
 
11
  COPY . .
12
 
13
+ CMD ["python", "main.py"]
main.py CHANGED
@@ -1,7 +1,31 @@
1
- from fastapi import FastAPI
 
 
 
2
 
3
- app = FastAPI()
 
 
4
 
5
 
6
- def read_root():
7
- return {"Hello": "World!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import requests
4
+ from torchvision import transforms
5
 
6
+ model = torch.hub.load("pytorch/vision:v0.6.0", "resnet18", pretrained=True).eval()
7
+ response = requests.get("https://git.io/JJkYN")
8
+ labels = response.text.split("\n")
9
 
10
 
11
+ def predict(inp):
12
+ inp = transforms.ToTensor()(inp).unsqueeze(0)
13
+ with torch.no_grad():
14
+ prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
15
+ confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
16
+ return confidences
17
+
18
+
19
+ def run():
20
+ demo = gr.Interface(
21
+ fn=predict,
22
+ inputs=gr.inputs.Image(type="pil"),
23
+ outputs=gr.outputs.Label(num_top_classes=3),
24
+ )
25
+
26
+ demo.launch(server_name="0.0.0.0", server_port=7860)
27
+
28
+
29
+ if __name__ == "__main__":
30
+ run()
31
+
requirements.txt CHANGED
@@ -14,3 +14,4 @@ wandb
14
  protobuf==3.20.1
15
  fastapi==0.74.*
16
  requests==2.27.*
 
 
14
  protobuf==3.20.1
15
  fastapi==0.74.*
16
  requests==2.27.*
17
+ gradio