Spaces:
Sleeping
Sleeping
File size: 12,541 Bytes
e03d094 a670d2e e03d094 a182687 e03d094 e09e1b2 e03d094 c94124b e03d094 13f6011 e03d094 a670d2e e03d094 fe0983f b08c22b c1f11b3 e03d094 a670d2e e03d094 c052bcc e03d094 c052bcc e03d094 c052bcc e03d094 c052bcc e03d094 a670d2e e03d094 a182687 039752f 2b9e0b0 039752f a182687 e03d094 039752f 2b9e0b0 e03d094 a182687 7a5b0e6 e03d094 c1f11b3 039752f e03d094 c1f11b3 e03d094 f92ed4e fa4bda6 e03d094 fa4bda6 e03d094 fa4bda6 e03d094 fa4bda6 e03d094 fa4bda6 e03d094 a670d2e e03d094 613658a e03d094 a670d2e e03d094 a670d2e e03d094 a670d2e e03d094 a670d2e e03d094 6e6ac11 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
import streamlit as st
from PIL import Image
import torch
import cohere
import torch.nn as nn
from torchvision import transforms
from torchvision.models import vit_b_16, vit_l_16, ViT_B_16_Weights, ViT_L_16_Weights
import pandas as pd
from huggingface_hub import hf_hub_download
from langchain_huggingface import HuggingFaceEmbeddings
import io
import os
import base64
from fpdf import FPDF
from sqlalchemy import create_engine
from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate
from qdrant_client import QdrantClient
from qdrant_client.http.models import Distance, VectorParams
from sentence_transformers import SentenceTransformer
#from langchain_community.vectorstores.pgvector import PGVector
#from langchain_postgres import PGVector
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_community.vectorstores import Qdrant
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.embeddings import SentenceTransformerEmbeddings
torch.cuda.empty_cache()
import nest_asyncio
nest_asyncio.apply()
co = cohere.Client(st.secrets["COHERE_API_KEY"])
st.set_page_config(page_title="DermBOT", page_icon="๐งฌ", layout="centered")
# === Model Selection ===
available_models = ["OpenAI GPT-4o", "LLaMA 3", "Gemini Pro"]
st.session_state["selected_model"] = st.sidebar.selectbox("Select LLM Model", available_models)
# === Qdrant DB Setup ===
qdrant_client = QdrantClient(
url="https://2715ddd8-647f-40ee-bca4-9027d193e8aa.us-east-1-0.aws.cloud.qdrant.io",
api_key="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2Nlc3MiOiJtIn0.HXzezXdWMFeeR16F7zvqgjzsqrcm8hqa-StXdToFP9Q"
)
collection_name = "ks_collection_1.5BE"
#embedding_model = SentenceTransformer("D:\DR\RAG\gte-Qwen2-1.5B-instruct", trust_remote_code=True)
#embedding_model.max_seq_length = 8192
#local_embedding = SentenceTransformerEmbeddings(model=embedding_model)
device = "cuda" if torch.cuda.is_available() else "cpu"
def get_safe_embedding_model():
model_name = "Alibaba-NLP/gte-Qwen2-1.5B-instruct"
try:
print("Trying to load embedding model on CUDA...")
embedding = HuggingFaceEmbeddings(
model_name=model_name,
model_kwargs={
"trust_remote_code": True,
"device": "cuda"
}
)
print("Loaded embedding model on GPU.")
return embedding
except RuntimeError as e:
if "CUDA out of memory" in str(e):
print("CUDA OOM. Falling back to CPU.")
else:
print(" Error loading model on CUDA:", str(e))
print("Loading embedding model on CPU...")
return HuggingFaceEmbeddings(
model_name=model_name,
model_kwargs={
"trust_remote_code": True,
"device": "cpu"
}
)
# Replace your old local_embedding line with this
local_embedding = get_safe_embedding_model()
print(" Qwen2-1.5B local embedding model loaded.")
vector_store = Qdrant(
client=qdrant_client,
collection_name=collection_name,
embeddings=local_embedding
)
retriever = vector_store.as_retriever()
# Dynamically initialize LLM based on selection
OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
selected_model = st.session_state["selected_model"]
if "OpenAI" in selected_model:
llm = ChatOpenAI(model="gpt-4o", temperature=0.2, api_key=OPENAI_API_KEY)
elif "LLaMA" in selected_model:
st.warning("LLaMA integration is not implemented yet.")
st.stop()
elif "Gemini" in selected_model:
st.warning("Gemini integration is not implemented yet.")
st.stop()
else:
st.error("Unsupported model selected.")
st.stop()
#retriever = vector_store.as_retriever()
AI_PROMPT_TEMPLATE = """
You are DermBOT, a compassionate and knowledgeable AI Dermatology Assistant designed to educate users about skin-related health concerns with clarity, empathy, and precision.
Your goal is to respond like a well-informed human expertโbalancing professionalism with warmth and reassurance.
When crafting responses:
- Begin with a clear, engaging summary of the condition or concern.
- Use short paragraphs for readability.
- Include bullet points or numbered lists where appropriate.
- Avoid overly technical terms unless explained simply.
- End with a helpful next step, such as lifestyle advice or when to see a doctor.
๐ฉบ Response Structure:
1. **Overview** โ Briefly introduce the condition or concern.
2. **Common Symptoms** โ Describe noticeable signs in simple terms.
3. **Causes & Risk Factors** โ Include genetic, lifestyle, and environmental aspects.
4. **Treatment Options** โ Outline common OTC and prescription treatments.
5. **When to Seek Help** โ Warn about symptoms that require urgent care.
Always encourage consulting a licensed dermatologist for personal diagnosis and treatment. For any breathing difficulties, serious infections, or rapid symptom worsening, advise calling emergency services immediately.
---
Query: {question}
Relevant Context: {context}
Your Response:
"""
prompt_template = PromptTemplate(template=AI_PROMPT_TEMPLATE, input_variables=["question", "context"])
#rag_chain = RetrievalQA.from_chain_type(
# llm=llm,
# retriever=retriever,
# chain_type="stuff",
# chain_type_kwargs={"prompt": prompt_template, "document_variable_name": "context"}
#)
# === Class Names ===
multilabel_class_names = [
"Vesicle", "Papule", "Macule", "Plaque", "Abscess", "Pustule", "Bulla", "Patch",
"Nodule", "Ulcer", "Crust", "Erosion", "Excoriation", "Atrophy", "Exudate", "Purpura/Petechiae",
"Fissure", "Induration", "Xerosis", "Telangiectasia", "Scale", "Scar", "Friable", "Sclerosis",
"Pedunculated", "Exophytic/Fungating", "Warty/Papillomatous", "Dome-shaped", "Flat topped",
"Brown(Hyperpigmentation)", "Translucent", "White(Hypopigmentation)", "Purple", "Yellow",
"Black", "Erythema", "Comedo", "Lichenification", "Blue", "Umbilicated", "Poikiloderma",
"Salmon", "Wheal", "Acuminate", "Burrow", "Gray", "Pigmented", "Cyst"
]
multiclass_class_names = [
"systemic", "hair", "drug_reactions", "uriticaria", "acne", "light",
"autoimmune", "papulosquamous", "eczema", "skincancer",
"benign_tumors", "bacteria_parasetic_infections", "fungal_infections", "viral_skin_infections"
]
# === Load Models ===
class SkinViT(nn.Module):
def __init__(self, num_classes):
super(SkinViT, self).__init__()
self.model = vit_b_16(weights=ViT_B_16_Weights.DEFAULT)
in_features = self.model.heads.head.in_features
self.model.heads.head = nn.Linear(in_features, num_classes)
def forward(self, x):
return self.model(x)
class DermNetViT(nn.Module):
def __init__(self, num_classes):
super(DermNetViT, self).__init__()
self.model = vit_l_16(weights=ViT_L_16_Weights.DEFAULT)
in_features = self.model.heads[0].in_features
self.model.heads[0] = nn.Sequential(
nn.Dropout(0.3),
nn.Linear(in_features, num_classes)
)
def forward(self, x):
return self.model(x)
#multilabel_model = torch.load("D:/DR/RAG/BestModels2703/skin_vit_fold10.pth", map_location='cpu')
#multiclass_model = torch.load("D:/DR/RAG/BestModels2703/best_dermnet_vit.pth", map_location='cpu')
# === Load Model State Dicts ===
multilabel_model_path = hf_hub_download(repo_id="santhoshraghu/DermBOT", filename="skin_vit_fold10_sd.pth")
multiclass_model_path = hf_hub_download(repo_id="santhoshraghu/DermBOT", filename="best_dermnet_vit_sd.pth")
def load_model_with_fallback(model_class, weight_path, num_classes, model_name):
try:
print(f"๐ Loading {model_name} on GPU...")
model = model_class(num_classes)
model.load_state_dict(torch.load(weight_path, map_location="cuda"))
model.to("cuda")
print(f"โ
{model_name} loaded on GPU.")
return model
except RuntimeError as e:
if "CUDA out of memory" in str(e):
print(f"โ ๏ธ {model_name} OOM. Falling back to CPU.")
else:
print(f"โ Error loading {model_name} on CUDA: {e}")
print(f"๐ Loading {model_name} on CPU...")
model = model_class(num_classes)
model.load_state_dict(torch.load(weight_path, map_location="cpu"))
model.to("cpu")
return model
# Load both models with fallback
multilabel_model = load_model_with_fallback(SkinViT, multilabel_model_path, len(multilabel_class_names), "SkinViT")
multiclass_model = load_model_with_fallback(DermNetViT, multiclass_model_path, len(multiclass_class_names), "DermNetViT")
multilabel_model.eval()
multiclass_model.eval()
# === Session Init ===
if "messages" not in st.session_state:
st.session_state.messages = []
# === Image Processing Function ===
def run_inference(image):
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize([0.5], [0.5])
])
input_tensor = transform(image).unsqueeze(0)
# Automatically match model device (GPU or CPU)
model_device = next(multilabel_model.parameters()).device
input_tensor = input_tensor.to(model_device)
with torch.no_grad():
probs_multi = torch.sigmoid(multilabel_model(input_tensor)).squeeze().cpu().numpy()
pred_idx = torch.argmax(multiclass_model(input_tensor), dim=1).item()
predicted_multi = [multilabel_class_names[i] for i, p in enumerate(probs_multi) if p > 0.5]
predicted_single = multiclass_class_names[pred_idx]
return predicted_multi, predicted_single
# === PDF Export ===
def export_chat_to_pdf(messages):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
for msg in messages:
role = "You" if msg["role"] == "user" else "AI"
pdf.multi_cell(0, 10, f"{role}: {msg['content']}\n")
buf = io.BytesIO()
pdf.output(buf)
buf.seek(0)
return buf
#Reranker utility
def rerank_with_cohere(query: str, documents: list, top_n: int = 5) -> list:
if not documents:
return []
raw_texts = [doc.page_content if hasattr(doc, "page_content") else str(doc) for doc in documents]
results = co.rerank(query=query, documents=raw_texts, top_n=min(top_n, len(raw_texts)), model="rerank-v3.5")
reranked_docs = [documents[result.index] for result in results]
return reranked_docs
# Final answer generation using reranked context
def get_reranked_response(query: str):
docs = retriever.get_relevant_documents(query)
reranked_docs = rerank_with_cohere(query, docs)
context = "\n\n".join([doc.page_content for doc in reranked_docs])
prompt = AI_PROMPT_TEMPLATE.format(question=query, context=context)
return llm.invoke([{"role": "system", "content": prompt}])
# === App UI ===
st.title("๐งฌ DermBOT โ Skin AI Assistant")
st.caption(f"๐ง Using model: {selected_model}")
uploaded_file = st.file_uploader("Upload a skin image", type=["jpg", "jpeg", "png"])
if uploaded_file:
st.image(uploaded_file, caption="Uploaded image", use_container_width=True)
image = Image.open(uploaded_file).convert("RGB")
predicted_multi, predicted_single = run_inference(image)
# Show predictions clearly to the user
st.markdown(f"๐งพ **Skin Issues**: {', '.join(predicted_multi)}")
st.markdown(f"๐ **Most Likely Diagnosis**: {predicted_single}")
query = f"What are my treatment options for {predicted_multi} and {predicted_single}?"
st.session_state.messages.append({"role": "user", "content": query})
with st.spinner("๐ Analyzing and retrieving context..."):
response = get_reranked_response(query)
st.session_state.messages.append({"role": "assistant", "content": response.content})
with st.chat_message("assistant"):
st.markdown(response.content)
# === Chat Interface ===
if prompt := st.chat_input("Ask a follow-up..."):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
response = get_reranked_response(prompt)
st.session_state.messages.append({"role": "assistant", "content": response.content})
with st.chat_message("assistant"):
st.markdown(response.content)
# === PDF Button ===
if st.button("๐ Download Chat as PDF"):
pdf_file = export_chat_to_pdf(st.session_state.messages)
st.download_button("Download PDF", data=pdf_file, file_name="chat_history.pdf", mime="application/pdf") |