InferX
OSS tool to run models on different devices with same API
Run InferX models locally on your Android device
build.gradle
build.gradle.kts
// build.gradle.kts (Project level) allprojects { repositories { google() mavenCentral() maven { url = uri("https://jitpack.io") } } }
// build.gradle.kts (App level) dependencies { implementation("com.github.exla-ai:InferX-android-sdk:latest-version") // Required dependencies implementation("androidx.core:core-ktx:1.12.0") implementation("androidx.appcompat:appcompat:1.6.1") implementation("com.google.android.material:material:1.11.0") }
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CAMERA" />
import com.inferx.android.InferXSdk class MainActivity : AppCompatActivity() { private lateinit var sdk: InferXSdk override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Initialize InferX SDK sdk = InferXSdk.getInstance(applicationContext) // Setup model download and initialization setupModel() } private fun setupModel() { // Download and initialize the model sdk.downloadModel("clip") { progress -> runOnUiThread { Log.d("InferX", "Download progress: $progress%") // Update your progress UI here } } sdk.initializeModel("clip") { success -> runOnUiThread { if (success) { Log.d("InferX", "Model ready") // Enable inference UI } else { Log.e("InferX", "Model initialization failed") } } } } }
// Image-text matching with CLIP private fun runImageTextMatching() { val imageBitmap = loadImageFromAssets("sample_image.jpg") val textQueries = listOf("a photo of a dog", "a photo of a cat", "a landscape") sdk.runInference("clip", imageBitmap, textQueries) { results -> runOnUiThread { // Process results results?.let { Log.d("InferX", "AI Response: $results") displayResults(results) } } } } private fun loadImageFromAssets(fileName: String): Bitmap? { return try { val inputStream = assets.open(fileName) BitmapFactory.decodeStream(inputStream) } catch (e: IOException) { Log.e("InferX", "Error loading image: ${e.message}") null } }
// Configure model with custom parameters val modelConfig = ModelConfig.Builder() .setModelName("clip") .setMaxInputSize(512) .setOptimizationLevel(OptimizationLevel.HIGH) .setCacheSize(100) // MB .build() sdk.initializeModel(modelConfig) { success -> // Handle initialization result }
private fun setupCameraInference() { val cameraManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager // Setup camera preview and capture sdk.startCameraInference("clip") { bitmap, results -> runOnUiThread { // Update UI with real-time results displayCameraResults(bitmap, results) } } }
private fun processBatchImages() { val imageList = listOf( loadImageFromAssets("image1.jpg"), loadImageFromAssets("image2.jpg"), loadImageFromAssets("image3.jpg") ).filterNotNull() val textQueries = listOf("a photo of a dog", "a photo of a cat") sdk.runBatchInference("clip", imageList, textQueries) { batchResults -> runOnUiThread { batchResults.forEachIndexed { index, result -> Log.d("InferX", "Image $index result: $result") } } } }
class InferXDemoActivity : AppCompatActivity() { private lateinit var sdk: InferXSdk private lateinit var progressBar: ProgressBar private lateinit var resultTextView: TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_demo) progressBar = findViewById(R.id.progressBar) resultTextView = findViewById(R.id.resultTextView) // Initialize SDK sdk = InferXSdk.getInstance(applicationContext) // Setup model setupModel() } private fun setupModel() { progressBar.visibility = View.VISIBLE // Download model with progress tracking sdk.downloadModel("clip") { progress -> runOnUiThread { Log.d("InferX", "Download progress: $progress%") progressBar.progress = progress } } // Initialize model sdk.initializeModel("clip") { success -> runOnUiThread { progressBar.visibility = View.GONE if (success) { Log.d("InferX", "Model ready") resultTextView.text = "Model ready! Tap to run inference." setupInferenceButton() } else { Log.d("InferX", "Model initialization failed") resultTextView.text = "Failed to initialize model" } } } } private fun setupInferenceButton() { findViewById<Button>(R.id.inferenceButton).setOnClickListener { runInference() } } private fun runInference() { val bitmap = loadSampleImage() val queries = listOf("a photo of a dog", "a photo of a cat", "a landscape") sdk.runInference("clip", bitmap, queries) { results -> runOnUiThread { Log.d("InferX", "AI Response: $results") resultTextView.text = "Results: $results" } } } private fun loadSampleImage(): Bitmap? { // Load your sample image here return BitmapFactory.decodeResource(resources, R.drawable.sample_image) } }
InferXSdk.setDebugMode(true)