AnnasBlackHat commited on
Commit
c49a9ad
·
1 Parent(s): 55e8762
README.md CHANGED
@@ -9,4 +9,8 @@ app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
9
  pinned: false
10
  ---
11
 
12
+ ## RUN Gradio Locally
13
+ ```
14
+ pip install gradio
15
+ gradio app.py
16
+ ```
app.py CHANGED
@@ -1,7 +1,57 @@
1
  import gradio as gr
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import requests
3
+ import random
4
+ from src.classification_model import ClassificationModel
5
 
6
+ #only for dummy data
7
+ response = requests.get("https://git.io/JJkYN")
8
+ labels = response.text.split("\n")
9
 
10
+ clf = ClassificationModel()
11
+ model_names = clf.get_model_names()
12
+ output_labels = []
13
+
14
+ def predict(models, img_urls, img_files):
15
+ print(f'model choosen: {models}')
16
+ model_predictions = {}
17
+
18
+ #set all labels visibility to false
19
+ for i, name in enumerate(model_names):
20
+ model_predictions[output_labels[i]] = gr.Label(label=f'# {name}', visible=False)
21
+ print(f'id {i} invisible')
22
+
23
+ for m in models:
24
+ idx = model_names.index(m)
25
+ print(f' {m} idx: ', idx)
26
+ result = {labels[random.randrange(0, len(labels))]: random.uniform(0, 1.0) for i in range(5)}
27
+ model_predictions[output_labels[idx]] = gr.Label(label=f'# {m}, 3 seconds', value=result, visible=True)
28
+
29
+ return model_predictions
30
+
31
+ with gr.Blocks() as demo:
32
+ gr.Markdown("# Image Classification Benchmark")
33
+
34
+ with gr.Row():
35
+ with gr.Column(scale=1):
36
+ model = gr.Dropdown(choices=model_names, multiselect=True, label='Choose the model')
37
+ img_urls = gr.Textbox(label='Image Urls (separated with comma)')
38
+ img_files = gr.File(label='Upload Files',file_count='multiple', file_types=['image'])
39
+ apply = gr.Button("Classify", variant='primary')
40
+ with gr.Column(scale=1):
41
+ for name in clf.get_model_names():
42
+ output_labels.append(gr.Label(label=f'# {name}'))
43
+
44
+ apply.click(fn=predict,
45
+ inputs=[model, img_urls, img_files],
46
+ outputs=output_labels)
47
+
48
+
49
+ if __name__ == "__main__":
50
+ demo.launch()
51
+
52
+ # inputs = [
53
+ # gr.Dropdown(choices=clf.get_model_names(), multiselect=True)
54
+ # ]
55
+
56
+ # iface = gr.Interface(fn=greet, inputs=inputs, outputs="text")
57
+ # iface.launch()
src/__pycache__/classification_model.cpython-312.pyc ADDED
Binary file (1.76 kB). View file
 
src/__pycache__/model_data.cpython-312.pyc ADDED
Binary file (443 Bytes). View file
 
src/classification_model.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .model_data import ModelData
2
+
3
+ class ClassificationModel:
4
+ """
5
+ Base class for all classification models.
6
+ """
7
+
8
+ def __init__(self):
9
+ self.models = self.initialize_models()
10
+
11
+ def get_model_names(self):
12
+ return [model.name for model in self.models]
13
+
14
+ def get_model_data(self, model_name):
15
+ for model in self.models:
16
+ if model.name == model_name:
17
+ return model
18
+ raise Exception(f'Model {model_name} not found')
19
+
20
+ def initialize_models(self):
21
+ return [
22
+ ModelData('clip-vit-base-patch32'),
23
+ ModelData('mobilenet_v3')
24
+ ]
25
+
26
+ def load_model(self):
27
+ """
28
+ Loads the model from the model path.
29
+ """
30
+ raise NotImplementedError
src/model_data.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ from dataclasses import dataclass
2
+
3
+ @dataclass
4
+ class ModelData:
5
+ name: str