# Convert PyTorch Model to Hugging Face model

**URL:** <https://discuss.huggingface.co/t/convert-pytorch-model-to-hugging-face-model/75956>\
**Category:** Inference Endpoints on the Hub\
**Created:** [March 5, 2024, 5:12am UTC](https://discuss.huggingface.co/t/convert-pytorch-model-to-hugging-face-model/75956 "2024-03-05T05:12:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Z3K3](https://avatars.discourse-cdn.com/v4/letter/z/a3d4f5/32.png) [@Z3K3](https://discuss.huggingface.co/u/Z3K3)\
**Post date:** [March 5, 2024, 5:12am UTC](https://discuss.huggingface.co/t/convert-pytorch-model-to-hugging-face-model/75956/1 "2024-03-05T05:12:32Z")

</div>

I have looked at a lot resources but I still have issues trying to convert a PyTorch model to a hugging face model format. I ultimately want to be able to use inference API with my custom model.

I have a “model.pt” file which I got from fine-tuning the Facebook Musicgen medium model ([The Git repo I used to train / Fine tune the model is here](https://github.com/chavinlo/musicgen_trainer)). I want to upload this to the hugging face hub so i can use this with inference API. How can I convert the `.pt` model to files/model that can be used on hugging face hub? I tried looking at other posts but there is no clear answer, or it is poorly explained.

Any help / guidance would be greatly appreciated 🙏

This is the code I have right now that is not working:

```auto
import torch
from transformers import MusicgenConfig, MusicgenModel
from audiocraft.models import musicgen
import os

os.mkdir('models')

state_dict = musicgen.MusicGen.get_pretrained('facebook/musicgen-medium', device='cuda').lm.load_state_dict(torch.load('NEW_MODEL.pt'))

config = MusicgenConfig.from_pretrained('facebook/musicgen-medium')
model = MusicgenModel(config)
model.load_state_dict(state_dict)

model.save_pretrained('/models')

loaded_model = MusicgenModel.from_pretrained('/models')

```
