eybro commited on
Commit
8a9f973
·
verified ·
1 Parent(s): 17cab1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -9
app.py CHANGED
@@ -22,6 +22,11 @@ split_dataset = dataset['train'].train_test_split(test_size=0.2, seed=42) # 80%
22
  dataset['train'] = split_dataset['train']
23
  dataset['test'] = split_dataset['test']
24
 
 
 
 
 
 
25
  def create_url_from_title(title: str, timestamp: int):
26
  video_urls = load_dataset("eybro/video_urls")
27
  df = video_urls['train'].to_pandas()
@@ -90,7 +95,6 @@ def inference(image):
90
  result_image = get_image(top4[0])
91
  url = create_url_from_title(result_image['label'], result_image['timestamp'])
92
  result = f"{result_image['label']} {result_image['timestamp']} \n{url}"
93
- #result = f"[This is a link to the video]({url})"
94
 
95
  n=2
96
  plt.figure(figsize=(8, 8))
@@ -111,18 +115,46 @@ def inference(image):
111
  ax.get_yaxis().set_visible(False)
112
 
113
  return result
 
 
 
 
 
 
 
114
 
115
  with gr.Blocks() as demo:
116
  gr.Markdown(
117
- """
118
- # Image to Video App
119
- Upload an image from a Gordon Ramsay show to find the corresponding youtube video
120
- """)
121
- with gr.Column(scale=1):
122
- inp = gr.Image(label='Upload image')
123
- with gr.Column(scale=1):
 
 
 
 
 
 
 
 
 
124
  out = gr.Markdown()
125
- inp.change(inference, inp, out)
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
  if __name__ == "__main__":
128
  demo.launch()
 
22
  dataset['train'] = split_dataset['train']
23
  dataset['test'] = split_dataset['test']
24
 
25
+ # Example images
26
+ example_images = {
27
+ "Example 1": "examples/example_1.jpg",
28
+ }
29
+
30
  def create_url_from_title(title: str, timestamp: int):
31
  video_urls = load_dataset("eybro/video_urls")
32
  df = video_urls['train'].to_pandas()
 
95
  result_image = get_image(top4[0])
96
  url = create_url_from_title(result_image['label'], result_image['timestamp'])
97
  result = f"{result_image['label']} {result_image['timestamp']} \n{url}"
 
98
 
99
  n=2
100
  plt.figure(figsize=(8, 8))
 
115
  ax.get_yaxis().set_visible(False)
116
 
117
  return result
118
+
119
+ def load_example(example_name):
120
+ if example_name in example_images:
121
+ image_path = example_images[example_name]
122
+ image = Image.open(image_path)
123
+ return image
124
+ return None
125
 
126
  with gr.Blocks() as demo:
127
  gr.Markdown(
128
+ """
129
+ # Image to Video App
130
+ Find your favorite Gordon Ramasay scene by uploading an image from the scene, the app will thereafter find a corresponding youtube video for that scene.
131
+ Or try one of our examples (unseen data for the model).
132
+ """
133
+ )
134
+ with gr.Row():
135
+ with gr.Column():
136
+ inp_image = gr.Image(label="Upload Image")
137
+ with gr.Column():
138
+ example_selection = gr.Gallery(
139
+ value=list(example_images.values()),
140
+ label="Click an Example Image",
141
+ ).style(grid=[1], height="auto")
142
+
143
+ with gr.Row():
144
  out = gr.Markdown()
145
+
146
+ def handle_selection(user_image, selected_example):
147
+ if user_image is not None:
148
+ return inference(user_image)
149
+ elif selected_example is not None:
150
+ image = load_example(selected_example)
151
+ return inference(image)
152
+ else:
153
+ return "Please upload an image or select an example image."
154
+
155
+ inputs = [inp_image, example_selection]
156
+ outputs = out
157
+ example_selection.select(handle_selection, inputs, outputs)
158
 
159
  if __name__ == "__main__":
160
  demo.launch()