Views
No views yet
transformers==4.18.0 installed. Run the following python script to dynamically download and run the model:1"""
2Quick Start: MEDNA-DFM for DNA Methylation Prediction
3This script demonstrates how to dynamically load the MEDNA-DFM model
4from the Hugging Face Hub and perform inference on raw DNA sequences.
5"""
6
7import sys
8import torch
9import requests
10from huggingface_hub import snapshot_download
11
12
13# Environment Patch for Legacy Dependencies
14def patch_legacy_requests():
15 """
16 Patches the requests session to ensure compatibility between legacy
17 transformers (v4.18.0) and modern Hugging Face API endpoints.
18 """
19 original_request = requests.Session.request
20 def patched_request(self, method, url, *args, **kwargs):
21 if url.startswith('/api/'):
22 # Route relative API paths through a stable mirror or hub URL
23 url = 'https://hf-mirror.com' + url
24 return original_request(self, method, url, *args, **kwargs)
25 requests.Session.request = patched_request
26
27
28patch_legacy_requests()
29print("Fetching MEDNA-DFM from Hugging Face Hub...")
30REPO_ID = "hy-0003/MEDNA-DFM_6mA_XocBLS256"
31cloud_model_path = snapshot_download(repo_id=REPO_ID)
32sys.path.insert(0, cloud_model_path)
33from configuration_medna import MednaConfig
34from modeling_medna import MEDNADFMForSequenceClassification
35
36print("Initializing model weights...")
37model = MEDNADFMForSequenceClassification.from_pretrained(cloud_model_path)
38model.eval()
39
40
41# Inference on Sample DNA Sequences
42# Typically, sequences centered around the target modification site (e.g., C or A)
43test_sequences = [
44 "CGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGC", # Sample 1: GC-rich motif
45 "AAAAATTTTTAAAAATTTTTGAGGAAAAATTTTTAAAAATT" # Sample 2: A-tract motif
46]
47
48with torch.no_grad():
49 outputs = model(test_sequences)
50 probabilities = torch.softmax(outputs.logits, dim=-1)
51
52
53print(f"{'MEDNA-DFM Prediction Results':^50}")
54for i, seq in enumerate(test_sequences):
55 prob_0 = probabilities[i][0].item()
56 prob_1 = probabilities[i][1].item()
57 # Thresholding for binary classification
58 prediction = "Methylated (1)" if prob_1 > 0.5 else "Unmethylated (0)"
59 print(f"Sequence {i+1}:")
60 print(f" Snippet : {seq[:10]} ... {seq[-10:]}")
61 print(f" P(Unmethylated) : {prob_0:.4f}")
62 print(f" P(Methylated) : {prob_1:.4f}")
63 print(f" Prediction : {prediction}")
64 print("-" * 50)