Skip to content

Commit

Permalink
feat: add basic embedding api for SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 10, 2023
1 parent 4ca6a15 commit 38d0d7d
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 24 deletions.
33 changes: 28 additions & 5 deletions app/src/main/java/org/unitmesh/llmpoc/ui/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import org.unitmesh.llmpoc.R
import org.unitmesh.llmpoc.databinding.FragmentHomeBinding

class HomeFragment : Fragment() {
Expand All @@ -18,12 +21,11 @@ class HomeFragment : Fragment() {
private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
val homeViewModel =
ViewModelProvider(this).get(HomeViewModel::class.java)
val homeViewModel = ViewModelProvider(this)[HomeViewModel::class.java]

_binding = FragmentHomeBinding.inflate(inflater, container, false)
val root: View = binding.root
Expand All @@ -32,11 +34,32 @@ class HomeFragment : Fragment() {
homeViewModel.text.observe(viewLifecycleOwner) {
textView.text = it
}

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

addButton.setOnClickListener { addSentence() }
computeButton.setOnClickListener { compute() }

return root
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

fun addSentence() {

}

fun compute() {

}

fun showJsonOutput(view: View) {
val textView: TextView = binding.jsonOutput
textView.text = "json output"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,14 @@ class HomeViewModel : ViewModel() {
private val _text = MutableLiveData<String>().apply {
value = "This is home Fragment"
}

val text: LiveData<String> = _text

fun addSentence(sourceSentence: String, compareSentence: String) {
// 处理添加句子的逻辑
}

fun computeSimilarity() {
// 处理计算相似度的逻辑
}
}
129 changes: 110 additions & 19 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,113 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">

<TextView
android:id="@+id/text_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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"
android:text="Sentence Similarity"
android:textSize="14sp"
android:textColor="@color/colorGray"
android:layout_marginBottom="8dp"/>

<EditText
android:id="@+id/defaultSource"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Source Sentence"
android:inputType="text"/>

<!-- 子标题 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sentences to compare to"
android:textSize="14sp"
android:textColor="@color/colorGray"
android:layout_marginBottom="8dp"/>

<!-- 输入表单 -->
<EditText
android:id="@+id/editTextSource"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Source Sentence"
android:inputType="text" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">

<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Sentence"
android:onClick="add"
android:layout_marginEnd="8dp"/> <!-- 添加一个间距 -->

<Button
android:id="@+id/computeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Compute"
android:onClick="compute"/>

</LinearLayout>

<!-- 输出结果 -->
<LinearLayout
android:id="@+id/outputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="16dp"
android:visibility="gone">


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

</LinearLayout>

</LinearLayout>
</ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="colorGray">#FF9E9E9E</color>
<color name="colorYellow">#FFFFEB3B</color>
</resources>

0 comments on commit 38d0d7d

Please sign in to comment.