Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: enlarge character pictures on click #4654

Merged
merged 4 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -648,3 +648,19 @@ div.svelte-362y77>*, div.svelte-362y77>.form>* {
.options {
z-index: 100 !important;
}

/* ----------------------------------------------
Big profile picture for characters
---------------------------------------------- */
.bigProfilePicture {
position: fixed;
bottom: 0;
left: 0;
width: calc((100vw - 880px - 120px) /2);
}

@media screen and (width <= 1300px) {
.bigProfilePicture {
display: none;
}
}
39 changes: 39 additions & 0 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ document.addEventListener("click", function (event) {
if (!isMouseOverButtonOrMenu() && menu.style.display === "flex") {
hideMenu();
}

if (event.target.classList.contains("pfp_character")) {
toggleBigPicture();
}
});

//------------------------------------------------
Expand All @@ -335,3 +339,38 @@ document.getElementById("show-controls").parentNode.style.bottom = "0px";
// Focus on the chat input
//------------------------------------------------
document.querySelector("#chat-input textarea").focus();

//------------------------------------------------
// Show enlarged character picture when the profile
// picture is clicked on
//------------------------------------------------
let bigPictureVisible = false;

function addBigPicture() {
var imgElement = document.createElement("img");
var timestamp = new Date().getTime();
imgElement.src = "/file/cache/pfp_character.png?time=" + timestamp;
imgElement.classList.add("bigProfilePicture");

var imgElementParent = document.getElementById("chat").parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
imgElementParent.appendChild(imgElement);
}

function deleteBigPicture() {
var bigProfilePictures = document.querySelectorAll('.bigProfilePicture');
bigProfilePictures.forEach(function (element) {
element.parentNode.removeChild(element);
});
}

function toggleBigPicture() {
if(bigPictureVisible) {
deleteBigPicture();
bigPictureVisible = false;
} else {
addBigPicture();
bigPictureVisible = true;
}
}

showBigPicture();
7 changes: 7 additions & 0 deletions js/update_big_picture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function updateBigPicture() {
var existingElement = document.querySelector('.bigProfilePicture');
if (existingElement) {
var timestamp = new Date().getTime();
existingElement.src = "/file/cache/pfp_character.png?time=" + timestamp;
}
}
15 changes: 10 additions & 5 deletions modules/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,13 @@ def generate_pfp_cache(character):

for path in [Path(f"characters/{character}.{extension}") for extension in ['png', 'jpg', 'jpeg']]:
if path.exists():
img = make_thumbnail(Image.open(path))
img.save(Path('cache/pfp_character.png'), format='PNG')
return img
original_img = Image.open(path)
original_img.save(Path('cache/pfp_character.png'), format='PNG')

thumb = make_thumbnail(original_img)
thumb.save(Path('cache/pfp_character_thumb.png'), format='PNG')

return thumb

return None

Expand Down Expand Up @@ -575,8 +579,9 @@ def load_character(character, name1, name2, instruct=False):
file_contents = open(filepath, 'r', encoding='utf-8').read()
data = json.loads(file_contents) if extension == "json" else yaml.safe_load(file_contents)

if Path("cache/pfp_character.png").exists() and not instruct:
Path("cache/pfp_character.png").unlink()
for path in [Path("cache/pfp_character.png"), Path("cache/pfp_character_thumb.png")]:
if path.exists() and not instruct:
path.unlink()

picture = generate_pfp_cache(character)

Expand Down
2 changes: 1 addition & 1 deletion modules/html_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def generate_cai_chat_html(history, name1, name2, style, reset_cache=False):
output = f'<style>{chat_styles[style]}</style><div class="chat" id="chat"><div class="messages">'

# We use ?name2 and ?time.time() to force the browser to reset caches
img_bot = f'<img src="file/cache/pfp_character.png?{name2}">' if Path("cache/pfp_character.png").exists() else ''
img_bot = f'<img src="file/cache/pfp_character_thumb.png?{name2}" class="pfp_character">' if Path("cache/pfp_character_thumb.png").exists() else ''
img_me = f'<img src="file/cache/pfp_me.png?{time.time() if reset_cache else ""}">' if Path("cache/pfp_me.png").exists() else ''

for i, _row in enumerate(history):
Expand Down
2 changes: 2 additions & 0 deletions modules/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
switch_tabs_js = f.read()
with open(Path(__file__).resolve().parent / '../js/show_controls.js', 'r') as f:
show_controls_js = f.read()
with open(Path(__file__).resolve().parent / '../js/update_big_picture.js', 'r') as f:
update_big_picture_js = f.read()

refresh_symbol = '🔄'
delete_symbol = '🗑️'
Expand Down
3 changes: 2 additions & 1 deletion modules/ui_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ def create_event_handlers():
ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then(
chat.load_latest_history, gradio('interface_state'), gradio('history')).then(
chat.redraw_html, gradio(reload_arr), gradio('display')).then(
lambda x: gr.update(choices=(histories := chat.find_all_histories(x)), value=histories[0]), gradio('interface_state'), gradio('unique_id'))
lambda x: gr.update(choices=(histories := chat.find_all_histories(x)), value=histories[0]), gradio('interface_state'), gradio('unique_id')).then(
lambda: None, None, None, _js=f'() => {{{ui.update_big_picture_js}; updateBigPicture()}}')

shared.gradio['mode'].change(
lambda x: gr.update(visible=x != 'instruct'), gradio('mode'), gradio('chat_style'), show_progress=False).then(
Expand Down