我已经为我的存储库创建了一个工作流程,一切甚至部署都进展顺利,我已经在与 docker 相同的环境中创建了模型,并且工作流程正在运行,但我仍然得到了这个:
Environment:
Request Method: GET
Request URL: http://mysite-olvdmbamqa-el.a.run.app/
Django Version: 4.0.2
Python Version: 3.11.5
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'welcome_deck',
'movie_recommender',
'irisapp']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/usr/local/lib/python3.11/site-packages/django/core/handlers/base.py", line 167, in _get_response
callback, callback_args, callback_kwargs = self.resolve_request(request)
File "/usr/local/lib/python3.11/site-packages/django/core/handlers/base.py", line 290, in resolve_request
resolver_match = resolver.resolve(request.path_info)
File "/usr/local/lib/python3.11/site-packages/django/urls/resolvers.py", line 592, in resolve
for pattern in self.url_patterns:
File "/usr/local/lib/python3.11/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.11/site-packages/django/urls/resolvers.py", line 634, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python3.11/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.11/site-packages/django/urls/resolvers.py", line 627, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
<source code not available>
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
<source code not available>
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
<source code not available>
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
<source code not available>
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
<source code not available>
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
<source code not available>
File "/app/mysite/urls.py", line 25, in <module>
path('movie/',include('movie_recommender.urls')),
File "/usr/local/lib/python3.11/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
<source code not available>
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
<source code not available>
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
<source code not available>
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
<source code not available>
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
<source code not available>
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
<source code not available>
File "/app/movie_recommender/urls.py", line 2, in <module>
from . import views
File "/app/movie_recommender/views.py", line 26, in <module>
similarity = load(open(model_path,'rb'))
File "/usr/local/lib/python3.11/site-packages/joblib/numpy_pickle.py", line 648, in load
obj = _unpickle(fobj)
File "/usr/local/lib/python3.11/site-packages/joblib/numpy_pickle.py", line 577, in _unpickle
obj = unpickler.load()
File "/usr/local/lib/python3.11/pickle.py", line 1213, in load
dispatch[key[0]](self)
Exception Type: KeyError at /
Exception Value: 118
请帮我修复代码。我还提供了views.py:
from django.shortcuts import render
from joblib import load
import requests
import os
from django.conf import settings
from django.views.decorators.cache import never_cache
# Create your views here.
def home(request):
return render(request, 'index.html', {'movies_list': movies_list['title'].values})
def fetch_poster(movie_id):
response=requests.get('https://api.themoviedb.org/3/movie/{}?api_key=fbb18fe9735d1a3e0ad1531398ed1b13'.format(movie_id))
data=response.json()
return 'https://image.tmdb.org/t/p/w500/'+ data['poster_path']
movies_list_path = os.path.join(settings.BASE_DIR, 'savedmodels/movies.joblib')
model_path = os.path.join(settings.BASE_DIR, 'savedmodels/similarity.joblib')
movies_list =load(open(movies_list_path, 'rb'))
similarity = load(open(model_path,'rb'))
def recommend(movies_list, movie):
movie_index = movies_list[movies_list['title'] == movie].index[0]
distances = similarity[movie_index]
movie_list = sorted(list(enumerate(distances)), reverse=True, key=lambda x: x[1])[1:7]
recommended_movies = []
recommended_movies_posters = []
for i in movie_list:
movie_id = movies_list.iloc[i[0]].id
recommended_movies.append(movies_list.iloc[i[0]].title)
# fetch poster from API
recommended_movies_posters.append(fetch_poster(movie_id))
return recommended_movies, recommended_movies_posters
@never_cache
def recommend_movies(request):
if request.method == 'POST':
selected_movie_name = request.POST['movie_name']
names, posters = recommend(movies_list, selected_movie_name)
return render(request, 'recommend.html', {'movie_data': zip(names, posters), 'movies_list': movies_list['title'].values})
return render(request, 'index.html', {'movies_list': movies_list['title'].values})
我尝试在环境中再次创建模型并使用 git-lfs 来处理大文件,但仍然收到此错误
检查模型路径:确保views.py 文件中的model_path 变量正确指向模型文件的位置。考虑到您的项目结构和部署环境,它应该是绝对路径或正确解析相对路径。
文件存在:验证部署环境中指定路径下是否存在模型文件。您可以添加一些日志记录或调试语句来确认这一点。
文件权限:确保应用程序具有读取模型文件所需的权限。检查Docker容器或部署环境中的文件权限。
或者您只需添加更多调试