Skip to content

Commit

Permalink
refactor: remove requests and python-dotenv to reduce size
Browse files Browse the repository at this point in the history
  • Loading branch information
leafspark committed Sep 5, 2024
1 parent 45d0212 commit 8fb500a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 33 deletions.
17 changes: 11 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
psutil~=6.0.0
requests~=2.32.3
numpy<3.0.0
torch~=2.4.0
sentencepiece~=0.2.0
# System Monitoring
PyYAML~=6.0.2
psutil~=6.0.0
pynvml~=11.5.3
# GUI Library
PySide6~=6.7.2
python-dotenv~=1.0.1
# llama.cpp tools (external)
safetensors~=0.4.4
numpy<3.0.0
torch~=2.4.0
sentencepiece~=0.2.0
# PyPI build
setuptools~=74.0.0
# HuggingFace Download/Upload
huggingface-hub~=0.24.6
# AutoFP8 (external)
transformers~=4.44.2
# Local Server
fastapi~=0.112.2
uvicorn~=0.30.6
64 changes: 52 additions & 12 deletions src/AutoGGUF.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
import json
import re
import shutil
import urllib.request
import urllib.error
from datetime import datetime
from functools import partial
from typing import Any, Dict, List, Tuple

import requests
from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
from dotenv import load_dotenv

import lora_conversion
import presets
Expand Down Expand Up @@ -44,7 +44,7 @@ def __init__(self, args: List[str]) -> None:
self.setGeometry(100, 100, width, height)
self.setWindowFlag(Qt.FramelessWindowHint)

load_dotenv() # Loads the .env file
self.load_dotenv() # Loads the .env file

# Configuration
self.model_dir_name = os.environ.get("AUTOGGUF_MODEL_DIR_NAME", "models")
Expand Down Expand Up @@ -805,6 +805,41 @@ def __init__(self, args: List[str]) -> None:

self.logger.info(AUTOGGUF_INITIALIZATION_COMPLETE)

def load_dotenv(self):
if not os.path.isfile(".env"):
self.logger.warning(".env file not found.")
return

try:
with open(".env") as f:
for line in f:
# Strip leading/trailing whitespace
line = line.strip()

# Ignore comments and empty lines
if not line or line.startswith("#"):
continue

# Match key-value pairs (unquoted and quoted values)
match = re.match(r"^([^=]+)=(.*)$", line)
if not match:
self.logger.warning(f"Could not parse line: {line}")
continue

key, value = match.groups()

# Remove any surrounding quotes from the value
if value.startswith(("'", '"')) and value.endswith(("'", '"')):
value = value[1:-1]

# Decode escape sequences
value = bytes(value, "utf-8").decode("unicode_escape")

# Set the environment variable
os.environ[key.strip()] = value.strip()
except Exception as e:
self.logger.error(f"Error loading .env: {e}")

def load_plugins(self) -> Dict[str, Dict[str, Any]]:
plugins = {}
plugin_dir = "plugins"
Expand Down Expand Up @@ -881,17 +916,22 @@ def apply_plugins(self) -> None:

def check_for_updates(self) -> None:
try:
response = requests.get(
"https://api.github.com/repos/leafspark/AutoGGUF/releases/latest"
)
response.raise_for_status() # Raise an exception for bad status codes
url = "https://api.github.com/repos/leafspark/AutoGGUF/releases/latest"
req = urllib.request.Request(url)

with urllib.request.urlopen(req) as response:
if response.status != 200:
raise urllib.error.HTTPError(
url, response.status, "HTTP Error", response.headers, None
)

latest_release = json.loads(response.read().decode("utf-8"))
latest_version = latest_release["tag_name"].replace("v", "")

latest_release = response.json()
latest_version = latest_release["tag_name"].replace("v", "")
if latest_version > AUTOGGUF_VERSION.replace("v", ""):
self.prompt_for_update(latest_release)

if latest_version > AUTOGGUF_VERSION.replace("v", ""):
self.prompt_for_update(latest_release)
except requests.exceptions.RequestException as e:
except urllib.error.URLError as e:
self.logger.warning(f"{ERROR_CHECKING_FOR_UPDATES} {e}")

def prompt_for_update(self, release) -> None:
Expand Down
39 changes: 24 additions & 15 deletions src/DownloadThread.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import urllib.request
import urllib.error
import zipfile

import requests
from PySide6.QtCore import QThread, Signal


Expand All @@ -17,19 +17,28 @@ def __init__(self, url, save_path) -> None:

def run(self) -> None:
try:
response = requests.get(self.url, stream=True)
response.raise_for_status()
total_size = int(response.headers.get("content-length", 0))
block_size = 8192
downloaded = 0

with open(self.save_path, "wb") as file:
for data in response.iter_content(block_size):
size = file.write(data)
downloaded += size
if total_size:
progress = int((downloaded / total_size) * 100)
self.progress_signal.emit(progress)
req = urllib.request.Request(self.url)

with urllib.request.urlopen(req) as response:
if response.status != 200:
raise urllib.error.HTTPError(
self.url, response.status, "HTTP Error", response.headers, None
)

total_size = int(response.headers.get("Content-Length", 0))
block_size = 8192
downloaded = 0

with open(self.save_path, "wb") as file:
while True:
data = response.read(block_size)
if not data:
break
size = file.write(data)
downloaded += size
if total_size:
progress = int((downloaded / total_size) * 100)
self.progress_signal.emit(progress)

# Extract the downloaded zip file
extract_dir = os.path.splitext(self.save_path)[0]
Expand Down

0 comments on commit 8fb500a

Please sign in to comment.