osanseviero HF staff commited on
Commit
df6aa25
1 Parent(s): 89a520c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -1
app.py CHANGED
@@ -1,9 +1,14 @@
 
1
  import numpy as np
2
  from tensorflow import keras
3
  from tensorflow.keras import layers
4
 
5
  from huggingface_hub import push_to_hub_keras
6
 
 
 
 
 
7
  def to_numpy(examples):
8
  examples["pixel_values"] = [np.array(image) for image in examples["image"]]
9
  return examples
@@ -31,7 +36,7 @@ def preprocess():
31
 
32
  return x_train, y_train, x_test, y_test
33
 
34
- def training():
35
  x_train, y_train, x_test, y_test = preprocess()
36
 
37
  model = keras.Sequential(
@@ -84,3 +89,22 @@ def find_samples_to_label():
84
  )
85
  unlabeled_data.push_to_hub("active-learning/unlabeled_samples")
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request, Response
2
  import numpy as np
3
  from tensorflow import keras
4
  from tensorflow.keras import layers
5
 
6
  from huggingface_hub import push_to_hub_keras
7
 
8
+ KEY = os.environ.get("WEBHOOK_SECRET")
9
+
10
+ app = FastAPI()
11
+
12
  def to_numpy(examples):
13
  examples["pixel_values"] = [np.array(image) for image in examples["image"]]
14
  return examples
 
36
 
37
  return x_train, y_train, x_test, y_test
38
 
39
+ def train():
40
  x_train, y_train, x_test, y_test = preprocess()
41
 
42
  model = keras.Sequential(
 
89
  )
90
  unlabeled_data.push_to_hub("active-learning/unlabeled_samples")
91
 
92
+ @app.get("/")
93
+ def read_root():
94
+ data = """
95
+ <h2 style="text-align:center">Active Learning Trainer</h2>
96
+ <p style="text-align:center">This is a demo app showing how to webhooks to do Active Learning.</p>
97
+ """
98
+ return Response(content=data, media_type="text/html")
99
+
100
+ @app.post("/webhook")
101
+ async def webhook(request):
102
+ if request.method == "POST":
103
+ if request.headers.get("X-Webhook-Secret") != KEY:
104
+ return Response("Invalid secret", status_code=401)
105
+ data = await request.json()
106
+ print("Webhook received!")
107
+ train()
108
+ find_samples_to_label()
109
+ return "Webhook received!" if result else result
110
+