Hacker1337 commited on
Commit
0c21c10
·
1 Parent(s): 2904d0e

initial version of my own gradio app.

Browse files
Files changed (1) hide show
  1. app.py +58 -91
app.py CHANGED
@@ -1,10 +1,11 @@
 
1
  from transformers import AutoModelForSequenceClassification
2
  import gradio as gr
3
  import numpy as np
4
  import random
5
 
6
  # import spaces #[uncomment to use ZeroGPU]
7
- from diffusers import DiffusionPipeline
8
  import torch
9
 
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -16,9 +17,12 @@ else:
16
  torch_dtype = torch.float32
17
 
18
 
19
- from article_classifier.dataset import labels, id2label, label2id, categorie2human
 
 
 
 
20
 
21
- model_path = "distilbert/distilbert-base-cased" # todo, replace with hacker1337/article-classifier
22
  model = AutoModelForSequenceClassification.from_pretrained(
23
  model_path,
24
  num_labels=len(id2label),
@@ -27,12 +31,6 @@ model = AutoModelForSequenceClassification.from_pretrained(
27
  problem_type="multi_label_classification",
28
  )
29
 
30
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
31
- pipe = pipe.to(device)
32
-
33
- MAX_SEED = np.iinfo(np.int32).max
34
- MAX_IMAGE_SIZE = 1024
35
-
36
 
37
  # @spaces.GPU #[uncomment to use ZeroGPU]
38
  def infer(
@@ -51,18 +49,35 @@ def infer(
51
 
52
  generator = torch.Generator().manual_seed(seed)
53
 
54
- image = pipe(
55
- prompt=prompt,
56
- negative_prompt=negative_prompt,
57
- guidance_scale=guidance_scale,
58
- num_inference_steps=num_inference_steps,
59
- width=width,
60
- height=height,
61
- generator=generator,
62
- ).images[0]
 
 
63
 
64
- return image, seed
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  examples = [
68
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
@@ -81,86 +96,38 @@ with gr.Blocks(css=css) as demo:
81
  with gr.Column(elem_id="col-container"):
82
  gr.Markdown(" # Text-to-Image Gradio Template")
83
 
84
- with gr.Row():
85
- prompt = gr.Text(
86
- label="Prompt",
87
- show_label=False,
88
- max_lines=1,
89
- placeholder="Enter your prompt",
90
- container=False,
91
- )
 
 
 
 
 
 
92
 
93
- run_button = gr.Button("Run", scale=0, variant="primary")
 
94
 
95
  result = gr.Image(label="Result", show_label=False)
96
 
97
- with gr.Accordion("Advanced Settings", open=False):
98
- negative_prompt = gr.Text(
99
- label="Negative prompt",
100
- max_lines=1,
101
- placeholder="Enter a negative prompt",
102
- visible=False,
103
- )
104
-
105
- seed = gr.Slider(
106
- label="Seed",
107
- minimum=0,
108
- maximum=MAX_SEED,
109
- step=1,
110
- value=0,
111
- )
112
-
113
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
114
-
115
- with gr.Row():
116
- width = gr.Slider(
117
- label="Width",
118
- minimum=256,
119
- maximum=MAX_IMAGE_SIZE,
120
- step=32,
121
- value=1024, # Replace with defaults that work for your model
122
- )
123
-
124
- height = gr.Slider(
125
- label="Height",
126
- minimum=256,
127
- maximum=MAX_IMAGE_SIZE,
128
- step=32,
129
- value=1024, # Replace with defaults that work for your model
130
- )
131
-
132
- with gr.Row():
133
- guidance_scale = gr.Slider(
134
- label="Guidance scale",
135
- minimum=0.0,
136
- maximum=10.0,
137
- step=0.1,
138
- value=0.0, # Replace with defaults that work for your model
139
- )
140
-
141
- num_inference_steps = gr.Slider(
142
- label="Number of inference steps",
143
- minimum=1,
144
- maximum=50,
145
- step=1,
146
- value=2, # Replace with defaults that work for your model
147
- )
148
-
149
- gr.Examples(examples=examples, inputs=[prompt])
150
  gr.on(
151
- triggers=[run_button.click, prompt.submit],
152
  fn=infer,
153
  inputs=[
154
- prompt,
155
- negative_prompt,
156
- seed,
157
- randomize_seed,
158
- width,
159
- height,
160
- guidance_scale,
161
- num_inference_steps,
162
  ],
163
- outputs=[result, seed],
164
  )
165
 
166
  if __name__ == "__main__":
 
1
+ import os
2
  from transformers import AutoModelForSequenceClassification
3
  import gradio as gr
4
  import numpy as np
5
  import random
6
 
7
  # import spaces #[uncomment to use ZeroGPU]
8
+ # from diffusers import DiffusionPipeline
9
  import torch
10
 
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
17
  torch_dtype = torch.float32
18
 
19
 
20
+ from dataset import labels, id2label, label2id, categorie2human
21
+
22
+ # model_path = "distilbert/distilbert-base-cased" # todo, replace with hacker1337/article-classifier
23
+ # model_path = os.path.expanduser(r"~\cache\huggingface\checkpoints\distilbert-arxiv\runs\Jun21_00-41-18_amir-xp")
24
+ model_path = os.path.expanduser("~/.cache/huggingface/checkpoints/distilbert-arxiv")
25
 
 
26
  model = AutoModelForSequenceClassification.from_pretrained(
27
  model_path,
28
  num_labels=len(id2label),
 
31
  problem_type="multi_label_classification",
32
  )
33
 
 
 
 
 
 
 
34
 
35
  # @spaces.GPU #[uncomment to use ZeroGPU]
36
  def infer(
 
49
 
50
  generator = torch.Generator().manual_seed(seed)
51
 
52
+ # image = pipe(
53
+ # prompt=prompt,
54
+ # negative_prompt=negative_prompt,
55
+ # guidance_scale=guidance_scale,
56
+ # num_inference_steps=num_inference_steps,
57
+ # width=width,
58
+ # height=height,
59
+ # generator=generator,
60
+ # ).images[0]
61
+
62
+ # return image, seed
63
 
 
64
 
65
+ examples_titles = [
66
+ "Survey on Semantic Stereo Matching",
67
+ ]
68
+ examples_summaries = [
69
+ """Stereo matching is one of the widely used techniques for inferring depth from
70
+ stereo images owing to its robustness and speed. It has become one of the major
71
+ topics of research since it finds its applications in autonomous driving,
72
+ robotic navigation, 3D reconstruction, and many other fields. Finding pixel
73
+ correspondences in non-textured, occluded and reflective areas is the major
74
+ challenge in stereo matching. Recent developments have shown that semantic cues
75
+ from image segmentation can be used to improve the results of stereo matching.
76
+ Many deep neural network architectures have been proposed to leverage the
77
+ advantages of semantic segmentation in stereo matching. This paper aims to give
78
+ a comparison among the state of art networks both in terms of accuracy and in
79
+ terms of speed which are of higher importance in real-time applications.""",
80
+ ]
81
 
82
  examples = [
83
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
 
96
  with gr.Column(elem_id="col-container"):
97
  gr.Markdown(" # Text-to-Image Gradio Template")
98
 
99
+ title_prompt = gr.Text(
100
+ label="Title Prompt",
101
+ show_label=False,
102
+ max_lines=1,
103
+ placeholder="Enter paper's title",
104
+ container=False,
105
+ )
106
+ summary_prompt = gr.Text(
107
+ label="Summary Prompt",
108
+ show_label=False,
109
+ max_lines=10,
110
+ placeholder="Enter paper's abstract",
111
+ container=False,
112
+ )
113
 
114
+
115
+ run_button = gr.Button("Run", scale=0, variant="primary")
116
 
117
  result = gr.Image(label="Result", show_label=False)
118
 
119
+ # with gr.Accordion("Advanced Settings", open=False):
120
+
121
+ gr.Examples(examples=examples_titles, inputs=[title_prompt])
122
+ gr.Examples(examples=examples_summaries, inputs=[summary_prompt])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  gr.on(
124
+ triggers=[run_button.click, title_prompt.submit, summary_prompt.submit],
125
  fn=infer,
126
  inputs=[
127
+ title_prompt,
128
+ summary_prompt,
 
 
 
 
 
 
129
  ],
130
+ outputs=[result],
131
  )
132
 
133
  if __name__ == "__main__":