Skip to content

Commit

Permalink
feat: make display works
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 10, 2023
1 parent 38d0d7d commit 0b2a369
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 51 deletions.
11 changes: 0 additions & 11 deletions app/src/main/java/org/unitmesh/llmpoc/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,5 @@ class MainActivity : AppCompatActivity() {
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)

val stSemantic = STSemantic.create(this)

val embeddingStore = InMemoryEmbeddingStore<Document>()

listOf("That is a happy dog", "That is a very happy person", "Today is a sunny day")
.map { Document.from(it) }
.forEach { embeddingStore.add(stSemantic.embed(it.text), it) }

embeddingStore.findRelevant(stSemantic.embed("That is a happy person"), 3)
.forEach { println(it) }
}
}
68 changes: 58 additions & 10 deletions app/src/main/java/org/unitmesh/llmpoc/ui/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import cc.unitmesh.rag.document.Document
import cc.unitmesh.rag.store.InMemoryEmbeddingStore
import org.unitmesh.llmpoc.R
import org.unitmesh.llmpoc.databinding.FragmentHomeBinding
import org.unitmesh.llmpoc.embedding.STSemantic

class HomeFragment : Fragment() {

private lateinit var embeddingStore: InMemoryEmbeddingStore<Document>
private lateinit var stSemantic: STSemantic
private var _binding: FragmentHomeBinding? = null

// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
private lateinit var textContainer: LinearLayout

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -30,18 +35,28 @@ class HomeFragment : Fragment() {
_binding = FragmentHomeBinding.inflate(inflater, container, false)
val root: View = binding.root

val textView: TextView = binding.textHome
val textView: TextView = binding.defaultSource
homeViewModel.text.observe(viewLifecycleOwner) {
textView.text = it
}

// 在此处初始化布局元素
val addButton: Button = root.findViewById(R.id.addButton)
val computeButton: Button = root.findViewById(R.id.computeButton)
// 初始化容器
textContainer = root.findViewById(R.id.textContainer)

// 添加默认的 TextView
addDefaultSentence("That is a happy dog")
addDefaultSentence("That is a very happy person")
addDefaultSentence("Today is a sunny day")

val addButton: Button = root.findViewById(R.id.addButton)
addButton.setOnClickListener { addSentence() }

val computeButton: Button = root.findViewById(R.id.computeButton)
computeButton.setOnClickListener { compute() }

stSemantic = STSemantic.create(this.requireContext())
embeddingStore = InMemoryEmbeddingStore()

return root
}

Expand All @@ -51,15 +66,48 @@ class HomeFragment : Fragment() {
}

fun addSentence() {
// 创建新的 TextView
val newTextView = EditText(requireContext())
newTextView.layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)

textContainer.addView(newTextView)
}

fun addDefaultSentence(sentence: String) {
val editText = EditText(requireContext())
editText.layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)

editText.setText(sentence)
textContainer.addView(editText)
}

fun compute() {
// 获取所有的 TextView
val textViews = mutableListOf<EditText>()
for (i in 0 until textContainer.childCount) {
val child = textContainer.getChildAt(i)
if (child is EditText) {
textViews.add(child)
}
}

}
// 获取所有的句子
val sentences = textViews.map { it.text.toString() }

// 处理添加句子的逻辑
sentences.forEach { embeddingStore.add(stSemantic.embed(it), Document.from(it)) }
val defaultTextView = binding.defaultSource
val defaultText = defaultTextView.text.toString()

fun showJsonOutput(view: View) {
val textView: TextView = binding.jsonOutput
textView.text = "json output"
// 处理计算相似度的逻辑
val relevantEmbeddings = embeddingStore.findRelevant(stSemantic.embed(defaultText), 3)
val jsonOutput = binding.jsonOutput
jsonOutput.text = relevantEmbeddings.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.lifecycle.ViewModel
class HomeViewModel : ViewModel() {

private val _text = MutableLiveData<String>().apply {
value = "This is home Fragment"
value = "That is a happy person"
}

val text: LiveData<String> = _text
Expand Down
41 changes: 12 additions & 29 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,6 @@
android:orientation="vertical"
android:padding="16dp">

<!-- 标题部分 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">

<TextView
android:id="@+id/text_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inference API"
android:textSize="18sp"
android:textStyle="bold"/>

</LinearLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -58,12 +41,13 @@
android:layout_marginBottom="8dp"/>

<!-- 输入表单 -->
<EditText
android:id="@+id/editTextSource"
<LinearLayout
android:id="@+id/textContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Source Sentence"
android:inputType="text" />
android:orientation="vertical">
<!-- 此处将用于容纳新的 TextView -->
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
Expand Down Expand Up @@ -94,16 +78,15 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="16dp"
android:visibility="gone">
android:layout_marginTop="16dp">


<TextView
android:id="@+id/jsonOutput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Output"
android:layout_marginStart="8dp"/>
<TextView
android:id="@+id/jsonOutput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Output: "
android:layout_marginStart="8dp"/>

</LinearLayout>

Expand Down

0 comments on commit 0b2a369

Please sign in to comment.