Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import requests
|
| 4 |
+
from dotenv import load_dotenv
|
| 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 |
+
|
| 13 |
+
headers = {
|
| 14 |
+
"Authorization": f"Bearer {NOTION_TOKEN}",
|
| 15 |
+
"Content-Type": "application/json",
|
| 16 |
+
"Notion-Version": "2022-06-28"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
def get_notion_entries():
|
| 20 |
+
url = f"https://api.notion.com/v1/databases/{DATABASE_ID}/query"
|
| 21 |
+
response = requests.post(url, headers=headers)
|
| 22 |
+
response.raise_for_status() # Raise an exception for HTTP errors
|
| 23 |
+
return response.json()
|
| 24 |
+
|
| 25 |
+
def enrich_classification_with_notion(classification_result):
|
| 26 |
+
notion_data = get_notion_entries()
|
| 27 |
+
enriched_results = []
|
| 28 |
+
for result in classification_result:
|
| 29 |
+
condition_name = result['label']
|
| 30 |
+
for entry in notion_data['results']:
|
| 31 |
+
if entry['properties']['condition_name']['title'][0]['text']['content'] == condition_name:
|
| 32 |
+
enriched_result = {
|
| 33 |
+
"label": condition_name,
|
| 34 |
+
"score": result['score'],
|
| 35 |
+
"condition_full_name": entry['properties']['condition_full_name']['rich_text'][0]['text']['content'],
|
| 36 |
+
"villain": entry['properties']['villain']['rich_text'][0]['text']['content'],
|
| 37 |
+
"hero": entry['properties']['hero']['rich_text'][0]['text']['content'],
|
| 38 |
+
"hero_image": entry['properties']['hero_image']['url'],
|
| 39 |
+
"product_url": entry['properties']['product_url']['url']
|
| 40 |
+
}
|
| 41 |
+
enriched_results.append(enriched_result)
|
| 42 |
+
return enriched_results
|
| 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 |
+
enriched_results = enrich_classification_with_notion(results)
|
| 50 |
+
return enriched_results
|
| 51 |
+
|
| 52 |
+
# Create the Gradio interface
|
| 53 |
+
iface = gr.Interface(
|
| 54 |
+
fn=classify_image,
|
| 55 |
+
inputs=gr.Image(type="pil"),
|
| 56 |
+
outputs=gr.JSON(),
|
| 57 |
+
title="Skin Condition Classifier",
|
| 58 |
+
description="Upload an image to classify the skin condition and get enriched data from Notion."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
iface.launch()
|