Spaces:
Runtime error
Runtime error
SaladSlayer00
commited on
Commit
•
781cf08
1
Parent(s):
d25a172
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,96 @@
|
|
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 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
# Function to query the API with an image file
|
15 |
-
def query(filename):
|
16 |
-
with open(filename, "rb") as f:
|
17 |
-
data = f.read()
|
18 |
-
response = requests.post(API_URL, headers=headers, data=data)
|
19 |
-
response.raise_for_status() # Ensure successful response
|
20 |
-
return response.json()
|
21 |
-
|
22 |
-
# Function to process the image and predict
|
23 |
-
def predict(image):
|
24 |
try:
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
predictions = query(tmp_file_path)
|
30 |
|
|
|
|
|
|
|
|
|
31 |
top_prediction = max(predictions, key=lambda x: x['score'])
|
32 |
-
|
|
|
|
|
|
|
33 |
|
34 |
-
|
|
|
35 |
|
36 |
-
return
|
37 |
except Exception as e:
|
38 |
-
print(f"
|
39 |
traceback.print_exc()
|
40 |
-
return "Error during prediction", "N/A"
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from datasets import load_dataset
|
4 |
import requests
|
|
|
|
|
|
|
|
|
5 |
import traceback
|
6 |
+
import json
|
7 |
|
8 |
+
dataset = load_dataset("SaladSlayer00/twin_matcher")
|
9 |
+
|
10 |
+
image_classifier = pipeline("image-classification", model="SaladSlayer00/twin_matcher")
|
11 |
+
|
12 |
+
def format_info(info_json):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
try:
|
14 |
+
|
15 |
+
info_data = json.loads(info_json)
|
16 |
+
|
17 |
+
formatted_info = "<table style='border-collapse: collapse; width: 80%; margin: 20px;'>"
|
18 |
+
formatted_info += "<tr style='background-color: #f2f2f2;'>"
|
19 |
+
for key in info_data[0].keys():
|
20 |
+
formatted_info += f"<th style='border: 1px solid #dddddd; text-align: left; padding: 8px;'><b>{key.capitalize()}</b></th>"
|
21 |
+
formatted_info += "</tr>"
|
22 |
+
|
23 |
+
for entry in info_data:
|
24 |
+
formatted_info += "<tr>"
|
25 |
+
for value in entry.values():
|
26 |
+
formatted_info += f"<td style='border: 1px solid #dddddd; text-align: left; padding: 8px;'>{value}</td>"
|
27 |
+
formatted_info += "</tr>"
|
28 |
+
formatted_info += "</table>"
|
29 |
+
return formatted_info
|
30 |
+
except Exception as e:
|
31 |
+
print(f"Error formatting info: {e}")
|
32 |
+
return "Info not available."
|
33 |
+
|
34 |
+
def fetch_info(celebrity_label):
|
35 |
+
try:
|
36 |
+
|
37 |
+
parts = celebrity_label.split("_")
|
38 |
+
formatted_label = " ".join([part.capitalize() for part in parts])
|
39 |
+
|
40 |
+
|
41 |
+
api_url = f'https://api.api-ninjas.com/v1/celebrity?name={formatted_label}'
|
42 |
+
|
43 |
+
token = os.environ.get('TOKEN')
|
44 |
+
response = requests.get(api_url, headers={'X-Api-Key': token})
|
45 |
+
if response.status_code == 200:
|
46 |
+
return format_info(response.text)
|
47 |
+
else:
|
48 |
+
return "Description not available."
|
49 |
+
except Exception as e:
|
50 |
+
print(f"Error fetching information: {e}")
|
51 |
+
traceback.print_exc()
|
52 |
+
return "Description not available."
|
53 |
+
|
54 |
+
def fetch_images_for_label(label):
|
55 |
+
label_data = dataset['train'].filter(lambda example: example['label'] == label)
|
56 |
+
images = [example['image'] for example in label_data]
|
57 |
+
return images
|
58 |
|
|
|
59 |
|
60 |
+
def predict_and_fetch_images(input_image):
|
61 |
+
try:
|
62 |
+
|
63 |
+
predictions = image_classifier(input_image)
|
64 |
top_prediction = max(predictions, key=lambda x: x['score'])
|
65 |
+
label, score = top_prediction['label'], top_prediction['score']
|
66 |
+
|
67 |
+
|
68 |
+
images = fetch_images_for_label(label)
|
69 |
|
70 |
+
|
71 |
+
info = fetch_info(label)
|
72 |
|
73 |
+
return label, score, images, info, "No Error"
|
74 |
except Exception as e:
|
75 |
+
print(f"Error during prediction: {e}")
|
76 |
traceback.print_exc()
|
77 |
+
return "Error during prediction", 0, [], "N/A", str(e)
|
78 |
+
|
79 |
+
|
80 |
+
iface = gr.Interface(
|
81 |
+
fn=predict_and_fetch_images,
|
82 |
+
inputs=gr.Image(type="pil", label="Upload or Take a Snapshot"),
|
83 |
+
outputs=[
|
84 |
+
"text",
|
85 |
+
"number",
|
86 |
+
gr.Gallery(label="Lookalike Images"),
|
87 |
+
"html",
|
88 |
+
gr.Textbox(type="text", label="Feedback", placeholder="Provide feedback here") # Feedback textbox
|
89 |
+
],
|
90 |
+
live=True,
|
91 |
+
title="Celebrity Lookalike Predictor",
|
92 |
+
description="Take a snapshot or upload an image to see which celebrity you look like!"
|
93 |
+
)
|
94 |
+
|
95 |
+
|
96 |
+
iface.launch()
|