Update app.py
Browse files1234456747 unmodified lines5556575859585960616263636465666767686970116 unmodified lines18718818919019019119219319419519610 unmodified lines207208209207210211212213214215216import osimport torchfrom fastapi import FastAPI, HTTPExceptionfrom fastapi.responses import JSONResponsefrom fastapi.responses import JSONResponse, StreamingResponsefrom pydantic import BaseModel, field_validatorfrom transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer, GenerationConfig, StoppingCriteriaListimport boto347 unmodified lines
@field_validator("max_new_tokens") def max_new_tokens_must_be_within_limit(cls, v): if v > 4: raise ValueError("max_new_tokens cannot be greater than 4.") if v > 500: raise ValueError("max_new_tokens cannot be greater than 500.") return v
class S3ModelLoader: def.__init__(self, bucket_name, s3_client): def __init__(self, bucket_name, s3_client): self.bucket_name = bucket_name self.s3_client = s3_client
def._get_s3_uri(self, model_name): def _get_s3_uri(self, model_name): return f"s3://{self.bucket_name}/{model_name.replace('/', '-')}"
async def load_model_and_tokenizer(self, model_name):116 unmodified lines audio = audio_generator(validated_body.input_text)[0]
audio_byte_arr = BytesIO() audio.save(audio_byte_arr) # It is expected that the audio is saved as wav. # Saving like this will not always work. Please check how your # audio_generator model is working. audio_generator.save_audio(audio_byte_arr, audio) audio_byte_arr.seek(0)
return StreamingResponse(audio_byte_arr, media_type="audio/wav")10 unmodified lines video = video_generator(validated_body.input_text)[0]
video_byte_arr = BytesIO() video.save(video_byte_arr) # Same as above. Please check how your video model is returning the # videos and save them accordingly. # It is expected that the video is saved as MP4 video_generator.save_video(video_byte_arr, video) video_byte_arr.seek(0)
return StreamingResponse(video_byte_arr, media_type="video/mp4")app.py+13-7