add image save function
Browse files- app.py +36 -15
- requirements.txt +4 -1
app.py
CHANGED
|
@@ -12,6 +12,11 @@ from utils import SCENARIOS, PROMPTS, IMAGES, OPTIONS, T2I_MODELS, INSTRUCTION,
|
|
| 12 |
import spaces #[uncomment to use ZeroGPU]
|
| 13 |
import transformers
|
| 14 |
import gspread
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
CLIP_MODEL = "ViT-H-14"
|
| 17 |
PRETRAINED_CLIP = "laion2b_s32b_b79k"
|
|
@@ -36,13 +41,14 @@ DEFAULT_SCENARIO = "Product advertisement"
|
|
| 36 |
METHODS = ["Baseline", "Experimental"]
|
| 37 |
MAX_ROUND = 5
|
| 38 |
|
| 39 |
-
# intermittent memory
|
| 40 |
counter1, counter2 = 1, 1
|
| 41 |
responses_memory = {}
|
| 42 |
assigned_scenarios = list(SCENARIOS.keys())[:2]
|
| 43 |
current_task1, current_task2 = METHODS # current task 1 (tab 1)
|
| 44 |
task1_success, task2_success = False, False
|
| 45 |
enable_submit1, enable_submit2 = False, False
|
|
|
|
|
|
|
| 46 |
|
| 47 |
########################################################################################################
|
| 48 |
# Generating images with two methods
|
|
@@ -293,9 +299,20 @@ def redesign(participant, scenario, prompt, sim_radio, like_radio, dislike_radio
|
|
| 293 |
else:
|
| 294 |
return {submit_btn1: gr.skip()} if active_tab == "Task A" else {submit_btn2: gr.skip()}
|
| 295 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
-
def save_response(participant, scenario, prompt, sim_radio, like_radio, dislike_radio, active_tab):
|
| 298 |
-
global current_task1, current_task2 # not change
|
| 299 |
global task1_success, task2_success, counter1, counter2, enable_submit1, enable_submit2, responses_memory, assigned_scenarios # will change
|
| 300 |
|
| 301 |
method = current_task1 if active_tab == "Task A" else current_task2
|
|
@@ -310,11 +327,16 @@ def save_response(participant, scenario, prompt, sim_radio, like_radio, dislike_
|
|
| 310 |
responses_memory[participant][method][counter]["unsatisfied_img"] = f"round {counter}, {dislike_radio}"
|
| 311 |
|
| 312 |
try:
|
|
|
|
| 313 |
gc = gspread.service_account(filename='credentials.json')
|
| 314 |
sheet = gc.open("DiverseGen-phase3").sheet1
|
| 315 |
-
|
| 316 |
for i, entry in responses_memory[participant][method].items():
|
| 317 |
sheet.append_row([participant, scenario, f"{active_tab}, {method}", i, entry["prompt"], entry["sim_radio"], entry["response"], entry["satisfied_img"], entry["unsatisfied_img"]])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
|
| 319 |
display_info_message("β
Your answer is saved!")
|
| 320 |
|
|
@@ -330,7 +352,6 @@ def save_response(participant, scenario, prompt, sim_radio, like_radio, dislike_
|
|
| 330 |
else:
|
| 331 |
task2_success = True
|
| 332 |
|
| 333 |
-
|
| 334 |
# decide if change scenario
|
| 335 |
# if scenario == assigned_scenarios[0]:
|
| 336 |
# next_scenario = assigned_scenarios[1] if task1_success and task2_success else assigned_scenarios[0]
|
|
@@ -426,11 +447,11 @@ with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Inconsolata"), "
|
|
| 426 |
|
| 427 |
with gr.Row(elem_id="compact-row"):
|
| 428 |
with gr.Column(elem_id="col-container"):
|
| 429 |
-
images_method1 = gr.Gallery(label="Images", columns=[4], rows=[1], height=200, elem_id="gallery")
|
| 430 |
-
history_images1 = gr.Gallery(label="History Images", columns=[4], rows=[1], elem_id="gallery")
|
| 431 |
with gr.Column(elem_id="col-container3"):
|
| 432 |
-
like_image1 = gr.Image(label="Satisfied Image", width=200, height=200, sources='upload', type="
|
| 433 |
-
dislike_image1 = gr.Image(label="Unsatisfied Image", width=200, height=200, sources='upload', type="
|
| 434 |
with gr.Column(elem_id="col-container2"):
|
| 435 |
gr.Markdown("### π Evaluation")
|
| 436 |
sim_radio1 = gr.Radio(
|
|
@@ -481,11 +502,11 @@ with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Inconsolata"), "
|
|
| 481 |
|
| 482 |
with gr.Row(elem_id="compact-row"):
|
| 483 |
with gr.Column(elem_id="col-container"):
|
| 484 |
-
images_method2 = gr.Gallery(label="Images", columns=[4], rows=[1], height=200, elem_id="gallery")
|
| 485 |
-
history_images2 = gr.Gallery(label="History Images", columns=[4], rows=[1], elem_id="gallery")
|
| 486 |
with gr.Column(elem_id="col-container3"):
|
| 487 |
-
like_image2 = gr.Image(label="Satisfied Image", width=200, height=200, sources='upload', type="
|
| 488 |
-
dislike_image2 = gr.Image(label="Unsatisfied Image", width=200, height=200, sources='upload', type="
|
| 489 |
|
| 490 |
with gr.Column(elem_id="col-container2"):
|
| 491 |
gr.Markdown("### π Evaluation")
|
|
@@ -552,11 +573,11 @@ with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Inconsolata"), "
|
|
| 552 |
outputs=[sim_radio2, dislike_radio2, like_radio2, images_method2, history_images2, example2.dataset, prompt2, next_btn2, redesign_btn2, submit_btn2]
|
| 553 |
)
|
| 554 |
submit_btn1.click(fn=save_response,
|
| 555 |
-
inputs=[participant, scenario, prompt1, sim_radio1, like_radio1, dislike_radio1, active_tab],
|
| 556 |
outputs=[sim_radio1, dislike_radio1, like_radio1, prompt1, next_btn1, redesign_btn1, submit_btn1, tabs])
|
| 557 |
|
| 558 |
submit_btn2.click(fn=save_response,
|
| 559 |
-
inputs=[participant, scenario, prompt2, sim_radio2, like_radio2, dislike_radio2, active_tab],
|
| 560 |
outputs=[sim_radio2, dislike_radio2, like_radio2, prompt2, next_btn2, redesign_btn2, submit_btn2, tabs])
|
| 561 |
|
| 562 |
|
|
|
|
| 12 |
import spaces #[uncomment to use ZeroGPU]
|
| 13 |
import transformers
|
| 14 |
import gspread
|
| 15 |
+
from googleapiclient.discovery import build
|
| 16 |
+
from googleapiclient.http import MediaFileUpload
|
| 17 |
+
from googleapiclient.errors import HttpError
|
| 18 |
+
from google.oauth2.service_account import Credentials
|
| 19 |
+
|
| 20 |
|
| 21 |
CLIP_MODEL = "ViT-H-14"
|
| 22 |
PRETRAINED_CLIP = "laion2b_s32b_b79k"
|
|
|
|
| 41 |
METHODS = ["Baseline", "Experimental"]
|
| 42 |
MAX_ROUND = 5
|
| 43 |
|
|
|
|
| 44 |
counter1, counter2 = 1, 1
|
| 45 |
responses_memory = {}
|
| 46 |
assigned_scenarios = list(SCENARIOS.keys())[:2]
|
| 47 |
current_task1, current_task2 = METHODS # current task 1 (tab 1)
|
| 48 |
task1_success, task2_success = False, False
|
| 49 |
enable_submit1, enable_submit2 = False, False
|
| 50 |
+
scopes = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive']
|
| 51 |
+
|
| 52 |
|
| 53 |
########################################################################################################
|
| 54 |
# Generating images with two methods
|
|
|
|
| 299 |
else:
|
| 300 |
return {submit_btn1: gr.skip()} if active_tab == "Task A" else {submit_btn2: gr.skip()}
|
| 301 |
|
| 302 |
+
def save_image(creds, like_image, dislike_image, name):
|
| 303 |
+
try:
|
| 304 |
+
service = build("drive", "v3", credentials=creds)
|
| 305 |
+
for image_path, suffix in zip([like_image, dislike_image], ["satisfied", "unsatisfied"]):
|
| 306 |
+
filename = f"{name}_{suffix}"
|
| 307 |
+
file_metadata = {"name": filename, "parents": ["1ru3-QbbzyVSk-1kBfVv4nhElFqYh3ITj"]}
|
| 308 |
+
media = MediaFileUpload(image_path, mimetype="image/png")
|
| 309 |
+
uploaded_file = service.files().create(body=file_metadata, media_body=media, fields="id").execute()
|
| 310 |
+
|
| 311 |
+
except HttpError as error:
|
| 312 |
+
print(f"An error occurred: {error}")
|
| 313 |
|
| 314 |
+
def save_response(participant, scenario, prompt, sim_radio, like_radio, dislike_radio, like_image, dislike_image, active_tab):
|
| 315 |
+
global current_task1, current_task2, scopes # not change
|
| 316 |
global task1_success, task2_success, counter1, counter2, enable_submit1, enable_submit2, responses_memory, assigned_scenarios # will change
|
| 317 |
|
| 318 |
method = current_task1 if active_tab == "Task A" else current_task2
|
|
|
|
| 327 |
responses_memory[participant][method][counter]["unsatisfied_img"] = f"round {counter}, {dislike_radio}"
|
| 328 |
|
| 329 |
try:
|
| 330 |
+
# save responses in google sheet
|
| 331 |
gc = gspread.service_account(filename='credentials.json')
|
| 332 |
sheet = gc.open("DiverseGen-phase3").sheet1
|
| 333 |
+
|
| 334 |
for i, entry in responses_memory[participant][method].items():
|
| 335 |
sheet.append_row([participant, scenario, f"{active_tab}, {method}", i, entry["prompt"], entry["sim_radio"], entry["response"], entry["satisfied_img"], entry["unsatisfied_img"]])
|
| 336 |
+
|
| 337 |
+
# save images in google drive
|
| 338 |
+
creds = Credentials.from_service_account_file('credentials.json',scopes=scopes)
|
| 339 |
+
save_image(creds, like_image, dislike_image, f"{participant}_{scenario}_{active_tab}_{method}")
|
| 340 |
|
| 341 |
display_info_message("β
Your answer is saved!")
|
| 342 |
|
|
|
|
| 352 |
else:
|
| 353 |
task2_success = True
|
| 354 |
|
|
|
|
| 355 |
# decide if change scenario
|
| 356 |
# if scenario == assigned_scenarios[0]:
|
| 357 |
# next_scenario = assigned_scenarios[1] if task1_success and task2_success else assigned_scenarios[0]
|
|
|
|
| 447 |
|
| 448 |
with gr.Row(elem_id="compact-row"):
|
| 449 |
with gr.Column(elem_id="col-container"):
|
| 450 |
+
images_method1 = gr.Gallery(label="Images", columns=[4], rows=[1], height=200, elem_id="gallery", type="png")
|
| 451 |
+
history_images1 = gr.Gallery(label="History Images", columns=[4], rows=[1], elem_id="gallery", type="png")
|
| 452 |
with gr.Column(elem_id="col-container3"):
|
| 453 |
+
like_image1 = gr.Image(label="Satisfied Image", width=200, height=200, sources='upload', format="png", type="filepath")
|
| 454 |
+
dislike_image1 = gr.Image(label="Unsatisfied Image", width=200, height=200, sources='upload', format="png", type="filepath")
|
| 455 |
with gr.Column(elem_id="col-container2"):
|
| 456 |
gr.Markdown("### π Evaluation")
|
| 457 |
sim_radio1 = gr.Radio(
|
|
|
|
| 502 |
|
| 503 |
with gr.Row(elem_id="compact-row"):
|
| 504 |
with gr.Column(elem_id="col-container"):
|
| 505 |
+
images_method2 = gr.Gallery(label="Images", columns=[4], rows=[1], height=200, elem_id="gallery", type="png")
|
| 506 |
+
history_images2 = gr.Gallery(label="History Images", columns=[4], rows=[1], elem_id="gallery", type="png")
|
| 507 |
with gr.Column(elem_id="col-container3"):
|
| 508 |
+
like_image2 = gr.Image(label="Satisfied Image", width=200, height=200, sources='upload', format="png", type="filepath")
|
| 509 |
+
dislike_image2 = gr.Image(label="Unsatisfied Image", width=200, height=200, sources='upload', format="png", type="filepath")
|
| 510 |
|
| 511 |
with gr.Column(elem_id="col-container2"):
|
| 512 |
gr.Markdown("### π Evaluation")
|
|
|
|
| 573 |
outputs=[sim_radio2, dislike_radio2, like_radio2, images_method2, history_images2, example2.dataset, prompt2, next_btn2, redesign_btn2, submit_btn2]
|
| 574 |
)
|
| 575 |
submit_btn1.click(fn=save_response,
|
| 576 |
+
inputs=[participant, scenario, prompt1, sim_radio1, like_radio1, dislike_radio1, like_image1, dislike_image1, active_tab],
|
| 577 |
outputs=[sim_radio1, dislike_radio1, like_radio1, prompt1, next_btn1, redesign_btn1, submit_btn1, tabs])
|
| 578 |
|
| 579 |
submit_btn2.click(fn=save_response,
|
| 580 |
+
inputs=[participant, scenario, prompt2, sim_radio2, like_radio2, dislike_radio2, like_image2, dislike_image2, active_tab],
|
| 581 |
outputs=[sim_radio2, dislike_radio2, like_radio2, prompt2, next_btn2, redesign_btn2, submit_btn2, tabs])
|
| 582 |
|
| 583 |
|
requirements.txt
CHANGED
|
@@ -10,4 +10,7 @@ sentence-transformers
|
|
| 10 |
ftfy
|
| 11 |
mediapy
|
| 12 |
open-clip-torch==2.24.0
|
| 13 |
-
gspread
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
ftfy
|
| 11 |
mediapy
|
| 12 |
open-clip-torch==2.24.0
|
| 13 |
+
gspread
|
| 14 |
+
google-api-python-client
|
| 15 |
+
google-auth-httplib2
|
| 16 |
+
google-auth-oauthlib
|