Skip to content

Commit

Permalink
Rodar modelos automaticamente com dataset em txt
Browse files Browse the repository at this point in the history
  • Loading branch information
ThalesBezerra21 committed Jun 26, 2024
1 parent 8895029 commit c80e8e3
Show file tree
Hide file tree
Showing 6 changed files with 998 additions and 24 deletions.
758 changes: 758 additions & 0 deletions android/MLCChat/app/src/main/assets/qa_dataset.txt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import ai.mlc.mlcllm.OpenAIProtocol.ChatCompletionMessage
import kotlinx.coroutines.*

val benchmarkingModelsLabels = listOf(
"Qwen2-1.5B-Instruct-q4f16_1-MLC",
//"Qwen2-1.5B-Instruct-q4f16_1-MLC",
"gemma-2b-q4f16_1-MLC"
)

Expand Down
165 changes: 165 additions & 0 deletions android/MLCChat/app/src/main/java/ai/mlc/mlcchat/BenchmarkingView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package ai.mlc.mlcchat

import ai.mlc.mlcchat.utils.benchmark.ResultViewModel
import android.content.Context
import android.util.Log
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.BarChart
import androidx.compose.material.icons.filled.Replay
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import kotlinx.coroutines.launch
import java.io.BufferedReader
import java.io.InputStreamReader

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun BenchmarkingView(
navController: NavController,
viewModel: AppViewModel,
resultViewModel: ResultViewModel
){

val localFocusManager = LocalFocusManager.current
val context = LocalContext.current
val questionsFileName = "qa_dataset.txt"

val chatState = viewModel.chatState

val modelChatState by remember {
viewModel.chatState.modelChatState
}

val questions = remember {
readQuestionsFile(context, questionsFileName).subList(0,3)
}

var pendingModels by remember {
mutableStateOf(viewModel.benchmarkingModels)
}

var pendingQuestions by remember {
mutableStateOf(questions)
}

LaunchedEffect(pendingModels) {
if(pendingModels.isNotEmpty()){
val modelState = pendingModels[0]
modelState.startChat()
}
}

LaunchedEffect(modelChatState) {
if(chatState.chatable()){

if(pendingQuestions.isEmpty()){

if(pendingModels.isEmpty()){

}else{
pendingModels = pendingModels.subList(1, pendingModels.size)
pendingQuestions = questions
}

}else{
chatState.requestGenerate(pendingQuestions[0])
pendingQuestions = pendingQuestions.subList(1, pendingQuestions.size)
}
}
}

Scaffold(topBar = {
TopAppBar(
title = {
Text(
text = chatState.modelName.value.split("-")[0],
color = MaterialTheme.colorScheme.onPrimary
)
},
colors = TopAppBarDefaults.topAppBarColors(containerColor = MaterialTheme.colorScheme.primary),
)
}, modifier = Modifier.pointerInput(Unit) {
detectTapGestures(onTap = {
localFocusManager.clearFocus()
})
}) {
paddingValues ->
Column(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
.padding(horizontal = 10.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
val lazyColumnListState = rememberLazyListState()
val coroutineScope = rememberCoroutineScope()

if(chatState.messages.isEmpty()){
CircularProgressIndicator()
}else{
MessagesView(
modifier = Modifier.fillMaxSize(),
lazyColumnListState = lazyColumnListState,
coroutineScope = coroutineScope,
chatState = chatState
)
}
}
}
}

fun readQuestionsFile(context: Context, fileName: String): List<String> {
val inputStream = context.assets.open(fileName)
val reader = BufferedReader(InputStreamReader(inputStream))
val stringBuilder = StringBuilder()
var line: String? = reader.readLine()
while (line != null) {
stringBuilder.append(line).append('\n')
line = reader.readLine()
}
reader.close()

val content = stringBuilder.toString()
val parts = content.split("\n\n")
val result = mutableListOf<String>()

for (part in parts) {
val lines = part.split("\n")
if (lines.size >= 2) {
result.add("${lines[0]} ${lines[1]}")
}
}

return result.toList()
}
46 changes: 29 additions & 17 deletions android/MLCChat/app/src/main/java/ai/mlc/mlcchat/ChatView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
Expand Down Expand Up @@ -59,6 +60,7 @@ import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -172,24 +174,12 @@ fun ChatView(
)
BenchmarkView()
Divider(thickness = 1.dp, modifier = Modifier.padding(vertical = 5.dp))
LazyColumn(
MessagesView(
modifier = Modifier.weight(9f),
verticalArrangement = Arrangement.spacedBy(5.dp, alignment = Alignment.Bottom),
state = lazyColumnListState
) {
coroutineScope.launch {
lazyColumnListState.animateScrollToItem(chatState.messages.size)
}
items(
items = chatState.messages,
key = { message -> message.id },
) { message ->
MessageView(messageData = message)
}
item {
// place holder item for scrolling to the bottom
}
}
lazyColumnListState = lazyColumnListState,
coroutineScope = coroutineScope,
chatState = chatState
)
Divider(thickness = 1.dp, modifier = Modifier.padding(top = 5.dp))
SendMessageView(chatState = chatState)
}
Expand Down Expand Up @@ -227,6 +217,28 @@ fun BenchmarkView(modifier: Modifier = Modifier) {
}
}

@Composable
fun MessagesView(modifier: Modifier = Modifier, lazyColumnListState: LazyListState, coroutineScope: CoroutineScope, chatState: AppViewModel.ChatState) {
LazyColumn(
modifier = modifier,
verticalArrangement = Arrangement.spacedBy(5.dp, alignment = Alignment.Bottom),
state = lazyColumnListState
) {
coroutineScope.launch {
lazyColumnListState.animateScrollToItem(chatState.messages.size)
}
items(
items = chatState.messages,
key = { message -> message.id },
) { message ->
MessageView(messageData = message)
}
item {
// place holder item for scrolling to the bottom
}
}
}

@Composable
fun MessageView(messageData: MessageData) {
SelectionContainer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fun HomeView(
var downloadingModels by remember { mutableStateOf(false) }

fun startBenchmarking() {

navController.navigate("benchmarking")
}

fun openDownloadDialog() {
Expand Down
49 changes: 44 additions & 5 deletions android/MLCChat/app/src/main/java/ai/mlc/mlcchat/NavView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,51 @@ import androidx.navigation.compose.rememberNavController

@ExperimentalMaterial3Api
@Composable
fun NavView(appViewModel: AppViewModel = viewModel(), resultViewModel: ResultViewModel = viewModel()) {
fun NavView(
appViewModel: AppViewModel = viewModel(),
resultViewModel: ResultViewModel = viewModel()
) {

val navController = rememberNavController()

NavHost(navController = navController, startDestination = "main") {
composable("main") { HomeView(navController, appViewModel) }
composable("home") { StartView(navController, appViewModel) }
composable("chat") { ChatView(navController, appViewModel.chatState, resultViewModel) }
composable("result") { ResultView(navController, resultViewModel) }

composable("main") {
HomeView(
navController,
appViewModel
)
}

composable("benchmarking") {
BenchmarkingView(
navController = navController,
viewModel = appViewModel,
resultViewModel = resultViewModel
)
}

composable("home") {
StartView(
navController,
appViewModel
)
}

composable("chat") {
ChatView(
navController,
appViewModel.chatState,
resultViewModel
)
}

composable("result") {
ResultView(
navController,
resultViewModel
)
}

}
}

0 comments on commit c80e8e3

Please sign in to comment.