Spaces:
Runtime error
Runtime error
Commit ·
5f830d0
1
Parent(s): 2bc793a
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,43 +11,51 @@ dataset = load_dataset("SaladSlayer00/twin_matcher_data")
|
|
| 11 |
image_classifier = pipeline("image-classification", model="SaladSlayer00/twin_matcher_beta")
|
| 12 |
|
| 13 |
def format_info(info_json):
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
formatted_info += "<tr style='background-color: #f2f2f2;'>"
|
| 22 |
-
for key in info_data[0].keys():
|
| 23 |
-
formatted_info += f"<th style='border: 1px solid #dddddd; text-align: left; padding: 8px;'><b>{key.capitalize()}</b></th>"
|
| 24 |
-
formatted_info += "</tr>"
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
formatted_info += f"<td style='border: 1px solid #dddddd; text-align: left; padding: 8px;'>{value}</td>"
|
| 31 |
formatted_info += "</tr>"
|
| 32 |
-
formatted_info += "</table>"
|
| 33 |
-
return formatted_info
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
def fetch_info(celebrity_label):
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
token = os.getenv('TOKEN')
|
| 46 |
-
response = requests.get(api_url, headers={'X-Api-Key': token})
|
| 47 |
-
if response.status_code == 200:
|
| 48 |
-
return format_info(response.text)
|
| 49 |
-
else:
|
| 50 |
-
return "A shining star for sure."
|
| 51 |
|
| 52 |
def fetch_images_for_label(label):
|
| 53 |
label_data = dataset['train'].filter(lambda example: example['label'] == label)
|
|
@@ -56,19 +64,23 @@ def fetch_images_for_label(label):
|
|
| 56 |
|
| 57 |
|
| 58 |
def predict_and_fetch_images(input_image):
|
| 59 |
-
|
| 60 |
# Use the image classifier pipeline
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
# Fetch images for the predicted label
|
| 66 |
-
|
| 67 |
|
| 68 |
# Fetch information for the predicted label
|
| 69 |
-
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
|
| 74 |
example_images = [
|
|
@@ -78,7 +90,6 @@ example_images = [
|
|
| 78 |
"images/alvaro_morte.png",
|
| 79 |
"images/amber_heard.png"
|
| 80 |
]
|
| 81 |
-
|
| 82 |
# Gradio interface
|
| 83 |
iface = gr.Interface(
|
| 84 |
fn=predict_and_fetch_images,
|
|
@@ -98,3 +109,4 @@ iface = gr.Interface(
|
|
| 98 |
|
| 99 |
|
| 100 |
iface.launch()
|
|
|
|
|
|
| 11 |
image_classifier = pipeline("image-classification", model="SaladSlayer00/twin_matcher_beta")
|
| 12 |
|
| 13 |
def format_info(info_json):
|
| 14 |
+
try:
|
| 15 |
|
| 16 |
+
info_data = json.loads(info_json)
|
| 17 |
|
| 18 |
|
| 19 |
+
formatted_info = "<table style='border-collapse: collapse; width: 80%; margin: 20px;'>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
formatted_info += "<tr style='background-color: #f2f2f2;'>"
|
| 22 |
+
for key in info_data[0].keys():
|
| 23 |
+
formatted_info += f"<th style='border: 1px solid #dddddd; text-align: left; padding: 8px;'><b>{key.capitalize()}</b></th>"
|
|
|
|
| 24 |
formatted_info += "</tr>"
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
|
| 27 |
+
for entry in info_data:
|
| 28 |
+
formatted_info += "<tr>"
|
| 29 |
+
for value in entry.values():
|
| 30 |
+
formatted_info += f"<td style='border: 1px solid #dddddd; text-align: left; padding: 8px;'>{value}</td>"
|
| 31 |
+
formatted_info += "</tr>"
|
| 32 |
+
formatted_info += "</table>"
|
| 33 |
+
return formatted_info
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"Error formatting info: {e}")
|
| 36 |
+
return "Info not available."
|
| 37 |
|
| 38 |
|
| 39 |
def fetch_info(celebrity_label):
|
| 40 |
+
try:
|
| 41 |
|
| 42 |
+
parts = celebrity_label.split("_")
|
| 43 |
+
formatted_label = " ".join([part.capitalize() for part in parts])
|
| 44 |
|
| 45 |
|
| 46 |
+
api_url = f'https://api.api-ninjas.com/v1/celebrity?name={formatted_label}'
|
| 47 |
+
|
| 48 |
+
token = os.getenv('TOKEN')
|
| 49 |
+
response = requests.get(api_url, headers={'X-Api-Key': token})
|
| 50 |
+
if response.status_code == 200:
|
| 51 |
+
return format_info(response.text)
|
| 52 |
+
else:
|
| 53 |
+
return "Description not available."
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Error fetching information: {e}")
|
| 56 |
+
traceback.print_exc()
|
| 57 |
+
return "Description not available."
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
def fetch_images_for_label(label):
|
| 61 |
label_data = dataset['train'].filter(lambda example: example['label'] == label)
|
|
|
|
| 64 |
|
| 65 |
|
| 66 |
def predict_and_fetch_images(input_image):
|
| 67 |
+
try:
|
| 68 |
# Use the image classifier pipeline
|
| 69 |
+
predictions = image_classifier(input_image)
|
| 70 |
+
top_prediction = max(predictions, key=lambda x: x['score'])
|
| 71 |
+
label, score = top_prediction['label'], top_prediction['score']
|
| 72 |
|
| 73 |
# Fetch images for the predicted label
|
| 74 |
+
images = fetch_images_for_label(label)
|
| 75 |
|
| 76 |
# Fetch information for the predicted label
|
| 77 |
+
info = fetch_info(label)
|
| 78 |
|
| 79 |
+
return label, score, images, info, "No Error"
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print(f"Error during prediction: {e}")
|
| 82 |
+
traceback.print_exc()
|
| 83 |
+
return "Error during prediction", 0, [], "N/A", str(e)
|
| 84 |
|
| 85 |
|
| 86 |
example_images = [
|
|
|
|
| 90 |
"images/alvaro_morte.png",
|
| 91 |
"images/amber_heard.png"
|
| 92 |
]
|
|
|
|
| 93 |
# Gradio interface
|
| 94 |
iface = gr.Interface(
|
| 95 |
fn=predict_and_fetch_images,
|
|
|
|
| 109 |
|
| 110 |
|
| 111 |
iface.launch()
|
| 112 |
+
|