Skip to content

Commit

Permalink
Merge pull request #16 from anrouxel/AddPrescription-View
Browse files Browse the repository at this point in the history
Add prescription view
  • Loading branch information
anrouxel authored Jan 12, 2024
2 parents f8c31ee + dad5e5e commit 6fb86e7
Show file tree
Hide file tree
Showing 58 changed files with 2,733 additions and 379 deletions.
18 changes: 15 additions & 3 deletions .idea/sonarlint/issuestore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions .idea/sonarlint/securityhotspotstore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ dependencies {
implementation("androidx.room:room-runtime:2.5.0")
ksp("androidx.room:room-compiler:2.5.0")

// Alarm
implementation("com.github.ColdTea-Projects:SmplrAlarm:2.1.0")

testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>

<application
android:allowBackup="true"
tools:replace="android:allowBackup"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/assets
Submodule assets updated from 614c01 to 2bf2fd
10 changes: 7 additions & 3 deletions app/src/main/java/fr/medicapp/medicapp/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package fr.medicapp.medicapp

import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.RequiresApi
import androidx.compose.runtime.getValue
import androidx.navigation.compose.rememberNavController
import fr.medicapp.medicapp.ai.PrescriptionAI
import fr.medicapp.medicapp.database.AppDatabase
import fr.medicapp.medicapp.entity.DoctorEntity
import fr.medicapp.medicapp.entity.PrescriptionEntity
import fr.medicapp.medicapp.ui.navigation.RootNavGraph
import fr.medicapp.medicapp.ui.theme.MedicAppTheme

class MainActivity : ComponentActivity() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val db = AppDatabase.getInstance(this)
AppDatabase.getInstance(this)
PrescriptionAI.getInstance(this)

setContent {
MedicAppTheme {
Expand Down
60 changes: 47 additions & 13 deletions app/src/main/java/fr/medicapp/medicapp/ai/PrescriptionAI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package fr.medicapp.medicapp.ai

import android.content.Context
import android.content.res.AssetManager
import android.net.Uri
import android.os.Handler
import android.os.HandlerThread
import android.util.Log
import androidx.annotation.WorkerThread
import com.google.mlkit.vision.common.InputImage
import com.google.mlkit.vision.text.TextRecognition
import com.google.mlkit.vision.text.latin.TextRecognizerOptions
import fr.medicapp.medicapp.tokenization.Feature
import fr.medicapp.medicapp.tokenization.FeatureConverter
import org.pytorch.IValue
Expand Down Expand Up @@ -39,23 +43,50 @@ class PrescriptionAI(
private val PREDICT_ANS_NUM = 5
private val NUM_LITE_THREADS = 4

/*val modelRunnable = Runnable {
private val recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)

}*/

fun analyse(visionText: String): MutableList<Pair<String, String>> {
val latch = CountDownLatch(1)
var result = mutableListOf<Pair<String, String>>()
init {
mBackgroundThread = HandlerThread("BackgroundThread")
mBackgroundThread.start()
mHandle = Handler(mBackgroundThread.looper)
mHandle.post {
loadModel()
result = runModel(visionText)
latch.countDown() // Decrements the count of the latch, releasing all waiting threads if the count reaches zero.
}
latch.await() // Causes the current thread to wait until the latch has counted down to zero.
return result
}

fun analyse(
imageUri: Uri,
onPrediction: (MutableList<Pair<String, String>>) -> Unit,
onDismiss: () -> Unit
) {
mHandle.post {
while (mModule == null) {
Thread.sleep(100)
}
val visionText = recognizeText(imageUri)
if (visionText != null) {
val sentenceTokenized = runModel(visionText)
onPrediction(sentenceTokenized)
}
onDismiss()
}
}

@WorkerThread
private fun recognizeText(imageUri: Uri): String? {
var visionText: String? = null
val latch = CountDownLatch(1)
val image = InputImage.fromFilePath(context, imageUri)
recognizer.process(image)
.addOnSuccessListener {
visionText = it.text
latch.countDown()
}
.addOnFailureListener {
latch.countDown()
}
latch.await()
return visionText
}

@WorkerThread
Expand All @@ -76,7 +107,7 @@ class PrescriptionAI(
}

val moduleFileAbsoluteFilePath: String? = assetFilePath(context.assets)
mModule = Module.load(moduleFileAbsoluteFilePath)//, mutableMapOf(), Device.CPU)
mModule = Module.load(moduleFileAbsoluteFilePath) // , mutableMapOf(), Device.CPU)
Log.v(TAG, "Model loaded.")
}
}
Expand Down Expand Up @@ -150,7 +181,10 @@ class PrescriptionAI(

for (i in predictionsLabelList.indices) {
if (predictionsLabelList[i] != "O") {
Log.d("AIModel", "Prediction: ${feature.origTokens[i]} -> ${predictionsLabelList[i]}")
Log.d(
"AIModel",
"Prediction: ${feature.origTokens[i]} -> ${predictionsLabelList[i]}"
)
sentenceTokenized.add(Pair(feature.origTokens[i], predictionsLabelList[i]))
}
}
Expand Down Expand Up @@ -209,4 +243,4 @@ class PrescriptionAI(
}
}
}
}
}
39 changes: 39 additions & 0 deletions app/src/main/java/fr/medicapp/medicapp/dao/MedicationDAO.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package fr.medicapp.medicapp.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import fr.medicapp.medicapp.entity.MedicationEntity
import fr.medicapp.medicapp.entity.UserEntity

@Dao
interface MedicationDAO {
@Query("SELECT * FROM Medications")
fun getAll(): List<MedicationEntity>

@Query("SELECT * FROM Medications WHERE CommercializationStatus NOT LIKE 'NON COMMERCIALISÉE'")
fun getAllWithoutNotTreadings(): List<MedicationEntity>

@Query("SELECT * FROM Medications WHERE cisCode = :id")
fun getOne(id: String): MedicationEntity

@Insert
fun add(t: MedicationEntity)

@Insert
fun addAll(vararg t: MedicationEntity)

@Delete
fun delete(t: MedicationEntity)

@Delete
fun deleteAll(vararg t: MedicationEntity)

@Update
fun update(t: MedicationEntity)

@Update
fun updateAll(vararg t: MedicationEntity)
}
41 changes: 41 additions & 0 deletions app/src/main/java/fr/medicapp/medicapp/dao/NotificationDAO.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package fr.medicapp.medicapp.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import fr.medicapp.medicapp.entity.DoctorEntity
import fr.medicapp.medicapp.entity.NotificationEntity
import fr.medicapp.medicapp.entity.SideEffectEntity

@Dao
interface NotificationDAO {
@Query("SELECT * FROM NotificationEntity")
fun getAll(): List<NotificationEntity>

@Query("SELECT * FROM NotificationEntity WHERE id = :id")
fun getOne(id: String): NotificationEntity

@Query("SELECT * FROM NotificationEntity WHERE medicationName = :medicament")
fun getByMedicament(medicament: String): List<NotificationEntity>


@Insert
fun add(t: NotificationEntity)

@Insert
fun addAll(vararg t: NotificationEntity)

@Delete
fun delete(t: NotificationEntity)

@Delete
fun deleteAll(vararg t: NotificationEntity)

@Update
fun update(t: NotificationEntity)

@Update
fun updateAll(vararg t: NotificationEntity)
}
3 changes: 3 additions & 0 deletions app/src/main/java/fr/medicapp/medicapp/dao/SideEffectDAO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ interface SideEffectDAO {
@Query("SELECT * FROM SideEffectEntity WHERE id = :id")
fun getOne(id: String): SideEffectEntity

@Query("SELECT * FROM SideEffectEntity WHERE medicament = :medicament")
fun getByMedicament(medicament: String): List<SideEffectEntity>

@Insert
fun add(t: SideEffectEntity)

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/fr/medicapp/medicapp/dao/TreatmentDAO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ interface TreatmentDAO {
@Query("SELECT * FROM TreatmentEntity WHERE id = :id")
fun getOne(id: String): TreatmentEntity

@Query("SELECT * FROM TreatmentEntity WHERE notification = 1")
fun getWithNotification(): List<TreatmentEntity>

@Insert
fun add(t: TreatmentEntity)

Expand Down
Binary file not shown.
Loading

0 comments on commit 6fb86e7

Please sign in to comment.