Views
No views yet
1from huggingface_hub import InferenceApi
2
3inference = InferenceApi("your-username/mattergpt")
4
5# Generate a single crystal structure
6result = inference({"formation_energy": -1.0, "band_gap": 2.0})
7print(result)
8
9# Generate multiple crystal structures
10results = inference([
11 {"formation_energy": -1.0, "band_gap": 2.0},
12 {"formation_energy": -2.0, "band_gap": 3.0}
13])
14for crystal in results:
15 print(crystal)pip install torch tqdmmatter_gpt_wrapper module, which should be provided with the model.1from matter_gpt_wrapper import MatterGPTWrapper, SimpleTokenizer
2import torch
3import os
4
5# Load the model
6model_path = "./" # Directory containing config.json and pytorch_model.pt
7model = MatterGPTWrapper.from_pretrained(model_path)
8model.to('cuda' if torch.cuda.is_available() else 'cpu')
9
10# Load the tokenizer
11tokenizer_path = "Voc_prior"
12tokenizer = SimpleTokenizer(tokenizer_path)config.json, pytorch_model.pt, and Voc_prior files are in the correct locations.1def generate_single(condition):
2 context = '>'
3 x = torch.tensor([tokenizer.stoi[context]], dtype=torch.long)[None,...].to(model.device)
4 p = torch.tensor([condition]).unsqueeze(1).to(model.device)
5
6 generated = model.generate(x, prop=p, max_length=model.config.block_size,
7 temperature=1.2, do_sample=True, top_k=0, top_p=0.9)
8 return tokenizer.decode(generated[0].tolist())
9
10# Example usage
11condition = [-1.0, 2.0] # formation energy and bandgap
12single_sequence = generate_single(condition)
13print(single_sequence)1from tqdm import tqdm
2
3def generate_multiple(condition, num_sequences, batch_size=32):
4 all_sequences = []
5 for _ in tqdm(range(0, num_sequences, batch_size)):
6 current_batch_size = min(batch_size, num_sequences - len(all_sequences))
7 context = '>'
8 x = torch.tensor([tokenizer.stoi[context]], dtype=torch.long)[None,...].repeat(current_batch_size, 1).to(model.device)
9 p = torch.tensor([condition]).repeat(current_batch_size, 1).unsqueeze(1).to(model.device)
10
11 generated = model.generate(x, prop=p, max_length=model.config.block_size,
12 temperature=1.2, do_sample=True, top_k=0, top_p=0.9)
13 all_sequences.extend([tokenizer.decode(seq.tolist()) for seq in generated])
14
15 if len(all_sequences) >= num_sequences:
16 break
17
18 return all_sequences[:num_sequences]
19
20# Example usage
21condition = [-1.0, 2.0] # formation energy and bandgap
22num_sequences = 10
23multiple_sequences = generate_multiple(condition, num_sequences)
24for seq in multiple_sequences:
25 print(seq)condition parameter is a list containing the desired formation energy and bandgap values.