-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #279 from mrhan1993:url_support
url support for lora in replicate
- Loading branch information
Showing
5 changed files
with
87 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
Manager loras from url | ||
@author: TechnikMax | ||
@github: https://github.com/TechnikMax | ||
""" | ||
import hashlib | ||
import os | ||
import requests | ||
|
||
|
||
def _hash_url(url): | ||
"""Generates a hash value for a given URL.""" | ||
return hashlib.md5(url.encode('utf-8')).hexdigest() | ||
|
||
|
||
class LoraManager: | ||
""" | ||
Manager loras from url | ||
""" | ||
def __init__(self): | ||
self.cache_dir = os.path.join( | ||
os.path.dirname(os.path.realpath(__file__)), | ||
'../../', | ||
'repositories/Fooocus/models/loras') | ||
|
||
def _download_lora(self, url): | ||
""" | ||
Downloads a LoRa from a URL and saves it in the cache. | ||
""" | ||
url_hash = _hash_url(url) | ||
filepath = os.path.join(self.cache_dir, f"{url_hash}.safetensors") | ||
file_name = f"{url_hash}.safetensors" | ||
|
||
if not os.path.exists(filepath): | ||
print(f"start download for: {url}") | ||
|
||
try: | ||
response = requests.get(url, timeout=10, stream=True) | ||
response.raise_for_status() | ||
with open(filepath, 'wb') as f: | ||
for chunk in response.iter_content(chunk_size=8192): | ||
f.write(chunk) | ||
print(f"Download successfully, saved as {file_name}") | ||
|
||
except Exception as e: | ||
raise Exception(f"error downloading {url}: {e}") from e | ||
|
||
else: | ||
print(f"LoRa already downloaded {url}") | ||
return file_name | ||
|
||
def check(self, urls): | ||
"""Manages the specified LoRAs: downloads missing ones and returns their file names.""" | ||
paths = [] | ||
for url in urls: | ||
path = self._download_lora(url) | ||
paths.append(path) | ||
return paths |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters