added profiling script, optimised imports

This commit is contained in:
Alexander Karpov 2023-11-20 01:06:17 +03:00
parent 9ac5a1f235
commit 356476217d
5 changed files with 50 additions and 5 deletions

2
.gitignore vendored
View File

@ -1,4 +1,6 @@
.idea
django_setup.txt
django_setup.prof
### Python template
# Byte-compiled / optimized / DLL files

View File

@ -8,20 +8,25 @@
russian_stopwords = set(stopwords.words("russian"))
# Set up lemmatizers
english_lemmatizer = WordNetLemmatizer()
russian_lemmatizer = MorphAnalyzer()
english_lemmatizer = None
russian_lemmatizer = None
def lemmatize_and_remove_stopwords(text, language="english"):
# Tokenize the text
global english_lemmatizer, russian_lemmatizer
tokens = word_tokenize(text)
# Lemmatize each token based on the specified language
lemmatized_tokens = []
for token in tokens:
if language == "russian":
if not russian_lemmatizer:
russian_lemmatizer = MorphAnalyzer()
lemmatized_token = russian_lemmatizer.parse(token)[0].normal_form
else: # Default to English
if not english_lemmatizer:
english_lemmatizer = WordNetLemmatizer()
lemmatized_token = english_lemmatizer.lemmatize(token)
lemmatized_tokens.append(lemmatized_token)

View File

@ -16,7 +16,7 @@
"Consola.ttf",
]
manager = PreviewManager(cache_path, create_folder=True)
manager = None
def textfile_to_image(textfile_path) -> Image:
@ -79,7 +79,10 @@ def _font_points_to_pixels(pt):
def create_preview(file_path: str) -> str:
global manager
# TODO: add text image generation/code image
if not manager:
manager = PreviewManager(cache_path, create_folder=True)
if manager.has_jpeg_preview(file_path):
return manager.get_jpeg_preview(file_path, height=500)
return ""
@ -91,6 +94,10 @@ def get_file_mimetype(file_path: str) -> str:
def get_description(file_path: str) -> str:
global manager
if not manager:
manager = PreviewManager(cache_path, create_folder=True)
if manager.has_text_preview(file_path):
return manager.get_text_preview(file_path)
return ""

View File

@ -9,7 +9,6 @@
from PIL import Image
from pydub import AudioSegment
from pytube import Search, YouTube
from yt_dlp import YoutubeDL
from akarpov.music.models import Song
from akarpov.music.services.db import load_track
@ -67,7 +66,7 @@ def parse_description(description: str) -> list:
def download_from_youtube_link(link: str, user_id: int) -> Song:
song = None
with YoutubeDL(ydl_opts) as ydl:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(link, download=False)
title = info_dict.get("title", None)
description = info_dict.get("description", None)

32
test_start.py Normal file
View File

@ -0,0 +1,32 @@
import cProfile
import os
import pstats
import django
# Function to run the Django setup process, which you want to profile
def django_setup():
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
django.setup()
# Create a Profile object and run the django_setup function
profiler = cProfile.Profile()
profiler.enable()
django_setup()
profiler.disable()
# Write the stats to a .prof file
profiler.dump_stats("django_setup.prof")
# Create a Stats object and print the sorted stats to a text file
with open("django_setup.txt", "w") as stream:
stats = pstats.Stats(profiler, stream=stream)
stats.sort_stats("cumtime") # Sort the statistics by cumulative time
stats.print_stats() # Print the statistics to the stream
# Optionally, print the stats to the console as well
stats = pstats.Stats(profiler)
stats.sort_stats("cumtime")
stats.print_stats()