amasood commited on
Commit
df0f983
·
verified ·
1 Parent(s): c85047c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import streamlit as st
4
+ from PIL import Image
5
+ from transformers import AutoModelForImageClassification, AutoImageProcessor
6
+ from groq import Groq
7
+
8
+ # Set page config
9
+ st.set_page_config(page_title="DermaBot - AI Skin Disease Detector", page_icon="🩺", layout="wide")
10
+
11
+ # Load model and processor
12
+ MODEL_NAME = "Jayanth2002/dinov2-base-finetuned-SkinDisease"
13
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
14
+
15
+ model = AutoModelForImageClassification.from_pretrained(MODEL_NAME).to(DEVICE)
16
+ processor = AutoImageProcessor.from_pretrained(MODEL_NAME)
17
+
18
+ # Set up the Groq API key (replace with your actual key or use an environment variable)
19
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY", "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")
20
+ client = Groq(api_key=GROQ_API_KEY)
21
+
22
+ # Function to predict skin disease
23
+ def predict_skin_disease(image):
24
+ image = image.convert("RGB")
25
+ inputs = processor(images=image, return_tensors="pt").to(DEVICE)
26
+
27
+ with torch.no_grad():
28
+ outputs = model(**inputs)
29
+
30
+ logits = outputs.logits
31
+ predicted_class_idx = logits.argmax(-1).item()
32
+ predicted_label = model.config.id2label[predicted_class_idx]
33
+
34
+ return predicted_label
35
+
36
+ # Function to get disease details from Groq API
37
+ def get_disease_info(disease_name):
38
+ prompt = f"Provide a detailed explanation about the skin disease '{disease_name}', including description of disease, causes, precausions, risk and treatment options."
39
+
40
+ chat_completion = client.chat.completions.create(
41
+ messages=[{"role": "user", "content": prompt}],
42
+ model="llama-3.3-70b-versatile",
43
+ )
44
+
45
+ return chat_completion.choices[0].message.content
46
+
47
+ # Function to handle chatbot queries
48
+ def chatbot_response(disease_name, user_query):
49
+ prompt = f"The detected skin disease is '{disease_name}'. {user_query}"
50
+
51
+ chat_completion = client.chat.completions.create(
52
+ messages=[{"role": "user", "content": prompt}],
53
+ model="llama-3.3-70b-versatile",
54
+ )
55
+
56
+ return chat_completion.choices[0].message.content
57
+
58
+ # Streamlit UI
59
+ st.image("https://huggingface.co/spaces/your-huggingface-space/logo.png", width=200)
60
+ st.title("🩺 DermaBot - AI Skin Disease Detector")
61
+ st.write("Upload an image of a skin condition to get a diagnosis and ask questions about it.")
62
+
63
+ # Upload image section
64
+ uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
65
+
66
+ if uploaded_image:
67
+ image = Image.open(uploaded_image)
68
+ st.image(image, caption="Uploaded Image", use_column_width=True)
69
+
70
+ if st.button("Detect Disease"):
71
+ with st.spinner("Analyzing..."):
72
+ disease_name = predict_skin_disease(image)
73
+ disease_info = get_disease_info(disease_name)
74
+ st.success(f"**Detected Disease:** {disease_name}")
75
+ st.write(f"**Details:** {disease_info}")
76
+
77
+ # Chatbot section
78
+ st.subheader("💬 Ask DermaBot")
79
+ user_query = st.text_input("Ask about the detected disease:")
80
+
81
+ if st.button("Ask"):
82
+ if uploaded_image:
83
+ with st.spinner("Thinking..."):
84
+ response = chatbot_response(disease_name, user_query)
85
+ st.write(response)
86
+ else:
87
+ st.warning("Please upload an image first.")
88
+
89
+ st.markdown("---")
90
+ st.write("🔍 Powered by **AI & Groq API** | © 2025 DermaBot")
91
+