Skincare-by-Dr-Aisha-Ghias commited on
Commit
4c9038c
1 Parent(s): 3a4bb32

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import openai
4
+ import os
5
+ # Initialize API keys and headers
6
+ api_key = os.getenv("api_key")
7
+ openai.api_key = os.getenv("OPENAI_API_KEY")
8
+ bearer = "Bearer " + api_key
9
+ API_URL = "https://api-inference.huggingface.co/models/Waqasjan123/Skin_Cancer_Detector_Live"
10
+ HEADERS = {"Authorization": bearer}
11
+
12
+ # Chat history
13
+ stringi = ""
14
+
15
+ # Initialize session state if it doesn't exist
16
+ if "messages" not in st.session_state:
17
+ st.session_state["messages"] = [{"role": "assistant", "content": "How can I assist you in dermatology?"}]
18
+
19
+ # Define the model query function with error handling for HuggingFace
20
+ def query_model(image_data):
21
+ try:
22
+ response = requests.post(API_URL, headers=HEADERS, data=image_data)
23
+ response.raise_for_status()
24
+ return response.json()
25
+ except requests.RequestException as e:
26
+ st.error(f"Error querying the model: Setting Up The Server Please Try Again in 10 Seconds")
27
+ return None
28
+
29
+ # Main
30
+ st.title("Skin Check Pro")
31
+ st.write("---")
32
+
33
+ # Upload Image
34
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "webp"])
35
+
36
+ if uploaded_file:
37
+ image_data = uploaded_file.read()
38
+ results = query_model(image_data)
39
+
40
+ # Display image and results on the sidebar
41
+ with st.sidebar:
42
+ st.image(image_data, caption='Uploaded Image.', width=250)
43
+ if results:
44
+ for classification in results:
45
+ st.write(f"**{classification['label']}**")
46
+ st.write(f"Score: {classification['score']:.2f}")
47
+ st.progress(classification['score'])
48
+ st.write("---")
49
+
50
+ st.write("## Ai Skin Check Assitant")
51
+ st.write("Have questions? Type your message below.")
52
+
53
+ # Display all previous messages
54
+ for msg in st.session_state.messages:
55
+ st.chat_message(msg["role"]).write(msg["content"])
56
+
57
+ # User input
58
+ user_input = st.chat_input("Type your message here...")
59
+
60
+ if user_input:
61
+ # Display only the user's message in the chat interface
62
+ st.session_state.messages.append({"role": "user", "content": user_input})
63
+ st.chat_message("user").write(user_input)
64
+
65
+ # Prepare complete message to send to OpenAI
66
+ complete_message = f"This is what we discussed before: {stringi}. The result for the uploaded image is: {results}. This is the new question: {user_input}. [IF SOMEONE IS SPECIFICALLY ASKING ABOUT THIS PROJECT OR CANCER DETECTION AI ONLY THEN MENTION SOMETHING LIKE THIS, Dr. Aisha Zargham led a team of experts, combining her rich medical knowledge with advances in artificial intelligence to create this game-changing skin cancer detection system.] [YOU ARE NOT TALKING WITH PATIENTS, YOU ARE TALKING WITH DOCOTRS WHO ARE DIAGNOSING PATIENTS] [REMEMER YOUR AUDIENCE IS DERMATOLOGIST, MEDICAL STUDENTS, DOCTORS, PLEASE ENGAGE AND HELP THEM IN RESEARCHING ON IMAGE RESULTS] [KEEP YOUR ANSWER SHORTER] [IN THE DISEASES FOCUS MAINLY THE DISEASE WITH THE HIGHEST SCORE IN RESULTS]"
67
+ conversation_messages = [{"role": "system", "content": "You are a expert dermatologist on a website where we analyze images through Ai for Skin Cancer."},
68
+ {"role": "user", "content": complete_message}]
69
+
70
+ # Generate assistant's response
71
+ completion = openai.ChatCompletion.create(
72
+ model="gpt-3.5-turbo",
73
+ messages=conversation_messages
74
+ )
75
+ assistant_response = completion.choices[0].message
76
+ # Update Chat History string
77
+ stringi += assistant_response['content']
78
+
79
+ # Append assistant's message to conversation and display it
80
+ st.session_state.messages.append(assistant_response)
81
+ st.chat_message("assistant", avatar="LOGO.png").write(assistant_response['content'])
82
+
83
+ else:
84
+ st.warning("Please upload an image to get started.")
85
+
86
+ # Disclaimer
87
+ st.write("---")
88
+ st.write("**Note on Usage:**")
89
+ st.write("This tool is based on research and is intended for informational purposes only. For medical advice, consult with a dermatologist.")