simonl0909 commited on
Commit
6f6920c
1 Parent(s): 3db32b8

webhooks test

Browse files
Files changed (3) hide show
  1. app.py +12 -91
  2. local-requirements.txt +8 -0
  3. model.py +16 -0
app.py CHANGED
@@ -1,97 +1,18 @@
1
  import gradio as gr
2
- import random
3
- import time
4
- import tqdm
5
- from datasets import load_dataset
6
- import shutil
7
- from uuid import uuid4
8
 
9
- with gr.Blocks() as demo:
10
- with gr.Row():
11
- text = gr.Textbox()
12
- textb = gr.Textbox()
13
- with gr.Row():
14
- load_set_btn = gr.Button("Load Set")
15
- load_nested_set_btn = gr.Button("Load Nested Set")
16
- load_random_btn = gr.Button("Load Random")
17
- clean_imgs_btn = gr.Button("Clean Images")
18
- wait_btn = gr.Button("Wait")
19
- do_all_btn = gr.Button("Do All")
20
- track_tqdm_btn = gr.Button("Bind TQDM")
21
- bind_internal_tqdm_btn = gr.Button("Bind Internal TQDM")
22
 
23
- text2 = gr.Textbox()
 
24
 
25
- # track list
26
- def load_set(text, text2, progress=gr.Progress()):
27
- imgs = [None] * 24
28
- for img in progress.tqdm(imgs, desc="Loading from list"):
29
- time.sleep(0.1)
30
- return "done"
31
- load_set_btn.click(load_set, [text, textb], text2)
32
 
33
- # track nested list
34
- def load_nested_set(text, text2, progress=gr.Progress()):
35
- imgs = [[None] * 8] * 3
36
- for img_set in progress.tqdm(imgs, desc="Nested list"):
37
- time.sleep(2)
38
- for img in progress.tqdm(img_set, desc="inner list"):
39
- time.sleep(0.1)
40
- return "done"
41
- load_nested_set_btn.click(load_nested_set, [text, textb], text2)
42
 
43
- # track iterable of unknown length
44
- def load_random(data, progress=gr.Progress()):
45
- def yielder():
46
- for i in range(0, random.randint(15, 20)):
47
- time.sleep(0.1)
48
- yield None
49
- for img in progress.tqdm(yielder()):
50
- pass
51
- return "done"
52
- load_random_btn.click(load_random, {text, textb}, text2)
53
-
54
- # manual progress
55
- def clean_imgs(text, progress=gr.Progress()):
56
- progress(0.2, desc="Collecting Images")
57
- time.sleep(1)
58
- progress(0.5, desc="Cleaning Images")
59
- time.sleep(1.5)
60
- progress(0.8, desc="Sending Images")
61
- time.sleep(1.5)
62
- return "done"
63
- clean_imgs_btn.click(clean_imgs, text, text2)
64
-
65
- # no progress
66
- def wait(text):
67
- time.sleep(4)
68
- return "done"
69
- wait_btn.click(wait, text, text2)
70
-
71
- # multiple progressions
72
- def do_all(data, progress=gr.Progress()):
73
- load_set(data[text], data[textb], progress)
74
- load_random(data, progress)
75
- clean_imgs(data[text], progress)
76
- progress(None)
77
- wait(text)
78
- return "done"
79
- do_all_btn.click(do_all, {text, textb}, text2)
80
-
81
- def track_tqdm(data, progress=gr.Progress(track_tqdm=True)):
82
- for i in tqdm.tqdm(range(5), desc="outer"):
83
- for j in tqdm.tqdm(range(4), desc="inner"):
84
- time.sleep(1)
85
- return "done"
86
- track_tqdm_btn.click(track_tqdm, {text, textb}, text2)
87
-
88
- def bind_internal_tqdm(data, progress=gr.Progress(track_tqdm=True)):
89
- outdir = "__tmp/" + str(uuid4())
90
- load_dataset("beans", split="train", cache_dir=outdir)
91
- shutil.rmtree(outdir)
92
- return "done"
93
- bind_internal_tqdm_btn.click(bind_internal_tqdm, {text, textb}, text2)
94
-
95
-
96
- if __name__ == "__main__":
97
- demo.queue(concurrency_count=20).launch()
 
1
  import gradio as gr
2
+ from huggingface_hub import WebhooksServer, WebhookPayload
 
 
 
 
 
3
 
4
+ from model import model
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ with gr.Blocks() as ui:
7
+ progress = gr.Progress(track_tqdm=True)
8
 
9
+ # 2. Create WebhooksServer with custom UI and secret
10
+ app = WebhooksServer(ui=ui, webhook_secret="test")
 
 
 
 
 
11
 
12
+ @app.add_webhook
13
+ async def train(payload: WebhookPayload):
14
+ print("Received payload:", payload.dict())
15
+ return payload.dict()
 
 
 
 
 
16
 
17
+ # 5. Start server (optional)
18
+ app.run()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
local-requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ datasets
2
+ huggingface-hub>=0.12.1
3
+ protobuf<4
4
+ click<8.1
5
+ pydantic~=1.0
6
+ gradio[oauth]==3.50.2
7
+ uvicorn>=0.14.0
8
+ spaces==0.17.0
model.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sentence_transformers import SentenceTransformer, models
2
+ import torch
3
+
4
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
5
+
6
+ word_embedding_model = models.Transformer("BAAI/bge-base-en-v1.5", max_seq_length=512)
7
+
8
+ word_embedding_model.tokenizer.add_tokens(['[TURN]'], special_tokens=True)
9
+ word_embedding_model.tokenizer.truncation_side = 'left'
10
+ word_embedding_model.auto_model.resize_token_embeddings(len(word_embedding_model.tokenizer))
11
+
12
+ pooling_model = models.Pooling(
13
+ word_embedding_model.get_word_embedding_dimension(), pooling_mode="cls"
14
+ )
15
+
16
+ model = SentenceTransformer(modules=[word_embedding_model, pooling_model], device=device)