Spaces:
Sleeping
Sleeping
Hjgugugjhuhjggg
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import os
|
|
2 |
import logging
|
3 |
import threading
|
4 |
import boto3
|
5 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig, StoppingCriteriaList
|
6 |
from fastapi import FastAPI, HTTPException, Request
|
7 |
from pydantic import BaseModel, field_validator
|
8 |
from huggingface_hub import hf_hub_download
|
@@ -77,7 +77,7 @@ class S3ModelLoader:
|
|
77 |
def download_model_from_huggingface(self, model_name):
|
78 |
try:
|
79 |
logging.info(f"Downloading model {model_name} from Hugging Face...")
|
80 |
-
model_dir = hf_hub_download(model_name, token=HUGGINGFACE_HUB_TOKEN
|
81 |
model_files = os.listdir(model_dir)
|
82 |
for model_file in model_files:
|
83 |
s3_path = f"lilmeaty_garca/{model_name}/{model_file}"
|
@@ -85,6 +85,7 @@ class S3ModelLoader:
|
|
85 |
logging.info(f"Model {model_name} saved to S3 successfully.")
|
86 |
except Exception as e:
|
87 |
logging.error(f"Error downloading model {model_name} from Hugging Face: {e}")
|
|
|
88 |
|
89 |
def download_all_models_in_background(self):
|
90 |
models_url = "https://huggingface.co/api/models"
|
@@ -104,6 +105,17 @@ class S3ModelLoader:
|
|
104 |
def run_in_background(self):
|
105 |
threading.Thread(target=self.download_all_models_in_background, daemon=True).start()
|
106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
@app.on_event("startup")
|
108 |
async def startup_event():
|
109 |
model_loader.run_in_background()
|
@@ -174,29 +186,21 @@ async def generate(request: Request, body: GenerateRequest):
|
|
174 |
generator = pipeline("text-to-speech", model=model, tokenizer=tokenizer, device=device)
|
175 |
audio = generator(validated_body.input_text)
|
176 |
audio_bytesio = BytesIO()
|
177 |
-
sf.write(audio_bytesio, audio["
|
178 |
-
|
179 |
-
return
|
180 |
|
181 |
elif validated_body.task_type == "text-to-video":
|
182 |
-
|
183 |
-
generator = pipeline("text-to-video", model=model, tokenizer=tokenizer, device=device)
|
184 |
-
video = generator(validated_body.input_text)
|
185 |
-
return Response(content=video, media_type="video/mp4")
|
186 |
-
except Exception as e:
|
187 |
-
raise HTTPException(status_code=500, detail=f"Error generating video: {str(e)}")
|
188 |
-
|
189 |
else:
|
190 |
-
raise HTTPException(status_code=400, detail="Invalid task type
|
191 |
|
192 |
except Exception as e:
|
193 |
-
logging.error(f"Error
|
194 |
-
raise HTTPException(status_code=500, detail=f"Internal
|
|
|
|
|
|
|
|
|
|
|
195 |
|
196 |
-
def download_model_from_s3_or_hf(model_name):
|
197 |
-
try:
|
198 |
-
model_dir = model_loader._download_from_s3(model_name)
|
199 |
-
return model_dir
|
200 |
-
except Exception:
|
201 |
-
model_loader.download_model_from_huggingface(model_name)
|
202 |
-
return model_loader._download_from_s3(model_name)
|
|
|
2 |
import logging
|
3 |
import threading
|
4 |
import boto3
|
5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig, StoppingCriteriaList, pipeline
|
6 |
from fastapi import FastAPI, HTTPException, Request
|
7 |
from pydantic import BaseModel, field_validator
|
8 |
from huggingface_hub import hf_hub_download
|
|
|
77 |
def download_model_from_huggingface(self, model_name):
|
78 |
try:
|
79 |
logging.info(f"Downloading model {model_name} from Hugging Face...")
|
80 |
+
model_dir = hf_hub_download(model_name, token=HUGGINGFACE_HUB_TOKEN)
|
81 |
model_files = os.listdir(model_dir)
|
82 |
for model_file in model_files:
|
83 |
s3_path = f"lilmeaty_garca/{model_name}/{model_file}"
|
|
|
85 |
logging.info(f"Model {model_name} saved to S3 successfully.")
|
86 |
except Exception as e:
|
87 |
logging.error(f"Error downloading model {model_name} from Hugging Face: {e}")
|
88 |
+
raise HTTPException(status_code=500, detail=f"Error downloading model from Hugging Face: {e}")
|
89 |
|
90 |
def download_all_models_in_background(self):
|
91 |
models_url = "https://huggingface.co/api/models"
|
|
|
105 |
def run_in_background(self):
|
106 |
threading.Thread(target=self.download_all_models_in_background, daemon=True).start()
|
107 |
|
108 |
+
def load_model_and_tokenizer(self, model_name):
|
109 |
+
try:
|
110 |
+
model_uri = self._download_from_s3(model_name)
|
111 |
+
model = AutoModelForCausalLM.from_pretrained(model_uri)
|
112 |
+
tokenizer = AutoTokenizer.from_pretrained(model_uri)
|
113 |
+
logging.info(f"Model {model_name} loaded successfully from {model_uri}.")
|
114 |
+
return model, tokenizer
|
115 |
+
except Exception as e:
|
116 |
+
logging.error(f"Error loading model {model_name}: {e}")
|
117 |
+
raise HTTPException(status_code=500, detail=f"Error loading model {model_name}: {e}")
|
118 |
+
|
119 |
@app.on_event("startup")
|
120 |
async def startup_event():
|
121 |
model_loader.run_in_background()
|
|
|
186 |
generator = pipeline("text-to-speech", model=model, tokenizer=tokenizer, device=device)
|
187 |
audio = generator(validated_body.input_text)
|
188 |
audio_bytesio = BytesIO()
|
189 |
+
sf.write(audio_bytesio, audio["samples"], audio["rate"], format="WAV")
|
190 |
+
audio_bytesio.seek(0)
|
191 |
+
return StreamingResponse(audio_bytesio, media_type="audio/wav")
|
192 |
|
193 |
elif validated_body.task_type == "text-to-video":
|
194 |
+
return {"error": "Text-to-video task type is not yet supported."}
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
else:
|
196 |
+
raise HTTPException(status_code=400, detail="Invalid task type")
|
197 |
|
198 |
except Exception as e:
|
199 |
+
logging.error(f"Error during generation: {e}")
|
200 |
+
raise HTTPException(status_code=500, detail=f"Internal Server Error: {e}")
|
201 |
+
|
202 |
+
import uvicorn
|
203 |
+
|
204 |
+
if __name__ == "__main__":
|
205 |
+
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True)
|
206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|