Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,10 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
import requests
|
| 4 |
-
|
| 5 |
import os
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Load environment variables
|
| 8 |
-
load_dotenv()
|
| 9 |
-
|
| 10 |
NOTION_TOKEN = os.getenv('NOTION_TOKEN')
|
| 11 |
DATABASE_ID = os.getenv('DATABASE_ID')
|
| 12 |
|
|
@@ -22,38 +20,84 @@ def get_notion_entries():
|
|
| 22 |
response.raise_for_status() # Raise an exception for HTTP errors
|
| 23 |
return response.json()
|
| 24 |
|
| 25 |
-
def
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# Initialize the image classification pipeline
|
| 45 |
classifier = pipeline("image-classification", model="ahishamm/vit-base-HAM-10000-patch-32")
|
| 46 |
|
| 47 |
def classify_image(image):
|
| 48 |
results = classifier(image)
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# Create the Gradio interface
|
| 53 |
iface = gr.Interface(
|
| 54 |
fn=classify_image,
|
| 55 |
inputs=gr.Image(type="pil"),
|
| 56 |
-
outputs=gr.
|
| 57 |
title="Skin Condition Classifier",
|
| 58 |
description="Upload an image to classify the skin condition and get enriched data from Notion."
|
| 59 |
)
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
+
import pandas as pd
|
| 3 |
import os
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from transformers import pipeline
|
| 6 |
|
| 7 |
# Load environment variables
|
|
|
|
|
|
|
| 8 |
NOTION_TOKEN = os.getenv('NOTION_TOKEN')
|
| 9 |
DATABASE_ID = os.getenv('DATABASE_ID')
|
| 10 |
|
|
|
|
| 20 |
response.raise_for_status() # Raise an exception for HTTP errors
|
| 21 |
return response.json()
|
| 22 |
|
| 23 |
+
def get_full_text(property):
|
| 24 |
+
if property and 'rich_text' in property and property['rich_text']:
|
| 25 |
+
return ''.join([text_part['text']['content'] for text_part in property['rich_text']])
|
| 26 |
+
elif property and 'title' in property and property['title']:
|
| 27 |
+
return ''.join([text_part['text']['content'] for text_part in property['title']])
|
| 28 |
+
return ""
|
| 29 |
+
|
| 30 |
+
def notion_to_dataframe(notion_data):
|
| 31 |
+
# Prepare lists to create DataFrame
|
| 32 |
+
condition_names = []
|
| 33 |
+
condition_full_names = []
|
| 34 |
+
villains = []
|
| 35 |
+
heroes = []
|
| 36 |
+
hero_images = []
|
| 37 |
+
product_urls = []
|
| 38 |
+
|
| 39 |
+
# Iterate through Notion entries and collect data
|
| 40 |
+
for entry in notion_data['results']:
|
| 41 |
+
condition_names.append(get_full_text(entry['properties']['condition_name']))
|
| 42 |
+
condition_full_names.append(get_full_text(entry['properties']['condition_full_name']))
|
| 43 |
+
villains.append(get_full_text(entry['properties']['villain']))
|
| 44 |
+
heroes.append(get_full_text(entry['properties']['hero']))
|
| 45 |
+
hero_images.append(entry['properties']['hero_image']['url'] if entry['properties']['hero_image'] else None)
|
| 46 |
+
product_urls.append(entry['properties']['product_url']['url'] if entry['properties']['product_url'] else None)
|
| 47 |
+
|
| 48 |
+
# Create DataFrame
|
| 49 |
+
df = pd.DataFrame({
|
| 50 |
+
'Condition Name': condition_names,
|
| 51 |
+
'Condition Full Name': condition_full_names,
|
| 52 |
+
'Villain': villains,
|
| 53 |
+
'Hero': heroes,
|
| 54 |
+
'Hero Image': hero_images,
|
| 55 |
+
'Product URL': product_urls
|
| 56 |
+
})
|
| 57 |
+
|
| 58 |
+
return df
|
| 59 |
+
|
| 60 |
+
# Fetch data
|
| 61 |
+
notion_data = get_notion_entries()
|
| 62 |
+
|
| 63 |
+
# Convert to DataFrame
|
| 64 |
+
df = notion_to_dataframe(notion_data)
|
| 65 |
|
| 66 |
# Initialize the image classification pipeline
|
| 67 |
classifier = pipeline("image-classification", model="ahishamm/vit-base-HAM-10000-patch-32")
|
| 68 |
|
| 69 |
def classify_image(image):
|
| 70 |
results = classifier(image)
|
| 71 |
+
condition_name = results[0]['label']
|
| 72 |
+
condition_data = df[df['Condition Name'] == condition_name].iloc[0]
|
| 73 |
+
|
| 74 |
+
classification = results[0]
|
| 75 |
+
confidence = round(classification['score'] * 100, 2)
|
| 76 |
+
condition_full_name = condition_data['Condition Full Name']
|
| 77 |
+
villain = condition_data['Villain']
|
| 78 |
+
hero = condition_data['Hero']
|
| 79 |
+
hero_image = condition_data['Hero Image']
|
| 80 |
+
product_url = condition_data['Product URL']
|
| 81 |
+
|
| 82 |
+
enriched_output = f"""
|
| 83 |
+
**{condition_full_name} ({confidence}% confident)**
|
| 84 |
+
|
| 85 |
+
{villain}
|
| 86 |
+
|
| 87 |
+
{hero} Find out if he is also your hero!
|
| 88 |
+
|
| 89 |
+

|
| 90 |
+
|
| 91 |
+
[Learn More]({product_url})
|
| 92 |
+
"""
|
| 93 |
+
|
| 94 |
+
return enriched_output
|
| 95 |
|
| 96 |
# Create the Gradio interface
|
| 97 |
iface = gr.Interface(
|
| 98 |
fn=classify_image,
|
| 99 |
inputs=gr.Image(type="pil"),
|
| 100 |
+
outputs=gr.Markdown(),
|
| 101 |
title="Skin Condition Classifier",
|
| 102 |
description="Upload an image to classify the skin condition and get enriched data from Notion."
|
| 103 |
)
|