Spaces:
Runtime error

tessttt / app.py
unthinkablee's picture
Update app.py
9f6460d verified
Raw History Blame Contribute Delete
2.16 kB
from fastapi import FastAPI, File, UploadFile
from PIL import Image
import torch
from transformers import SegformerForImageSegmentation, AutoFeatureExtractor
from typing import List
import gradio as gr
import openai
# Set DALL-E API key
openai.api_key = "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"
# Load Hugging Face model and feature extractor
model_name = "nvidia/segformer-b0-finetuned-ade-512-512" # Replace with a dental-specific model if available
model = SegformerForImageSegmentation.from_pretrained(model_name)
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
def process_image(file):
"""
Process the uploaded image and add teeth to the user's portrait or mouth-marked image.
"""
try:
# Load image from upload
image = Image.open(file).convert("RGB")
# Preprocess the image
inputs = feature_extractor(images=image, return_tensors="pt")
# Perform segmentation or feature extraction
with torch.no_grad():
outputs = model(**inputs)
# Extract segmentation map
segmentation_map = outputs.logits.argmax(dim=1).squeeze().cpu().numpy()
# Placeholder logic for teeth overlay (to be replaced with actual logic)
# Here, you can add logic to detect the mouth region and overlay teeth images
result_image = image # This is where you'd add the new teeth overlay
# Save or return the processed image
result_path = "result_with_teeth.jpg"
result_image.save(result_path)
return result_image
except Exception as e:
return f"Error: {str(e)}"
# Set up Gradio interface
interface = gr.Interface(
fn=process_image,
inputs=gr.Image(type="file", label="Upload Your Image"),
outputs=gr.Image(type="file", label="Image with Teeth Added"),
title="Teeth Adding App",
description="Upload your portrait or mouth-marked image to add teeth using AI."
)
if __name__ == "__main__":
interface.launch(server_name="0.0.0.0", server_port=8000)