Spaces:
Runtime error
Runtime error
SaladSlayer00
commited on
Commit
·
2bf3c18
1
Parent(s):
373340b
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import io
|
4 |
+
import os
|
5 |
+
import tempfile
|
6 |
+
from PIL import Image
|
7 |
+
import traceback
|
8 |
+
|
9 |
+
# API details
|
10 |
+
API_URL = "https://api-inference.huggingface.co/models/SaladSlayer00/twin_matcher"
|
11 |
+
headers = {"Authorization": f"{os.getenv('TOKEN')}"}
|
12 |
+
|
13 |
+
# Function to query the API with an image file
|
14 |
+
def query(filename):
|
15 |
+
with open(filename, "rb") as f:
|
16 |
+
data = f.read()
|
17 |
+
response = requests.post(API_URL, headers=headers, data=data)
|
18 |
+
response.raise_for_status() # Ensure successful response
|
19 |
+
return response.json()
|
20 |
+
|
21 |
+
# Function to process the image and predict
|
22 |
+
def predict(image):
|
23 |
+
try:
|
24 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpeg') as tmp_file:
|
25 |
+
image.save(tmp_file, format="JPEG")
|
26 |
+
tmp_file_path = tmp_file.name
|
27 |
+
|
28 |
+
predictions = query(tmp_file_path)
|
29 |
+
|
30 |
+
top_prediction = max(predictions, key=lambda x: x['score'])
|
31 |
+
result = (top_prediction['label'], top_prediction['score'])
|
32 |
+
|
33 |
+
os.remove(tmp_file_path)
|
34 |
+
|
35 |
+
return result
|
36 |
+
except Exception as e:
|
37 |
+
print(f"Exception during prediction: {e}")
|
38 |
+
traceback.print_exc()
|
39 |
+
return "Error during prediction", "N/A"
|
40 |
+
|
41 |
+
# Gradio interface
|
42 |
+
with gr.Interface(fn=predict,
|
43 |
+
inputs=gr.Image(type="pil"),
|
44 |
+
outputs=["text", "number"],
|
45 |
+
title="Celebrity Lookalike Predictor",
|
46 |
+
description="Take a snapshot to see which celebrity you look like!") as demo:
|
47 |
+
demo.launch()
|