twin_matcher / app.py
SaladSlayer00's picture
Create app.py
2bf3c18
raw
history blame
1.5 kB
import gradio as gr
import requests
import io
import os
import tempfile
from PIL import Image
import traceback
# API details
API_URL = "https://api-inference.huggingface.co/models/SaladSlayer00/twin_matcher"
headers = {"Authorization": f"{os.getenv('TOKEN')}"}
# Function to query the API with an image file
def query(filename):
with open(filename, "rb") as f:
data = f.read()
response = requests.post(API_URL, headers=headers, data=data)
response.raise_for_status() # Ensure successful response
return response.json()
# Function to process the image and predict
def predict(image):
try:
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpeg') as tmp_file:
image.save(tmp_file, format="JPEG")
tmp_file_path = tmp_file.name
predictions = query(tmp_file_path)
top_prediction = max(predictions, key=lambda x: x['score'])
result = (top_prediction['label'], top_prediction['score'])
os.remove(tmp_file_path)
return result
except Exception as e:
print(f"Exception during prediction: {e}")
traceback.print_exc()
return "Error during prediction", "N/A"
# Gradio interface
with gr.Interface(fn=predict,
inputs=gr.Image(type="pil"),
outputs=["text", "number"],
title="Celebrity Lookalike Predictor",
description="Take a snapshot to see which celebrity you look like!") as demo:
demo.launch()