-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rodar modelos automaticamente com dataset em txt
- Loading branch information
1 parent
8895029
commit c80e8e3
Showing
6 changed files
with
998 additions
and
24 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
android/MLCChat/app/src/main/java/ai/mlc/mlcchat/BenchmarkingView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters