Hjgugugjhuhjggg commited on
Commit
944ca71
·
verified ·
1 Parent(s): 05818b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -3
app.py CHANGED
@@ -4,7 +4,6 @@ from fastapi import FastAPI, HTTPException
4
  from fastapi.responses import StreamingResponse
5
  from pydantic import BaseModel
6
  from transformers import (
7
- AutoConfig,
8
  AutoModelForCausalLM,
9
  AutoTokenizer,
10
  GenerationConfig,
@@ -15,6 +14,7 @@ import asyncio
15
  from io import BytesIO
16
  from botocore.exceptions import NoCredentialsError
17
  import boto3
 
18
 
19
  # Diccionario global para almacenar los tokens y configuraciones de los modelos
20
  token_dict = {}
@@ -64,12 +64,16 @@ class S3ModelLoader:
64
 
65
  s3_uri = self._get_s3_uri(model_name)
66
  try:
67
- model = AutoModelForCausalLM.from_pretrained(s3_uri, local_files_only=True)
68
- tokenizer = AutoTokenizer.from_pretrained(s3_uri, local_files_only=True)
 
 
 
69
 
70
  if tokenizer.eos_token_id is None:
71
  tokenizer.eos_token_id = tokenizer.pad_token_id
72
 
 
73
  token_dict[model_name] = {
74
  "model": model,
75
  "tokenizer": tokenizer,
@@ -77,6 +81,10 @@ class S3ModelLoader:
77
  "eos_token_id": tokenizer.eos_token_id
78
  }
79
 
 
 
 
 
80
  return token_dict[model_name]
81
  except NoCredentialsError:
82
  raise HTTPException(status_code=500, detail="AWS credentials not found.")
 
4
  from fastapi.responses import StreamingResponse
5
  from pydantic import BaseModel
6
  from transformers import (
 
7
  AutoModelForCausalLM,
8
  AutoTokenizer,
9
  GenerationConfig,
 
14
  from io import BytesIO
15
  from botocore.exceptions import NoCredentialsError
16
  import boto3
17
+ from huggingface_hub import snapshot_download
18
 
19
  # Diccionario global para almacenar los tokens y configuraciones de los modelos
20
  token_dict = {}
 
64
 
65
  s3_uri = self._get_s3_uri(model_name)
66
  try:
67
+ # Descargamos el modelo y el tokenizer desde Hugging Face directamente a S3
68
+ model_path = snapshot_download(model_name, token=HUGGINGFACE_HUB_TOKEN)
69
+
70
+ model = AutoModelForCausalLM.from_pretrained(model_path)
71
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
72
 
73
  if tokenizer.eos_token_id is None:
74
  tokenizer.eos_token_id = tokenizer.pad_token_id
75
 
76
+ # Guardamos en el diccionario global
77
  token_dict[model_name] = {
78
  "model": model,
79
  "tokenizer": tokenizer,
 
81
  "eos_token_id": tokenizer.eos_token_id
82
  }
83
 
84
+ # Subimos los modelos al S3 si es necesario
85
+ self.s3_client.upload_file(model_path, self.bucket_name, f'{model_name}/model')
86
+ self.s3_client.upload_file(f'{model_path}/tokenizer', self.bucket_name, f'{model_name}/tokenizer')
87
+
88
  return token_dict[model_name]
89
  except NoCredentialsError:
90
  raise HTTPException(status_code=500, detail="AWS credentials not found.")