commit 3406c5817243d9aab736be7a102ac3c1ab232da3 Author: Ari Putra Wijaya Date: Mon May 12 10:48:33 2025 +0800 test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..6bceee3 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Ai Sample \ No newline at end of file diff --git a/.idea/AndroidProjectSystem.xml b/.idea/AndroidProjectSystem.xml new file mode 100644 index 0000000..4a53bee --- /dev/null +++ b/.idea/AndroidProjectSystem.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/deploymentTargetSelector.xml b/.idea/deploymentTargetSelector.xml new file mode 100644 index 0000000..b268ef3 --- /dev/null +++ b/.idea/deploymentTargetSelector.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..639c779 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..c224ad5 --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/migrations.xml b/.idea/migrations.xml new file mode 100644 index 0000000..f8051a6 --- /dev/null +++ b/.idea/migrations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..b2c751a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..16660f1 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle.kts b/app/build.gradle.kts new file mode 100644 index 0000000..6ba820e --- /dev/null +++ b/app/build.gradle.kts @@ -0,0 +1,62 @@ +plugins { + alias(libs.plugins.android.application) + alias(libs.plugins.kotlin.android) + id("com.google.gms.google-services") +} + +android { + namespace = "com.balimedia.aisample" + compileSdk = 35 + + defaultConfig { + applicationId = "com.balimedia.aisample" + minSdk = 24 + targetSdk = 35 + versionCode = 1 + versionName = "1.0" + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } + kotlinOptions { + jvmTarget = "11" + } +} + +dependencies { + + implementation(libs.androidx.core.ktx) + implementation(libs.androidx.appcompat) + implementation(libs.material) + implementation(libs.androidx.activity) + implementation(libs.androidx.constraintlayout) + implementation(libs.firebase.bom) + implementation(libs.firebase.vertexai) + implementation(libs.androidx.lifecycle.runtime.ktx) + implementation(libs.firebase.analytics) + implementation(libs.compose.theme.adapter) + + testImplementation(libs.junit) + androidTestImplementation(libs.androidx.junit) + androidTestImplementation(libs.androidx.espresso.core) + + implementation(libs.androidx.lifecycle.runtime.ktx) // Check for the latest version + implementation("com.github.frhnfrq:MathView:1.2") + implementation ("io.noties.markwon:core:4.6.2") + implementation ("io.noties.markwon:ext-latex:4.6.2") + implementation("io.noties.markwon:inline-parser:4.6.2") + +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/com/balimedia/aisample/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/balimedia/aisample/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..09abd6c --- /dev/null +++ b/app/src/androidTest/java/com/balimedia/aisample/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package com.balimedia.aisample + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("com.balimedia.aisample", appContext.packageName) + } +} \ No newline at end of file diff --git a/app/src/google-services.json b/app/src/google-services.json new file mode 100644 index 0000000..8869d5f --- /dev/null +++ b/app/src/google-services.json @@ -0,0 +1,29 @@ +{ + "project_info": { + "project_number": "487905885152", + "project_id": "nanta-ai", + "storage_bucket": "nanta-ai.firebasestorage.app" + }, + "client": [ + { + "client_info": { + "mobilesdk_app_id": "1:487905885152:android:e3458f12cb55b66ec90985", + "android_client_info": { + "package_name": "com.balimedia.aisample" + } + }, + "oauth_client": [], + "api_key": [ + { + "current_key": "AIzaSyBlUrnE5TCvn33LxAWw0nI32rHSKREj7XU" + } + ], + "services": { + "appinvite_service": { + "other_platform_oauth_client": [] + } + } + } + ], + "configuration_version": "1" +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..a4a5608 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/balimedia/aisample/MainActivity.kt b/app/src/main/java/com/balimedia/aisample/MainActivity.kt new file mode 100644 index 0000000..8bb681a --- /dev/null +++ b/app/src/main/java/com/balimedia/aisample/MainActivity.kt @@ -0,0 +1,132 @@ +package com.balimedia.aisample + +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.graphics.LinearGradient +import android.os.Bundle +import android.util.Log +import android.widget.Button +import android.widget.EditText +import android.widget.TextView +import androidx.activity.enableEdgeToEdge +import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.lifecycleScope +import com.google.firebase.Firebase +import com.google.firebase.FirebaseApp +import com.google.firebase.vertexai.type.content +import com.google.firebase.vertexai.vertexAI +import io.noties.markwon.Markwon +import io.noties.markwon.ext.latex.JLatexMathPlugin +import io.noties.markwon.inlineparser.MarkwonInlineParserPlugin +import kotlinx.coroutines.launch + +class MainActivity : AppCompatActivity() { + private lateinit var promptEditText: EditText + private lateinit var generateButton: Button + private lateinit var resultTextView: TextView + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + promptEditText = findViewById(R.id.promptEditText) + generateButton = findViewById(R.id.generateButton) + resultTextView = findViewById(R.id.resultTextView) + FirebaseApp.initializeApp(this) + // Inisialisasi Vertex AI + val vertexAI = Firebase.vertexAI() + val generativeModel = vertexAI.generativeModel(modelName = "gemini-2.0-flash", systemInstruction = content { text("Anda adalah asistan virtual yang bertugas membantu mengerjakan soal dan informatif") }) + //val generativeModel = vertexAI.generativeModel(modelName = "gemini-2.0-flash" ) + val markwon = Markwon.builder(this) // 'this' dari Java menjadi 'context' di sini + // required plugin to support inline parsing + .usePlugin(MarkwonInlineParserPlugin.create()) + // gunakan JLatexMathPlugin.create() dengan lambda untuk konfigurasi builder + .usePlugin(JLatexMathPlugin.create(resultTextView.textSize) { builder -> + // ENABLE inlines + builder.inlinesEnabled(true) + }) + .build() + val bitmap: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.ss) + +// set markdown + + /*generateButton.setOnClickListener { + val prompt = promptEditText.text.toString() + if (prompt.isNotBlank()) { + resultTextView.text = "" // Bersihkan hasil sebelumnya + generateButton.isEnabled = false // Nonaktifkan tombol saat generasi berlangsung + var result = "" + var total=0 + + // Luncurkan coroutine untuk melakukan streaming + lifecycleScope.launch { + try { + val response = generativeModel.generateContentStream(prompt) + + // Kumpulkan dan tampilkan chunk teks + response.collect { chunk -> + val chunkText = chunk.text + + if (!chunkText.isNullOrEmpty()) { + //resultTextView.append(chunkText)// Tambahkan chunk ke TextView(chunkText) // Tambahkan chunk ke TextView + result += chunkText + markwon.setMarkdown(resultTextView,result); + } + chunk.usageMetadata?.let { it1 -> total = it1.totalTokenCount } + } + println("Streaming complete. Total tokens: $total") // Log ketika streaming selesai + + } catch (e: Exception) { + resultTextView.text= "Error: ${e.message}" + println("Error during streaming: ${e.message}") + } finally { + generateButton.isEnabled = true // Aktifkan kembali tombol + } + } + } + }*/ + val prompt = content { + image(bitmap) + text("kerjakan soal sesuai gambar berikan penjelasan mendetail tampilkan rumusnya dalam format LaTeX tanpa markdown") + } + generateButton.setOnClickListener { + //val prompt = promptEditText.text.toString() + resultTextView.text = "" // Bersihkan hasil sebelumnya + generateButton.isEnabled = false // Nonaktifkan tombol saat generasi berlangsung + var result = "" + var total=0 + + // Luncurkan coroutine untuk melakukan streaming + lifecycleScope.launch { + try { + val response = generativeModel.generateContentStream(prompt) + + // Kumpulkan dan tampilkan chunk teks + response.collect { chunk -> + val chunkText = chunk.text + + if (!chunkText.isNullOrEmpty()) { + //resultTextView.append(chunkText)// Tambahkan chunk ke TextView(chunkText) // Tambahkan chunk ke TextView + result += chunkText + + + //markwon.setMarkdown(resultTextView,result); + var node = markwon.parse(result) + var markdown = markwon.render(node) + markwon.setParsedMarkdown(resultTextView, markdown); + Log.d("#LATEX",result) + } + chunk.usageMetadata?.let { it1 -> total = it1.totalTokenCount } + } + println("Streaming complete. Total tokens: $total") // Log ketika streaming selesai + + } catch (e: Exception) { + resultTextView.text= "Error: ${e.message}" + println("Error during streaming: ${e.message}") + } finally { + generateButton.isEnabled = true // Aktifkan kembali tombol + } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_launcher_foreground.xml b/app/src/main/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ss.png b/app/src/main/res/drawable/ss.png new file mode 100644 index 0000000..7c685cd Binary files /dev/null and b/app/src/main/res/drawable/ss.png differ diff --git a/app/src/main/res/font/font.ttf b/app/src/main/res/font/font.ttf new file mode 100644 index 0000000..7062ca1 Binary files /dev/null and b/app/src/main/res/font/font.ttf differ diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..017a1a0 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,44 @@ + + + + + +