Spaces:
Sleeping
Sleeping
Hjgugugjhuhjggg
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -4,14 +4,13 @@ import time
|
|
4 |
import threading
|
5 |
import asyncio
|
6 |
from io import BytesIO
|
7 |
-
from typing import Union
|
8 |
import requests
|
9 |
import boto3
|
10 |
import torch
|
11 |
import safetensors
|
12 |
import soundfile as sf
|
13 |
import numpy as np
|
14 |
-
from fastapi import FastAPI, HTTPException,
|
15 |
from fastapi.responses import StreamingResponse
|
16 |
from pydantic import BaseModel, ValidationError, field_validator
|
17 |
from transformers import (
|
@@ -50,8 +49,6 @@ class GenerateRequest(BaseModel):
|
|
50 |
chunk_delay: float = 0.0
|
51 |
stop_sequences: list[str] = []
|
52 |
|
53 |
-
model_config = {"protected_namespaces": ()}
|
54 |
-
|
55 |
@field_validator("model_name")
|
56 |
def model_name_cannot_be_empty(cls, v):
|
57 |
if not v:
|
@@ -71,13 +68,13 @@ class S3ModelLoader:
|
|
71 |
self.s3_client = s3_client
|
72 |
|
73 |
def _get_s3_uri(self, model_name):
|
74 |
-
return f"s3://{self.bucket_name}/{model_name.replace('/', '-')}"
|
75 |
|
76 |
def _download_from_s3(self, model_name):
|
77 |
s3_uri = self._get_s3_uri(model_name)
|
78 |
try:
|
79 |
logging.info(f"Attempting to load model {model_name} from S3...")
|
80 |
-
model_files = self.s3_client.list_objects_v2(Bucket=self.bucket_name, Prefix=model_name)
|
81 |
if "Contents" not in model_files:
|
82 |
raise FileNotFoundError(f"Model files not found in S3 for {model_name}")
|
83 |
|
@@ -99,7 +96,6 @@ class S3ModelLoader:
|
|
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)
|
@@ -115,7 +111,7 @@ class S3ModelLoader:
|
|
115 |
try:
|
116 |
logging.info(f"Downloading model {model_name} from Hugging Face...")
|
117 |
model_dir = hf_hub_download(model_name, token=HUGGINGFACE_HUB_TOKEN)
|
118 |
-
self.s3_client.upload_file(model_dir, self.bucket_name, model_name)
|
119 |
logging.info(f"Model {model_name} saved to S3 successfully.")
|
120 |
except Exception as e:
|
121 |
logging.error(f"Error downloading model {model_name} from Hugging Face: {e}")
|
|
|
4 |
import threading
|
5 |
import asyncio
|
6 |
from io import BytesIO
|
|
|
7 |
import requests
|
8 |
import boto3
|
9 |
import torch
|
10 |
import safetensors
|
11 |
import soundfile as sf
|
12 |
import numpy as np
|
13 |
+
from fastapi import FastAPI, HTTPException, Request, UploadFile, File
|
14 |
from fastapi.responses import StreamingResponse
|
15 |
from pydantic import BaseModel, ValidationError, field_validator
|
16 |
from transformers import (
|
|
|
49 |
chunk_delay: float = 0.0
|
50 |
stop_sequences: list[str] = []
|
51 |
|
|
|
|
|
52 |
@field_validator("model_name")
|
53 |
def model_name_cannot_be_empty(cls, v):
|
54 |
if not v:
|
|
|
68 |
self.s3_client = s3_client
|
69 |
|
70 |
def _get_s3_uri(self, model_name):
|
71 |
+
return f"s3://{self.bucket_name}/lilmeaty_garca/{model_name.replace('/', '-')}"
|
72 |
|
73 |
def _download_from_s3(self, model_name):
|
74 |
s3_uri = self._get_s3_uri(model_name)
|
75 |
try:
|
76 |
logging.info(f"Attempting to load model {model_name} from S3...")
|
77 |
+
model_files = self.s3_client.list_objects_v2(Bucket=self.bucket_name, Prefix=f"lilmeaty_garca/{model_name}")
|
78 |
if "Contents" not in model_files:
|
79 |
raise FileNotFoundError(f"Model files not found in S3 for {model_name}")
|
80 |
|
|
|
96 |
|
97 |
async def load_model_and_tokenizer(self, model_name):
|
98 |
try:
|
|
|
99 |
model_dir = await asyncio.to_thread(self._download_from_s3, model_name)
|
100 |
config = AutoConfig.from_pretrained(model_dir)
|
101 |
tokenizer = AutoTokenizer.from_pretrained(model_dir, config=config)
|
|
|
111 |
try:
|
112 |
logging.info(f"Downloading model {model_name} from Hugging Face...")
|
113 |
model_dir = hf_hub_download(model_name, token=HUGGINGFACE_HUB_TOKEN)
|
114 |
+
self.s3_client.upload_file(model_dir, self.bucket_name, f"lilmeaty_garca/{model_name}")
|
115 |
logging.info(f"Model {model_name} saved to S3 successfully.")
|
116 |
except Exception as e:
|
117 |
logging.error(f"Error downloading model {model_name} from Hugging Face: {e}")
|