Commit 46f764ee authored by p x's avatar p x
Browse files

让文本可输入

parent ab1fd687
...@@ -57,7 +57,7 @@ class InferPtJavaK { ...@@ -57,7 +57,7 @@ class InferPtJavaK {
private val mapper = ObjectMapper() private val mapper = ObjectMapper()
suspend fun initialize(context: Context) { suspend fun initialize(context: Context) : Boolean{
try { try {
var parentFileDir = File(context.filesDir, "$basePath/intent_tokenizer") var parentFileDir = File(context.filesDir, "$basePath/intent_tokenizer")
if (!parentFileDir.exists()) if (!parentFileDir.exists())
...@@ -125,8 +125,10 @@ class InferPtJavaK { ...@@ -125,8 +125,10 @@ class InferPtJavaK {
.optEngine("PyTorch").optTranslator(NoopTranslator()).build() .optEngine("PyTorch").optTranslator(NoopTranslator()).build()
slotModel = slotCriteria.loadModel() slotModel = slotCriteria.loadModel()
slotPredictor = slotModel!!.newPredictor() as Predictor<NDList?, NDList>? slotPredictor = slotModel!!.newPredictor() as Predictor<NDList?, NDList>?
return true
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
return false
} }
} }
......
package com.sd.hmct_small_model package com.sd.hmct_small_model
import android.app.ProgressDialog
import android.content.ComponentName import android.content.ComponentName
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import androidx.activity.enableEdgeToEdge import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.snackbar.Snackbar
import com.sd.hmct_small_model.databinding.ActivityMainBinding import com.sd.hmct_small_model.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
...@@ -15,6 +17,8 @@ class MainActivity : AppCompatActivity() { ...@@ -15,6 +17,8 @@ class MainActivity : AppCompatActivity() {
val pyTorchVm: PyTorchVm by viewModels() val pyTorchVm: PyTorchVm by viewModels()
// val tensorFlowVm: TensorFlowVm by viewModels() // val tensorFlowVm: TensorFlowVm by viewModels()
private var pd: ProgressDialog? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
enableEdgeToEdge() enableEdgeToEdge()
...@@ -25,7 +29,6 @@ class MainActivity : AppCompatActivity() { ...@@ -25,7 +29,6 @@ class MainActivity : AppCompatActivity() {
// v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) // v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
// insets // insets
// } // }
pyTorchVm.initialize(this) pyTorchVm.initialize(this)
setLis() setLis()
} }
...@@ -60,16 +63,30 @@ class MainActivity : AppCompatActivity() { ...@@ -60,16 +63,30 @@ class MainActivity : AppCompatActivity() {
private fun setLis() { private fun setLis() {
binding.bt1.setOnClickListener { binding.bt1.setOnClickListener {
var str = binding.et.text.toString() if (pyTorchVm.isInitialized) {
pyTorchVm.analyzeText(str) showProgress()
var str = binding.et.text.toString()
pyTorchVm.analyzeText(str)
} else {
Snackbar.make(binding.root, "请等待模型加载完成", Snackbar.LENGTH_SHORT).show()
}
} }
pyTorchVm.starDemo.observe(this) { it -> pyTorchVm.starDemo.observe(this) { it ->
binding.et.setText("打开车窗到60%") pd?.dismiss()
binding.et.setHint("打开车窗到60%")
starDemoApl(it.name, it.value, it.status, it.speed) starDemoApl(it.name, it.value, it.status, it.speed)
} }
} }
private fun showProgress() {
pd = ProgressDialog(this)
pd?.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd?.setMessage("正在计算,请稍后");
pd?.setIndeterminate(false);
pd?.setCancelable(true);
pd?.show();
}
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
......
...@@ -28,6 +28,8 @@ class PyTorchVm : ViewModel() { ...@@ -28,6 +28,8 @@ class PyTorchVm : ViewModel() {
private var inferPtJavaK = InferPtJavaK.instance private var inferPtJavaK = InferPtJavaK.instance
var starDemo = MutableLiveData<StarDemoBean>() var starDemo = MutableLiveData<StarDemoBean>()
//模型是否加载完成
var isInitialized = false
fun initialize(context: Context) { fun initialize(context: Context) {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
...@@ -41,7 +43,7 @@ class PyTorchVm : ViewModel() { ...@@ -41,7 +43,7 @@ class PyTorchVm : ViewModel() {
// } catch (e: Exception) { // } catch (e: Exception) {
// e.printStackTrace() // e.printStackTrace()
// } // }
inferPtJavaK.initialize(context) isInitialized = inferPtJavaK.initialize(context)
} }
} }
......
...@@ -13,14 +13,15 @@ ...@@ -13,14 +13,15 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="20dp" android:layout_marginTop="20dp"
android:maxLines="1" android:maxLines="1"
android:text="空调设置45" /> android:text=""
android:hint="空调设置45"/>
<Button <Button
android:id="@+id/bt1" android:id="@+id/bt1"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Button" /> android:text="开始计算" />
</LinearLayout> </LinearLayout>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment