MarcoParola commited on
Commit
5793f6d
·
1 Parent(s): 29b91e4

first commit

Browse files
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import csv
4
+ import os
5
+ from huggingface_hub import HfApi, HfFolder
6
+ import yaml
7
+ from src.utils import load_images, load_words, save_results, load_global_variable
8
+
9
+ # set css style for gallery, specifically for the width of the container
10
+ css = """
11
+ #gallery {
12
+ height: 300px;
13
+ }
14
+ """
15
+
16
+
17
+
18
+ def main():
19
+
20
+ config = yaml.safe_load(open("config/config.yaml"))
21
+ print(config)
22
+
23
+ with gr.Blocks(theme=gr.themes.Glass(), css=css) as demo:
24
+ title = gr.Markdown(f"# My Gradio App Title")
25
+ welcome_message = gr.Markdown("# Welcome to the Gradio App!")
26
+
27
+ # Load global variable, images, and words after confirming alert
28
+ global_var = load_global_variable()
29
+ images = load_images(global_var)
30
+ words = load_words(global_var)
31
+
32
+ image_gallery = gr.Gallery(images, columns=10, elem_classes="gallery-container", elem_id="gallery")
33
+ checkbox_group = gr.CheckboxGroup(words, label="Select 3 words")
34
+
35
+ result_message = gr.Textbox(label="Result")
36
+
37
+ submit_button = gr.Button("Submit")
38
+ submit_button.click(save_results, inputs=checkbox_group, outputs=result_message)
39
+
40
+ demo.load(lambda: None, inputs=None, outputs=welcome_message)
41
+
42
+ demo.launch()
43
+
44
+
45
+ if __name__ == "__main__":
46
+ main()
config/config.yaml ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ data_dir: data
2
+ image_dir: images
3
+ saliency_dir: saliency
4
+
5
+ repo_id: "MarcoParola/saliency-evaluation"
6
+
7
+ dataset:
8
+ intel-image:
9
+ n_classes: 6
10
+ class_names: ['building', 'forest', 'glacier', 'mountain', 'sea', 'street']
11
+ imagenette:
12
+ n_classes: 10
13
+ class_names: ['tench', 'English springer', 'cassette player', 'chain saw', 'church', 'French horn', 'garbage truck', 'gas pump', 'golf ball', 'parachute']
data/images/1600.jpg ADDED
data/images/1605.jpg ADDED
data/images/1608.jpg ADDED
data/images/1617.jpg ADDED
data/images/1642.jpg ADDED
data/images/1652.jpg ADDED
data/images/1655.jpg ADDED
data/images/1657.jpg ADDED
data/images/1666.jpg ADDED
data/images/1699.jpg ADDED
data/images/1700.jpg ADDED
data/images/1705.jpg ADDED
data/images/1712.jpg ADDED
data/images/1715.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ hydra-core
3
+ huggingface_hub
src/__pycache__/utils.cpython-310.pyc ADDED
Binary file (1.96 kB). View file
 
src/utils.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pandas as pd
3
+ from huggingface_hub import HfApi, HfFolder
4
+ import yaml
5
+
6
+
7
+ config = yaml.safe_load(open("./config/config.yaml"))
8
+
9
+ # Function to load global variable from CSV
10
+ def load_global_variable():
11
+ if os.path.exists('global_variable.csv'):
12
+ df = pd.read_csv('global_variable.csv')
13
+ return df['value'][0]
14
+ else:
15
+ return 0
16
+
17
+ def load_images(global_var):
18
+ image_dir = os.path.join(config["data_dir"], config["image_dir"])
19
+ #images = [f"image_{global_var}_{i}.jpg" for i in range(10)]
20
+ images = [os.path.join(image_dir, f) for f in os.listdir(image_dir) if os.path.isfile(os.path.join(image_dir, f))]
21
+ return images
22
+
23
+ # Function to load words based on global variable
24
+ def load_words(global_var):
25
+ words = [f"word_{global_var}_{i}" for i in range(10)]
26
+ return words
27
+
28
+ # Function to save results and increment global variable
29
+ def save_results(results):
30
+
31
+ filename = "results.txt"
32
+ results_str = "\n".join(results)
33
+ with open(filename, 'w') as f:
34
+ f.write(results_str)
35
+
36
+ # Upload the file to Hugging Face Hub
37
+ api = HfApi()
38
+ token = os.getenv("HUGGINGFACE_TOKEN")
39
+ #token = HfFolder.get_token()
40
+
41
+ if not token:
42
+ print("Token not found. Please login to Hugging Face.")
43
+ return
44
+
45
+ try:
46
+ api.upload_file(
47
+ path_or_fileobj=filename,
48
+ path_in_repo=filename,
49
+ repo_id=config["repo_id"],
50
+ token=token,
51
+ commit_message="Add results.txt"
52
+ )
53
+ print(f"File {filename} uploaded successfully to {config['repo_id']}.")
54
+ except Exception as e:
55
+ print(f"Failed to upload file: {e}")