Spaces:
Sleeping
Sleeping
try to fix again
Browse files
app.py
CHANGED
|
@@ -1,112 +1,138 @@
|
|
| 1 |
-
import gradio as gr
|
| 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 |
-
cleaned_chunks
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
chunk_embeddings =
|
| 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 |
chatbot.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import asyncio
|
| 3 |
+
import random
|
| 4 |
+
import torch
|
| 5 |
+
from huggingface_hub import InferenceClient
|
| 6 |
+
from sentence_transformers import SentenceTransformer
|
| 7 |
+
|
| 8 |
+
# ---------------------- THEME ----------------------
|
| 9 |
+
theme = gr.themes.Ocean(
|
| 10 |
+
secondary_hue="lime",
|
| 11 |
+
neutral_hue="teal",
|
| 12 |
+
text_size="lg",
|
| 13 |
+
spacing_size="lg",
|
| 14 |
+
).set(
|
| 15 |
+
body_background_fill='*primary_400',
|
| 16 |
+
body_background_fill_dark='*primary_950',
|
| 17 |
+
body_text_color='*primary_50',
|
| 18 |
+
body_text_color_dark='*primary_50',
|
| 19 |
+
background_fill_primary_dark='*secondary_500',
|
| 20 |
+
background_fill_secondary='*primary_700',
|
| 21 |
+
background_fill_secondary_dark='*primary_900',
|
| 22 |
+
button_primary_background_fill='linear-gradient(120deg, *secondary_800 0%, *primary_300 60%, *primary_800 100%)',
|
| 23 |
+
button_primary_background_fill_dark='linear-gradient(120deg, *secondary_400 0%, *primary_400 60%, *primary_600 100%)',
|
| 24 |
+
button_primary_background_fill_hover='linear-gradient(120deg, *secondary_400 0%, *primary_300 60%, *neutral_300 100%)'
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# ---------------------- LOAD KNOWLEDGE BASE ----------------------
|
| 28 |
+
with open("Skin_cancer_harvard.txt", "r", encoding="utf-8") as file:
|
| 29 |
+
Skin_cancer_harvard_text = file.read()
|
| 30 |
+
print(Skin_cancer_harvard_text)
|
| 31 |
+
|
| 32 |
+
def preprocess_text(text):
|
| 33 |
+
cleaned_text = text.strip()
|
| 34 |
+
chunks = cleaned_text.split("\n")
|
| 35 |
+
cleaned_chunks = [chunk.strip() for chunk in chunks if chunk.strip()]
|
| 36 |
+
print(cleaned_chunks)
|
| 37 |
+
print(len(cleaned_chunks))
|
| 38 |
+
return cleaned_chunks
|
| 39 |
+
|
| 40 |
+
cleaned_chunks = preprocess_text(Skin_cancer_harvard_text)
|
| 41 |
+
|
| 42 |
+
# ---------------------- EMBEDDINGS ----------------------
|
| 43 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 44 |
+
|
| 45 |
+
def create_embeddings(text_chunks):
|
| 46 |
+
chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True)
|
| 47 |
+
print(chunk_embeddings.shape)
|
| 48 |
+
return chunk_embeddings
|
| 49 |
+
|
| 50 |
+
chunk_embeddings = create_embeddings(cleaned_chunks)
|
| 51 |
+
|
| 52 |
+
# ---------------------- SEMANTIC SEARCH ----------------------
|
| 53 |
+
def get_top_chunks(query, chunk_embeddings, text_chunks):
|
| 54 |
+
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 55 |
+
query_embedding_normalized = query_embedding / query_embedding.norm()
|
| 56 |
+
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
|
| 57 |
+
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
|
| 58 |
+
top_indices = torch.topk(similarities, k=3).indices
|
| 59 |
+
top_chunks = [cleaned_chunks[i] for i in top_indices]
|
| 60 |
+
return top_chunks
|
| 61 |
+
|
| 62 |
+
# ---------------------- LLM CLIENT ----------------------
|
| 63 |
+
client = InferenceClient("microsoft/phi-4")
|
| 64 |
+
|
| 65 |
+
# ---------------------- CHAT FUNCTION ----------------------
|
| 66 |
+
def respond(message, history):
|
| 67 |
+
info = get_top_chunks(message, chunk_embeddings, cleaned_chunks)
|
| 68 |
+
|
| 69 |
+
messages = [
|
| 70 |
+
{
|
| 71 |
+
'role': 'system',
|
| 72 |
+
'content': (
|
| 73 |
+
f'You are a friendly chatbot using {info} to answer questions. '
|
| 74 |
+
'You are always willing to help and want the best for the user. '
|
| 75 |
+
'You need to emphasize that you are not a medical professional at the end '
|
| 76 |
+
'of the message, but you are here to help to the best of your ability. '
|
| 77 |
+
'Be confident and comforting to the users when helping them. '
|
| 78 |
+
'In your response add suggestions for a couple follow-up questions '
|
| 79 |
+
'to further the conversation with the chatbot.'
|
| 80 |
+
)
|
| 81 |
+
}
|
| 82 |
+
]
|
| 83 |
+
|
| 84 |
+
if history:
|
| 85 |
+
messages.extend(history)
|
| 86 |
+
messages.append({'role': 'user', 'content': message})
|
| 87 |
+
|
| 88 |
+
# Run blocking HF API in background thread (prevents StopIteration error)
|
| 89 |
+
def blocking_call():
|
| 90 |
+
return client.chat_completion(messages, max_tokens=500, top_p=0.8)
|
| 91 |
+
|
| 92 |
+
response = asyncio.run(asyncio.to_thread(blocking_call))
|
| 93 |
+
content = response['choices'][0]['message']['content'].strip()
|
| 94 |
+
history.append((message, content))
|
| 95 |
+
return history, content
|
| 96 |
+
|
| 97 |
+
# ---------------------- GRADIO APP ----------------------
|
| 98 |
+
with gr.Blocks(theme=theme) as chatbot:
|
| 99 |
+
with gr.Row(scale=1):
|
| 100 |
+
gr.Image("Capstone_Banner.png")
|
| 101 |
+
|
| 102 |
+
with gr.Row():
|
| 103 |
+
with gr.Column(scale=1):
|
| 104 |
+
gr.Image("Aloe_the_Turtle.png")
|
| 105 |
+
|
| 106 |
+
with gr.Row():
|
| 107 |
+
gr.Markdown(
|
| 108 |
+
"Click the button below to access the teachable machine, an AI Visual Scanner to detect Skin Cancer. "
|
| 109 |
+
"The main purpose of this teachable machine is to check if you have a cancerous or non-cancerous mole. "
|
| 110 |
+
"Place your mole near your camera and the analysis will be represented below. "
|
| 111 |
+
"Note that these results are not 100% accurate, so be sure to consult a medical professional if you have any concerns."
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
with gr.Row(scale=1):
|
| 115 |
+
gr.Button(
|
| 116 |
+
value="AI Visual Testing Moles for Skin Cancer!",
|
| 117 |
+
link="https://teachablemachine.withgoogle.com/models/onfoEa0p-/"
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
with gr.Column(scale=3):
|
| 121 |
+
gr.ChatInterface(
|
| 122 |
+
fn=respond,
|
| 123 |
+
title="Your Personal Skin Chatbot!",
|
| 124 |
+
description=(
|
| 125 |
+
"Welcome, my name is Aloe the Turtle and I am here to help you address any dermatology-related "
|
| 126 |
+
"questions you may have on topics such as Skin Cancer, Acne, Eczema, and much more. "
|
| 127 |
+
"Just remember, while I have comprehensive knowledge on skin concerns, I am not a medical professional!"
|
| 128 |
+
),
|
| 129 |
+
type="messages",
|
| 130 |
+
theme=theme,
|
| 131 |
+
examples=[
|
| 132 |
+
"What ingredients should I use to clear my Acne?",
|
| 133 |
+
"What can I do to proactively prevent Skin Cancer?",
|
| 134 |
+
"How do I tell the difference between eczema and psoriasis?"
|
| 135 |
+
]
|
| 136 |
+
)
|
| 137 |
|
| 138 |
chatbot.launch()
|