dn6 HF staff commited on
Commit
d35f312
β€’
1 Parent(s): f1c460c

add application file

Browse files
Files changed (2) hide show
  1. app.py +102 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import comet_ml
2
+ import uuid
3
+ import gradio as gr
4
+
5
+
6
+ class MyProject:
7
+ def __init__(self):
8
+ self.experiment = None
9
+
10
+ def start_experiment(
11
+ self, comet_api_key: str, comet_workspace: str, comet_project_name: str
12
+ ):
13
+ if not comet_api_key:
14
+ return """
15
+ Please add your API key in order to log your predictions to a Comet Experiment.
16
+ If you don't have a Comet account yet, you can sign up here:
17
+
18
+ https://www.comet.ml/signup
19
+ """
20
+
21
+ else:
22
+ try:
23
+ self.experiment = comet_ml.Experiment(
24
+ api_key=comet_api_key,
25
+ workspace=comet_workspace,
26
+ project_name=comet_project_name,
27
+ )
28
+ self.experiment.add_tags(["spaces"])
29
+
30
+ return f"Started {self.experiment.name}. Happy logging!😊"
31
+
32
+ except Exception as e:
33
+ return e
34
+
35
+ def end_experiment(self):
36
+ if self.experiment is not None:
37
+ self.experiment.end()
38
+ return f"Ended {self.experiment.name}"
39
+
40
+ def start_comet_interface(self):
41
+ demo = gr.Blocks()
42
+ with demo:
43
+ # credentials
44
+ comet_api_key = gr.Textbox(label="Comet API Key")
45
+ comet_workspace = gr.Textbox(label="Comet Workspace")
46
+ comet_project_name = gr.Textbox(label="Comet Project Name")
47
+
48
+ with gr.Row():
49
+ start_experiment = gr.Button("Start Experiment", variant="primary")
50
+ end_experiment = gr.Button("End Experiment", variant="secondary")
51
+
52
+ output = gr.Textbox(label="Status")
53
+
54
+ start_experiment.click(
55
+ self.start_experiment,
56
+ inputs=[
57
+ comet_api_key,
58
+ comet_workspace,
59
+ comet_project_name,
60
+ ],
61
+ outputs=output,
62
+ )
63
+ end_experiment.click(self.end_experiment, inputs=None, outputs=output)
64
+
65
+ return demo
66
+
67
+ def predict(
68
+ self,
69
+ model,
70
+ prompt,
71
+ ):
72
+ io = gr.Interface.load(model)
73
+ image = io(prompt)
74
+
75
+ if self.experiment is not None:
76
+ image_id = uuid.uuid4().hex
77
+ self.experiment.log_image(image, name=image_id)
78
+ self.experiment.log_text(
79
+ prompt, metadata={"image_id": image_id, "model": model}
80
+ )
81
+
82
+ return image
83
+
84
+ def load_interface(self):
85
+ model = gr.Textbox(label="Model", value="spaces/valhalla/glide-text2im")
86
+ prompt = gr.Textbox(label="Prompt")
87
+
88
+ outputs = gr.Image(label="Image")
89
+
90
+ interface = gr.Interface(self.predict, inputs=[model, prompt], outputs=outputs)
91
+
92
+ return interface
93
+
94
+ def launch(self):
95
+ interface = gr.TabbedInterface(
96
+ [self.start_comet_interface(), self.load_interface()],
97
+ tab_names=["Comet Settings", "Diffusion Model"],
98
+ )
99
+ interface.launch()
100
+
101
+
102
+ MyProject().launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ comet_ml
2
+ gradio