Spaces:
Sleeping
Sleeping
Hjgugugjhuhjggg
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import os
|
|
2 |
import logging
|
3 |
import time
|
4 |
import threading
|
|
|
5 |
from io import BytesIO
|
6 |
from typing import Union
|
7 |
import requests
|
@@ -98,7 +99,8 @@ class S3ModelLoader:
|
|
98 |
|
99 |
async def load_model_and_tokenizer(self, model_name):
|
100 |
try:
|
101 |
-
|
|
|
102 |
config = AutoConfig.from_pretrained(model_dir)
|
103 |
tokenizer = AutoTokenizer.from_pretrained(model_dir, config=config)
|
104 |
model = AutoModelForCausalLM.from_pretrained(model_dir, config=config)
|
@@ -141,8 +143,6 @@ class S3ModelLoader:
|
|
141 |
async def startup_event():
|
142 |
model_loader.run_in_background()
|
143 |
|
144 |
-
app = FastAPI()
|
145 |
-
|
146 |
s3_client = boto3.client('s3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY, region_name=AWS_REGION)
|
147 |
model_loader = S3ModelLoader(S3_BUCKET_NAME, s3_client)
|
148 |
|
|
|
2 |
import logging
|
3 |
import time
|
4 |
import threading
|
5 |
+
import asyncio
|
6 |
from io import BytesIO
|
7 |
from typing import Union
|
8 |
import requests
|
|
|
99 |
|
100 |
async def load_model_and_tokenizer(self, model_name):
|
101 |
try:
|
102 |
+
# Offload the blocking I/O to a separate thread
|
103 |
+
model_dir = await asyncio.to_thread(self._download_from_s3, model_name)
|
104 |
config = AutoConfig.from_pretrained(model_dir)
|
105 |
tokenizer = AutoTokenizer.from_pretrained(model_dir, config=config)
|
106 |
model = AutoModelForCausalLM.from_pretrained(model_dir, config=config)
|
|
|
143 |
async def startup_event():
|
144 |
model_loader.run_in_background()
|
145 |
|
|
|
|
|
146 |
s3_client = boto3.client('s3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY, region_name=AWS_REGION)
|
147 |
model_loader = S3ModelLoader(S3_BUCKET_NAME, s3_client)
|
148 |
|