SaladSlayer00 commited on
Commit
03c454c
1 Parent(s): bc0462c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -40
app.py CHANGED
@@ -3,47 +3,21 @@ 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.getenv('AWS_SECRET_ACCESS_KEY')
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:
@@ -51,23 +25,24 @@ def fetch_info(celebrity_label):
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"
@@ -77,20 +52,22 @@ def predict_and_fetch_images(input_image):
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()
 
3
  from datasets import load_dataset
4
  import requests
5
  import traceback
6
+ import os
7
 
8
  dataset = load_dataset("SaladSlayer00/twin_matcher")
9
 
10
  image_classifier = pipeline("image-classification", model="SaladSlayer00/twin_matcher")
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def fetch_info(celebrity_label):
13
  try:
 
14
  parts = celebrity_label.split("_")
15
  formatted_label = " ".join([part.capitalize() for part in parts])
 
 
16
  api_url = f'https://api.api-ninjas.com/v1/celebrity?name={formatted_label}'
17
+ token = os.getenv('TOKEN')
18
+ response = requests.get(api_url, headers={'X-Api-Key': token})
 
19
  if response.status_code == 200:
20
+ return response.text
21
  else:
22
  return "Description not available."
23
  except Exception as e:
 
25
  traceback.print_exc()
26
  return "Description not available."
27
 
28
+ # Function to fetch images for the predicted label from the Hugging Face dataset
29
  def fetch_images_for_label(label):
30
  label_data = dataset['train'].filter(lambda example: example['label'] == label)
31
  images = [example['image'] for example in label_data]
32
  return images
33
 
34
+ # Function to process the image, predict, and fetch images and info
35
  def predict_and_fetch_images(input_image):
36
  try:
37
+ # Use the image classifier pipeline
38
  predictions = image_classifier(input_image)
39
  top_prediction = max(predictions, key=lambda x: x['score'])
40
  label, score = top_prediction['label'], top_prediction['score']
41
 
42
+ # Fetch images for the predicted label
43
  images = fetch_images_for_label(label)
44
 
45
+ # Fetch information for the predicted label
46
  info = fetch_info(label)
47
 
48
  return label, score, images, info, "No Error"
 
52
  return "Error during prediction", 0, [], "N/A", str(e)
53
 
54
 
55
+
56
+ # Gradio interface
57
  iface = gr.Interface(
58
  fn=predict_and_fetch_images,
59
  inputs=gr.Image(type="pil", label="Upload or Take a Snapshot"),
60
  outputs=[
61
+ "text", # Predicted label
62
+ "number", # Prediction score
63
+ gr.Gallery(label="Lookalike Images"), # Slideshow component for images
64
+ "text", # Info/Description
65
  gr.Textbox(type="text", label="Feedback", placeholder="Provide feedback here") # Feedback textbox
66
  ],
67
  live=True,
68
+ title="Celebrity Lookalike Predictor with Description and Feedback",
69
  description="Take a snapshot or upload an image to see which celebrity you look like!"
70
  )
71
 
72
+ # Launch the Gradio interface
73
  iface.launch()