de-Rodrigo commited on
Commit
00b05e0
·
1 Parent(s): 4948600

Upload Images

Browse files
Files changed (2) hide show
  1. app.py +51 -22
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,12 +1,48 @@
1
- from typing import List
 
2
  import gradio as gr
3
  # from transformers import AutoModel, AutoTokenizer
4
  from huggingface_hub import list_models
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  def get_collection_models(tag: str) -> List[str]:
7
  """Get a list of models from a specific Hugging Face collection."""
8
  models = list_models(author="de-Rodrigo")
9
-
10
  model_names = []
11
  for model in models:
12
  if tag in model.tags:
@@ -20,26 +56,19 @@ def load_model(model_name: str):
20
  tokenizer = AutoTokenizer.from_pretrained(model_name)
21
  return model, tokenizer
22
 
23
- # Example processing function
24
- def process_input(text: str, model_name: str) -> str:
25
- model, tokenizer = load_model(model_name)
26
- inputs = tokenizer(text, return_tensors="pt")
27
- outputs = model(**inputs)
28
- return f"Processed output with {model_name}"
29
 
30
- # Create Gradio interface
31
- def create_interface(tag: str):
32
- iface = gr.Interface(
33
- fn=process_input,
34
- inputs=[
35
- gr.Textbox(lines=2, placeholder="Enter some text", label="Input Text"),
36
- gr.Dropdown(choices=get_collection_models(tag), label="Select Model")
37
- ],
38
- outputs=gr.Textbox(label="Model Output"),
39
- title="Hugging Face Model Selector from Collection")
40
- return iface
41
 
42
- # Specify the name of your collection
43
- filter_tag = "saliency-merit"
44
- iface = create_interface(filter_tag)
 
 
 
 
45
  iface.launch()
 
1
+ import io
2
+ import requests
3
  import gradio as gr
4
  # from transformers import AutoModel, AutoTokenizer
5
  from huggingface_hub import list_models
6
+ from datasets import load_dataset
7
+ from typing import List
8
+ from PIL import Image
9
+
10
+
11
+ def get_image_names(dataset):
12
+ return [str(i) for i in range(len(dataset))]
13
+
14
+ def get_image_from_dataset(index):
15
+ image_data = dataset[int(index)]["image"]
16
+ return image_data
17
+
18
+ def process_image(image=None, dataset_image_index=None):
19
+ if dataset_image_index:
20
+ image = get_image_from_dataset(dataset_image_index)
21
+
22
+ return image
23
+
24
+
25
+
26
+ def create_interface(tag, image_indices):
27
+ """ Create Gradio interface"""
28
+ iface = gr.Interface(
29
+ fn=process_image,
30
+ inputs=[
31
+ gr.Dropdown(choices=get_collection_models(tag), label="Select Model"),
32
+ gr.Image(type="pil", label="Upload Image"),
33
+ gr.Dropdown(choices=image_indices, label="Select one from MERIT Dataset test-set"),
34
+ ],
35
+ outputs=gr.Image(label="Output Image"),
36
+ title="Saliency Visualization",
37
+ description="Upload your image or select one from the MERIT Dataset test-set."
38
+ )
39
+ return iface
40
+
41
 
42
  def get_collection_models(tag: str) -> List[str]:
43
  """Get a list of models from a specific Hugging Face collection."""
44
  models = list_models(author="de-Rodrigo")
45
+
46
  model_names = []
47
  for model in models:
48
  if tag in model.tags:
 
56
  tokenizer = AutoTokenizer.from_pretrained(model_name)
57
  return model, tokenizer
58
 
59
+ # # Example processing function
60
+ # def process_input(text: str, model_name: str) -> str:
61
+ # model, tokenizer = load_model(model_name)
62
+ # inputs = tokenizer(text, return_tensors="pt")
63
+ # outputs = model(**inputs)
64
+ # return f"Processed output with {model_name}"
65
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
+ dataset_name = "de-Rodrigo/merit"
68
+ dataset = load_dataset(dataset_name, name="en-digital-seq", split="train", num_proc=8)
69
+ image_indices = get_image_names(dataset)
70
+
71
+ models_tag = "saliency-merit"
72
+
73
+ iface = create_interface(models_tag, image_indices)
74
  iface.launch()
requirements.txt CHANGED
@@ -4,3 +4,4 @@ huggingface_hub
4
  torch
5
  numpy
6
  Pillow
 
 
4
  torch
5
  numpy
6
  Pillow
7
+ datasets