Commit ·
c49a9ad
1
Parent(s): 55e8762
basic ui
Browse filesFiles changed (6)
8 unmodified lines910111212131415168 unmodified linespinned: false---
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference## RUN Gradio Locally```pip install gradio gradio app.py```1234534678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455675657import gradio as grimport requestsimport randomfrom src.classification_model import ClassificationModel
def greet(name): return "Hello " + name + "!!"#only for dummy dataresponse = requests.get("https://git.io/JJkYN")labels = response.text.split("\n")
clf = ClassificationModel()model_names = clf.get_model_names()output_labels = []
def predict(models, img_urls, img_files): print(f'model choosen: {models}') model_predictions = {}
#set all labels visibility to false for i, name in enumerate(model_names): model_predictions[output_labels[i]] = gr.Label(label=f'# {name}', visible=False) print(f'id {i} invisible') for m in models: idx = model_names.index(m) print(f' {m} idx: ', idx) result = {labels[random.randrange(0, len(labels))]: random.uniform(0, 1.0) for i in range(5)} model_predictions[output_labels[idx]] = gr.Label(label=f'# {m}, 3 seconds', value=result, visible=True) return model_predictions
with gr.Blocks() as demo: gr.Markdown("# Image Classification Benchmark") with gr.Row(): with gr.Column(scale=1): model = gr.Dropdown(choices=model_names, multiselect=True, label='Choose the model') img_urls = gr.Textbox(label='Image Urls (separated with comma)') img_files = gr.File(label='Upload Files',file_count='multiple', file_types=['image']) apply = gr.Button("Classify", variant='primary') with gr.Column(scale=1): for name in clf.get_model_names(): output_labels.append(gr.Label(label=f'# {name}'))
apply.click(fn=predict, inputs=[model, img_urls, img_files], outputs=output_labels)
if __name__ == "__main__": demo.launch()
# inputs = [# gr.Dropdown(choices=clf.get_model_names(), multiselect=True)# ]
iface = gr.Interface(fn=greet, inputs="text", outputs="text")iface.launch()# iface = gr.Interface(fn=greet, inputs=inputs, outputs="text")# iface.launch()123456789101112131415161718192021222324252627282930from .model_data import ModelData
class ClassificationModel: """ Base class for all classification models. """
def __init__(self): self.models = self.initialize_models()
def get_model_names(self): return [model.name for model in self.models]
def get_model_data(self, model_name): for model in self.models: if model.name == model_name: return model raise Exception(f'Model {model_name} not found')
def initialize_models(self): return [ ModelData('clip-vit-base-patch32'), ModelData('mobilenet_v3') ]
def load_model(self): """ Loads the model from the model path. """ raise NotImplementedError12345from dataclasses import dataclass
@dataclassclass ModelData: name: str README.md+5-1
app.py+54-4
src/__pycache__/classification_model.cpython-312.pycAdded
src/__pycache__/model_data.cpython-312.pycAdded
src/classification_model.pyAdded+30
src/model_data.pyAdded+5