Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,31 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
from PIL import Image
|
| 4 |
-
from fourm.models.fm import FM
|
| 5 |
-
from torchvision import transforms
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
st.set_page_config(layout="wide")
|
| 9 |
-
st.title("4M-7_B Multimodal Demo (CPU Mode)")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
@st.cache_resource
|
| 13 |
-
def load_4m_model():
|
| 14 |
-
# Load the 0.4B parameter model directly to CPU
|
| 15 |
-
model = FM.from_pretrained("EPFL-VILAB/4M-7_B_CC12M")
|
| 16 |
-
model.to("cpu")
|
| 17 |
-
model.eval()
|
| 18 |
-
return model
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
if
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
with col1:
|
| 31 |
-
st.image(img, caption="Original Image")
|
| 32 |
-
|
| 33 |
-
# Preprocessing
|
| 34 |
-
preprocess = transforms.Compose([
|
| 35 |
-
transforms.Resize(224),
|
| 36 |
-
transforms.CenterCrop(224),
|
| 37 |
-
transforms.ToTensor(),
|
| 38 |
-
])
|
| 39 |
-
input_tensor = preprocess(img).unsqueeze(0)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
with st.spinner("Processing on CPU..."):
|
| 43 |
-
with torch.no_grad():
|
| 44 |
-
# Generate depth/normals (target_modalities can be 'normal' or 'depth')
|
| 45 |
-
preds = model.generate({'rgb': input_tensor}, target_modalities=['normal'])
|
| 46 |
-
|
| 47 |
-
# Post-process output
|
| 48 |
-
output_tensor = preds['normal'][0].clamp(0, 1)
|
| 49 |
-
output_img = transforms.ToPILImage()(output_tensor)
|
| 50 |
-
|
| 51 |
-
with col2:
|
| 52 |
-
st.image(output_img, caption="Predicted Surface Normals")
|
| 53 |
-
st.success("Done!")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import os
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# 1. Page Config
|
| 5 |
+
st.set_page_config(page_title="4M CPU Boot", layout="wide")
|
|
|
|
| 6 |
|
| 7 |
+
st.title("4M-7_B Multimodal (CPU Optimization)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# 2. Delayed Imports (Helps avoid crash on start)
|
| 10 |
+
try:
|
| 11 |
+
import torch
|
| 12 |
+
from PIL import Image
|
| 13 |
+
from fourm.models.fm import FM
|
| 14 |
+
from torchvision import transforms
|
| 15 |
+
st.success("All libraries loaded successfully!")
|
| 16 |
+
except ImportError as e:
|
| 17 |
+
st.error(f"Missing library: {e}")
|
| 18 |
+
st.stop()
|
| 19 |
|
| 20 |
+
# 3. Load Model with local_files_only=False first time
|
| 21 |
+
@st.cache_resource
|
| 22 |
+
def load_model():
|
| 23 |
+
# This downloads the model weights (approx 1.6GB)
|
| 24 |
+
return FM.from_pretrained("EPFL-VILAB/4M-7_B_CC12M").to("cpu").eval()
|
| 25 |
|
| 26 |
+
if st.button("Initialize Model"):
|
| 27 |
+
with st.spinner("Downloading/Loading weights to RAM..."):
|
| 28 |
+
model = load_model()
|
| 29 |
+
st.write("Model is ready for inference!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# Add your inference code (the upload and predict part) below here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|