Upload 4 files
Browse files- app.py +60 -0
- labels.txt +24 -0
- requirements.txt.txt +4 -0
- skin_model.tflite +3 -0
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tflite_runtime.interpreter as tflite
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import os
|
| 6 |
+
from huggingface_hub import InferenceClient
|
| 7 |
+
|
| 8 |
+
# 1. Load the TFLite Model
|
| 9 |
+
interpreter = tflite.Interpreter(model_path="model.tflite")
|
| 10 |
+
interpreter.allocate_tensors()
|
| 11 |
+
input_details = interpreter.get_input_details()
|
| 12 |
+
output_details = interpreter.get_output_details()
|
| 13 |
+
|
| 14 |
+
# 2. Load Labels
|
| 15 |
+
with open("labels.txt", "r") as f:
|
| 16 |
+
labels = [line.strip() for line in f.readlines()]
|
| 17 |
+
|
| 18 |
+
# 3. Setup AI Assistant Client (Mistral is great for medical info)
|
| 19 |
+
# Note: To use this, add your HF_TOKEN to the Space "Secrets" in Settings
|
| 20 |
+
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3", token=os.getenv("HF_TOKEN"))
|
| 21 |
+
|
| 22 |
+
def predict_and_advise(image):
|
| 23 |
+
# --- PART A: SKIN DETECTION ---
|
| 24 |
+
# Preprocess image (resizing to match your model's input size)
|
| 25 |
+
input_shape = input_details[0]['shape']
|
| 26 |
+
img = Image.fromarray(image).resize((input_shape[1], input_shape[2]))
|
| 27 |
+
input_data = np.expand_dims(np.array(img, dtype=np.float32), axis=0)
|
| 28 |
+
|
| 29 |
+
# Run inference
|
| 30 |
+
interpreter.set_tensor(input_details[0]['index'], input_data)
|
| 31 |
+
interpreter.invoke()
|
| 32 |
+
output_data = interpreter.get_tensor(output_details[0]['index'])[0]
|
| 33 |
+
|
| 34 |
+
# Get top prediction
|
| 35 |
+
top_index = np.argmax(output_data)
|
| 36 |
+
disease_name = labels[top_index]
|
| 37 |
+
confidence = float(output_data[top_index])
|
| 38 |
+
|
| 39 |
+
# --- PART B: MEDICAL AI ASSISTANT ---
|
| 40 |
+
prompt = f"A skin analysis AI has detected {disease_name}. Briefly explain what this is and provide 3 general care tips. End by saying: 'This is not a medical diagnosis; please see a dermatologist.'"
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
advice = client.text_generation(prompt, max_new_tokens=250)
|
| 44 |
+
except:
|
| 45 |
+
advice = "Could not fetch advice. Please consult a dermatologist."
|
| 46 |
+
|
| 47 |
+
return {
|
| 48 |
+
"condition": disease_name,
|
| 49 |
+
"confidence": f"{confidence*100:.2f}%",
|
| 50 |
+
"assistant_advice": advice
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# 4. Create the API Interface
|
| 54 |
+
demo = gr.Interface(
|
| 55 |
+
fn=predict_and_advise,
|
| 56 |
+
inputs=gr.Image(),
|
| 57 |
+
outputs=gr.JSON(),
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
demo.launch()
|
labels.txt
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
acne
|
| 2 |
+
actinic_keratosis
|
| 3 |
+
atopic_dermatitis
|
| 4 |
+
basal_cell_carcinoma
|
| 5 |
+
dermatofibroma
|
| 6 |
+
eczema
|
| 7 |
+
exanthems
|
| 8 |
+
hair_loss
|
| 9 |
+
herpes
|
| 10 |
+
light_diseases
|
| 11 |
+
lupus
|
| 12 |
+
melanocytic_nevi
|
| 13 |
+
melanoma
|
| 14 |
+
nail_fungus
|
| 15 |
+
poison_ivy
|
| 16 |
+
psoriasis
|
| 17 |
+
scabies
|
| 18 |
+
seborrheic_keratosis
|
| 19 |
+
systemic_disease
|
| 20 |
+
tinea
|
| 21 |
+
urticaria
|
| 22 |
+
vascular_tumors
|
| 23 |
+
vasculitis
|
| 24 |
+
warts
|
requirements.txt.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tflite-runtime
|
| 2 |
+
numpy
|
| 3 |
+
Pillow
|
| 4 |
+
huggingface_hub
|
skin_model.tflite
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bfa3b931863e36821fc12e6c589febd1b1f9f5f451e57660054743442c499e37
|
| 3 |
+
size 2839400
|