Turn this into a py gui that allows me to add the dfm_models path and then automatically follow this format and use dfm file name and stuff as the stuff used then after give me a list of all then in the gui so I can update the res for them and give I my one biz per because they are both the same number here is the thing 'iperov/jackie_chan_224': {
'hashes': {
'deep_swapper': {
'url': 'https://huggingface.co/bluefoxcreation/DFM/resolve/main/Jackie_Chan.hash',
'path': 'C:\dfm_models\Jackie_Chan.hash'
}
},
'sources': {
'deep_swapper': {
'url': 'https://github.com/iperov/DeepFaceLive/releases/download/JACKIE_CHAN/Jackie_Chan.dfm',
'path': 'C:\dfm_models\Jackie_Chan.dfm'
}
},
'template': 'arcface_128_v2',
'size': (224, 224)
},
'iperov/bruce_lee_224': {
'hashes': {
'deep_swapper': {
'url': 'https://invalid-url.com/Bruce_Lee.hash',
'path': 'C:\dfm_models\Bruce_Lee.hash'
}
},
'sources': {
'deep_swapper': {
'url': 'https://invalid-url.com/Bruce_Lee.dfm',
'path': 'C:\dfm_models\Bruce_Lee.dfm'
}
},
'template': 'arcface_128_v2',
'size': (224, 224)
},
'iperov/arnold_224': {
'hashes': {
'deep_swapper': {
'url': 'https://invalid-url.com/Arnold.hash',
'path': 'C:\dfm_models\Arnold.hash'
}
},
'sources': {
'deep_swapper': {
'url': 'https://invalid-url.com/Arnold.dfm',
'path': 'C:\dfm_models\Arnold.dfm'
}
},
'template': 'arcface_128_v2',
'size': (224, 224)
}
}
Assistant
Certainly! Below is a Python script that uses the tkinter library to create a GUI for managing your dfm_models. This GUI allows you to:
Set the dfm_models Path: Input the directory where your DFM models are stored.
Load Models: Automatically parse the models from the specified directory following the provided format.
Display Models: Show a list of all models with their current settings.
Update Resolution (size): Modify the resolution for each model.
Save Changes: Update the internal data structure with the new settings.
Prerequisites
Ensure you have Python installed on your system. This script uses the standard tkinter library, which comes bundled with Python. No additional packages are required.
Complete Python Script
python
import os
import json
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
class DFMModelManager:
def __init__(self, root):
self.root = root
self.root.title("DFM Models Manager")
self.models = {}
self.dfmpath = ""
self.create_widgets()
def create_widgets(self):
# Frame for path selection
path_frame = ttk.Frame(self.root, padding="10")
path_frame.grid(row=0, column=0, sticky="ew")
self.path_var = tk.StringVar()
path_label = ttk.Label(path_frame, text="DFM Models Path:")
path_label.pack(side=tk.LEFT, padx=(0,5))
path_entry = ttk.Entry(path_frame, textvariable=self.path_var, width=50)
path_entry.pack(side=tk.LEFT, fill=tk.X, expand=True)
browse_button = ttk.Button(path_frame, text="Browse", command=self.browse_path)
browse_button.pack(side=tk.LEFT, padx=(5,0))
load_button = ttk.Button(path_frame, text="Load Models", command=self.load_models)
load_button.pack(side=tk.LEFT, padx=(5,0))
# Treeview for displaying models
tree_frame = ttk.Frame(self.root, padding="10")
tree_frame.grid(row=1, column=0, sticky="nsew")
columns = ("Model Name", "Hash URL", "Source URL", "Template", "Size")
self.tree = ttk.Treeview(tree_frame, columns=columns, show='headings')
for col in columns:
self.tree.heading(col, text=col)
self.tree.column(col, width=150, anchor='center')
self.tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar = ttk.Scrollbar(tree_frame, orient=tk.VERTICAL, command=self.tree.yview)
self.tree.configure(yscroll=scrollbar.set)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Frame for updating resolution
update_frame = ttk.Frame(self.root, padding="10")
update_frame.grid(row=2, column=0, sticky="ew")
size_label = ttk.Label(update_frame, text="Update Size (width, height):")
size_label.pack(side=tk.LEFT, padx=(0,5))
self.size_entry = ttk.Entry(update_frame, width=20)
self.size_entry.pack(side=tk.LEFT, padx=(0,5))
update_button = ttk.Button(update_frame, text="Update Selected", command=self.update_selected)
update_button.pack(side=tk.LEFT, padx=(5,0))
# Frame for save button
save_frame = ttk.Frame(self.root, padding="10")
save_frame.grid(row=3, column=0, sticky="ew")
save_button = ttk.Button(save_frame, text="Save Changes", command=self.save_changes)
save_button.pack(side=tk.RIGHT)
# Make the GUI expandable
self.root.rowconfigure(1, weight=1)
self.root.columnconfigure(0, weight=1)
def browse_path(self):
directory = filedialog.askdirectory()
if directory:
self.path_var.set(directory)
def load_models(self):
self.dfmpath = self.path_var.get()
if not os.path.isdir(self.dfmpath):
messagebox.showerror("Error", "Invalid directory path.")
return
# Clear existing models
self.models.clear()
for item in self.tree.get_children():
self.tree.delete(item)
# Scan the directory for .dfm files
for file in os.listdir(self.dfmpath):
if file.endswith(".dfm"):
model_name = os.path.splitext(file)[0]
dfm_path = os.path.join(self.dfmpath, file)
hash_file = model_name + ".hash"
hash_path = os.path.join(self.dfmpath, hash_file)
if os.path.exists(hash_path):
model_key = f"iperov/{model_name}_224"
model_data = {
"hashes": {
"deep_swapper": {
"url": f"https://invalid-url.com/{model_name}.hash",
"path": hash_path.replace("/", "\\")
}
},
"sources": {
"deep_swapper": {
"url": f"https://invalid-url.com/{model_name}.dfm",
"path": dfm_path.replace("/", "\\")
}
},
"template": "arcface_128_v2",
"size": (224, 224)
}
self.models[model_key] = model_data
# Insert into treeview
self.tree.insert("", "end", iid=model_key, values=(
model_key,
model_data["hashes"]["deep_swapper"]["url"],
model_data["sources"]["deep_swapper"]["url"],
model_data["template"],
model_data["size"]
))
if not self.models:
messagebox.showinfo("Info", "No models found in the specified directory.")
def update_selected(self):
selected = self.tree.selection()
if not selected:
messagebox.showwarning("Warning", "No model selected.")
return
new_size = self.size_entry.get().strip()
if not new_size:
messagebox.showwarning("Warning", "Size cannot be empty.")
return
try:
size_tuple = tuple(map(int, new_size.strip("()").split(',')))
if len(size_tuple) != 2:
raise ValueError
except ValueError:
messagebox.showerror("Error", "Invalid size format. Use format: width, height")
return
for item in selected:
self.models[item]["size"] = size_tuple
# Update treeview
current_values = self.tree.item(item, "values")
updated_values = list(current_values)
updated_values[4] = size_tuple
self.tree.item(item, values=updated_values)
def save_changes(self):
# Here you can implement saving to a file or applying the changes
# For demonstration, we'll save to a JSON file
save_path = os.path.join(self.dfmpath, "updated_models.json")
try:
with open(save_path, 'w') as f:
json.dump(self.models, f, indent=4)
messagebox.showinfo("Success", f"Changes saved to {save_path}")
except Exception as e:
messagebox.showerror("Error", f"Failed to save changes.\n{e}")
def main():
root = tk.Tk()
root.geometry("900x600")
app = DFMModelManager(root)
root.mainloop()
if __name__ == "__main__":
main()
Explanation of the Script
Imports:
os, json: For file and data management.
tkinter and related modules: For building the GUI.
DFMModelManager Class:
Initialization: Sets up the main window and initializes the model data structure.
create_widgets: Constructs all GUI components, including:
Path Selection Frame: Input field and buttons to browse and load models.
Treeview: Displays a list of models with columns for model name, URLs, template, and size.
Update Frame: Allows updating the size for selected models.
Save Button: Saves the updated models to a JSON file.
browse_path: Opens a directory dialog to select the dfm_models path.
load_models: Scans the selected directory for .dfm and .hash files, constructs the models dictionary, and populates the Treeview.
update_selected: Updates the size attribute for selected models based on user input.
save_changes: Saves the updated models dictionary to a updated_models.json file in the dfm_models directory.
main Function:
Initializes the Tkinter root window, sets its size, and starts the main event loop.
How to Use the GUI
Run the Script:
Save the script to a file, e.g., dfm_manager.py.
Run it using Python:
bash
python dfm_manager.py
Set the DFM Models Path:
Click the Browse button to select the directory containing your .dfm and .hash files.
Alternatively, manually enter the path in the input field.
Load Models:
Click the Load Models button to scan the directory and populate the list of models.
View Models:
The Treeview will display all detected models with their current configuration.
Update Resolution (size):
Select one or more models from the list.
Enter the new size in the format width, height (e.g., 256, 256) in the Update Size field.
Click Update Selected to apply the changes.
Save Changes:
After making updates, click the Save Changes button to store the updated model configurations in a updated_models.json file within your dfm_models directory.
Notes
Error Handling: The script includes basic error handling for invalid paths, input formats, and file operations. Make sure to provide valid inputs to avoid errors.
Extensibility: You can extend the script to handle more attributes or functionalities as needed, such as updating URLs or templates.
Saving Format: Currently, updates are saved in a JSON file. You can modify the save_changes method to save in a different format if required.
Feel free to customize and enhance the GUI according to your specific requirements!