SaladSlayer00 commited on
Commit
2bc793a
1 Parent(s): 09d93d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -43
app.py CHANGED
@@ -11,51 +11,43 @@ 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
- 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,23 +56,20 @@ def fetch_images_for_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
  example_images = [
86
  "images/megan_fox.png",
 
11
  image_classifier = pipeline("image-classification", model="SaladSlayer00/twin_matcher_beta")
12
 
13
  def format_info(info_json):
14
+
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
+
 
 
35
 
36
 
37
  def fetch_info(celebrity_label):
 
38
 
39
+ parts = celebrity_label.split("_")
40
+ formatted_label = " ".join([part.capitalize() for part in parts])
41
 
42
 
43
+ api_url = f'https://api.api-ninjas.com/v1/celebrity?name={formatted_label}'
 
 
 
 
 
 
 
 
 
 
 
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
 
57
 
58
  def predict_and_fetch_images(input_image):
59
+
60
  # Use the image classifier pipeline
61
+ predictions = image_classifier(input_image)
62
+ top_prediction = max(predictions, key=lambda x: x['score'])
63
+ label, score = top_prediction['label'], top_prediction['score']
64
 
65
  # Fetch images for the predicted label
66
+ images = fetch_images_for_label(label)
67
 
68
  # Fetch information for the predicted label
69
+ info = fetch_info(label)
70
+
71
+ return label, score, images, info, "No Error"
72
 
 
 
 
 
 
73
 
74
  example_images = [
75
  "images/megan_fox.png",