Spaces:
Running
Running
import os | |
import requests | |
import gradio as gr | |
from PIL import Image | |
import base64 | |
# API Keys (set these in your environment or replace with your keys) | |
nvidia_api_key = os.getenv("Vision") # NVIDIA API Key | |
imagebb_api_key = os.getenv("Img") # Imgbb API Key | |
# NVIDIA API Endpoint | |
invoke_url = "https://ai.api.nvidia.com/v1/gr/meta/llama-3.2-90b-vision-instruct/chat/completions" | |
def upload_image_to_imgbb(image_path): | |
"""Uploads an image to ImgBB and returns the URL.""" | |
url = f"https://api.imgbb.com/1/upload?key={imagebb_api_key}" | |
with open(image_path, "rb") as image_file: | |
response = requests.post(url, files={"image": image_file}) | |
if response.status_code == 200: | |
return response.json()["data"]["url"] | |
else: | |
raise ValueError(f"Image upload failed: {response.json()}") | |
def identify_skin_condition(image, instruction=("You are a dermatologist with over 20 years of experience trained to diagnose skin infections. " | |
"Analyze the image and diagnose the skin. Write a sentence at the end to tell the person to consult or take their animals to their doctor if the skin infection is serious/chronic.")): | |
""" | |
Identifies the skin condition in the uploaded image using NVIDIA’s Llama 3.2 Vision Instruct model. | |
This function yields progress messages as it executes. | |
""" | |
# Yield progress update | |
yield "Saving image locally..." | |
image_path = "uploaded_skin_image.jpeg" | |
image.save(image_path) | |
yield "Uploading image to ImgBB..." | |
image_url = upload_image_to_imgbb(image_path) | |
yield "Sending image to NVIDIA API..." | |
headers = { | |
"Authorization": f"Bearer {nvidia_api_key}", | |
"Accept": "application/json" | |
} | |
payload = { | |
"model": "meta/llama-3.2-90b-vision-instruct", | |
"messages": [ | |
{ | |
"role": "user", | |
"content": [ | |
{"type": "text", "text": instruction}, | |
{"type": "image_url", "image_url": {"url": image_url}} | |
] | |
} | |
], | |
"max_tokens": 7200, | |
"temperature": 0.15, | |
"top_p": 0.25 | |
} | |
response = requests.post(invoke_url, headers=headers, json=payload) | |
yield "Processing response..." | |
response_data = response.json() | |
if "choices" in response_data: | |
result = response_data["choices"][0]["message"]["content"] | |
else: | |
result = f"Error in response: {response_data}" | |
yield result | |
# Gradio Interface (using live=True to stream progress updates) | |
iface = gr.Interface( | |
fn=identify_skin_condition, | |
inputs=gr.Image(type="pil", label="Upload Skin Image"), | |
outputs=gr.Markdown(label="Diagnosis and Recommendations"), | |
title="SkinAid: Skin Infection Identifier App", | |
description="Upload an image of your skin infection, and the app will identify the most likely condition and provide recommendations. Powered by AI", | |
live=False, | |
) | |
iface.launch() |