Spaces:
Build error
Build error
Rivalcoder commited on
Commit ·
91f9d2d
1
Parent(s): a85ce4c
Edit
Browse files123456789101112131415161718197 unmodified lines27282930313233341 unmodified line363738394041427 unmodified lines505152535455import torchfrom fastapi import FastAPI, File, UploadFilefrom fastapi.responses import JSONResponsefrom transformers import ConvNextForImageClassification, AutoImageProcessorfrom PIL import Imageimport io# Class names (for skin diseases)class_names = ['Acne and Rosacea Photos', 'Actinic Keratosis Basal Cell Carcinoma and other Malignant Lesions', 'Atopic Dermatitis Photos','Bullous Disease Photos', 'Cellulitis Impetigo and other Bacterial Infections', 'Eczema Photos', 'Exanthems and Drug Eruptions','Hair Loss Photos Alopecia and other Hair Diseases', 'Herpes HPV and other STDs Photos', 'Light Diseases and Disorders of Pigmentation','Lupus and other Connective Tissue diseases', 'Melanoma Skin Cancer Nevi and Moles', 'Nail Fungus and other Nail Disease','Poison Ivy Photos and other Contact Dermatitis', 'Psoriasis pictures Lichen Planus and related diseases','Scabies Lyme Disease and other Infestations and Bites', 'Seborrheic Keratoses and other Benign Tumors', 'Systemic Disease','Tinea Ringworm Candidiasis and other Fungal Infections', 'Urticaria Hives', 'Vascular Tumors', 'Vasculitis Photos','Warts Molluscum and other Viral Infections']7 unmodified lines# FastAPI appapp = FastAPI()# Prediction helperdef predict(image: Image.Image):inputs = processor(images=image, return_tensors="pt")with torch.no_grad():1 unmodified linepredicted_class = torch.argmax(outputs.logits, dim=1).item()return predicted_class, class_names[predicted_class]# Endpoint: /predict@app.post("/predict/")async def predict_endpoint(file: UploadFile = File(...)):try:7 unmodified linesexcept Exception as e:return JSONResponse(content={"error": str(e)}, status_code=500)# Required for Hugging Face Spaces (do NOT run uvicorn manually)# Just expose the appapp = app12345678910111213141516171819202122237 unmodified lines3132333435363738394041424344451 unmodified line474849505152537 unmodified lines61626364656667686970717273747576777879808182import torchfrom fastapi import FastAPI, File, UploadFilefrom fastapi.responses import JSONResponse, RedirectResponsefrom transformers import ConvNextForImageClassification, AutoImageProcessorfrom PIL import Imageimport ioimport gradio as grfrom starlette.middleware.cors import CORSMiddlewarefrom fastapi.staticfiles import StaticFilesfrom gradio.routes import mount_gradio_app# Class namesclass_names = ['Acne and Rosacea Photos', 'Actinic Keratosis Basal Cell Carcinoma and other Malignant Lesions', 'Atopic Dermatitis Photos','Bullous Disease Photos', 'Cellulitis Impetigo and other Bacterial Infections', 'Eczema Photos', 'Exanthems and Drug Eruptions','Hair Loss Photos Alopecia and other Hair Diseases', 'Herpes HPV and other STDs Photos', 'Light Diseases and Disorders of Pigmentation','Lupus and other Connective Tissue diseases', 'Melanoma Skin Cancer Nevi and Moles', 'Nail Fungus and other Nail Disease','Poison Ivy Photos and other Contact Dermatitis', 'Psoriasis pictures Lichen Planus and related diseases','Scabies Lyme Disease and other Infestations and Bites', 'Seborrheic Keratoses and other Benign Tumors', 'Systemic Disease','Tinea Ringworm Candidiasis and other Fungal Infections', 'Urticaria Hives', 'Vascular Tumors', 'Vasculitis Photos','Warts Molluscum and other Viral Infections']7 unmodified lines# FastAPI appapp = FastAPI()app.add_middleware(CORSMiddleware,allow_origins=["*"], # Adjust for productionallow_credentials=True,allow_methods=["*"],allow_headers=["*"],)# Predict functiondef predict(image: Image.Image):inputs = processor(images=image, return_tensors="pt")with torch.no_grad():1 unmodified linepredicted_class = torch.argmax(outputs.logits, dim=1).item()return predicted_class, class_names[predicted_class]# FastAPI route@app.post("/predict/")async def predict_endpoint(file: UploadFile = File(...)):try:7 unmodified linesexcept Exception as e:return JSONResponse(content={"error": str(e)}, status_code=500)@app.get("/")def redirect_root_to_gradio():return RedirectResponse(url="/gradio")# Gradio interfacedef gradio_interface(image):predicted_class, predicted_name = predict(image)return f"{predicted_name} (Class {predicted_class})"gradio_app = gr.Interface(fn=gradio_interface,inputs=gr.Image(type="pil"),outputs="text",title="Skin Disease Classifier",description="Upload a skin image to classify the condition using a fine-tuned ConvNeXt model.")# Mount Gradio in FastAPIapp = mount_gradio_app(app, gradio_app, path="/gradio")
app.py+40-13