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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -8
app.py CHANGED
@@ -3,21 +3,52 @@ from transformers import pipeline
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,13 +56,13 @@ def fetch_info(celebrity_label):
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
@@ -51,8 +82,6 @@ def predict_and_fetch_images(input_image):
51
  traceback.print_exc()
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,
@@ -61,13 +90,13 @@ iface = gr.Interface(
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()
 
3
  from datasets import load_dataset
4
  import requests
5
  import traceback
6
+ import json
7
  import os
8
 
9
  dataset = load_dataset("SaladSlayer00/twin_matcher")
10
 
11
  image_classifier = pipeline("image-classification", model="SaladSlayer00/twin_matcher")
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:
 
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)
62
  images = [example['image'] for example in label_data]
63
  return images
64
 
65
+
66
  def predict_and_fetch_images(input_image):
67
  try:
68
  # Use the image classifier pipeline
 
82
  traceback.print_exc()
83
  return "Error during prediction", 0, [], "N/A", str(e)
84
 
 
 
85
  # Gradio interface
86
  iface = gr.Interface(
87
  fn=predict_and_fetch_images,
 
90
  "text", # Predicted label
91
  "number", # Prediction score
92
  gr.Gallery(label="Lookalike Images"), # Slideshow component for images
93
+ "html", # Info/Description as HTML
94
  gr.Textbox(type="text", label="Feedback", placeholder="Provide feedback here") # Feedback textbox
95
  ],
96
  live=True,
97
+ title="Celebrity Lookalike Predictor",
98
  description="Take a snapshot or upload an image to see which celebrity you look like!"
99
  )
100
 
101
+
102
  iface.launch()