phani50101 commited on
Commit
809312a
·
1 Parent(s): 88bd3ae

Add application file2

Browse files
Files changed (1) hide show
  1. app.py +24 -22
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  from huggingface_hub import snapshot_download
3
  import gradio as gr
4
  import openvino_genai
@@ -73,33 +72,39 @@ generation_executor = ThreadPoolExecutor(max_workers=4) # Increased workers
73
  image_executor = ThreadPoolExecutor(max_workers=8)
74
 
75
  def fetch_images(query: str, num: int = DEFAULT_NUM_IMAGES) -> list:
76
- """Fetch images in parallel using ThreadPoolExecutor"""
77
  start_time = time.time()
78
 
79
  if num <= 0:
80
  return []
81
 
82
  try:
83
- futures = []
84
  service = build("customsearch", "v1", developerKey=GOOGLE_API_KEY)
85
-
86
- for _ in range(num):
87
- future = image_executor.submit(
88
- service.cse().list(q=query, cx=GOOGLE_CSE_ID, searchType="image", num=1).execute
89
- )
90
- futures.append(future)
91
-
92
  image_links = []
93
- for future in as_completed(futures):
94
- try:
95
- res = future.result()
96
- if "items" in res and res["items"]:
97
- image_links.append(res["items"][0]["link"])
98
- except Exception as e:
99
- print(f"Image fetch error: {e}")
100
 
101
- print(f"Parallel image fetch time: {time.time() - start_time:.2f} seconds")
102
- return image_links
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  except Exception as e:
104
  print(f"Error in image fetching: {e}")
105
  return []
@@ -356,11 +361,8 @@ with gr.Blocks(css="""
356
  gr.HTML("""
357
  <div class="system-info">
358
  <strong>Performance Optimized for High-RAM Systems</strong>
359
-
360
  <ul>
361
-
362
  <li>Adaptive resource allocation based on request type</li>
363
-
364
  </ul>
365
  </div>
366
  """)
 
 
1
  from huggingface_hub import snapshot_download
2
  import gradio as gr
3
  import openvino_genai
 
72
  image_executor = ThreadPoolExecutor(max_workers=8)
73
 
74
  def fetch_images(query: str, num: int = DEFAULT_NUM_IMAGES) -> list:
75
+ """Fetch unique images by requesting different result pages"""
76
  start_time = time.time()
77
 
78
  if num <= 0:
79
  return []
80
 
81
  try:
 
82
  service = build("customsearch", "v1", developerKey=GOOGLE_API_KEY)
 
 
 
 
 
 
 
83
  image_links = []
84
+ seen_urls = set() # To track unique URLs
 
 
 
 
 
 
85
 
86
+ # Start from different positions to get unique images
87
+ for start_index in range(1, num * 2, 2): # Step by 2 to get different pages
88
+ if len(image_links) >= num:
89
+ break
90
+
91
+ res = service.cse().list(
92
+ q=query,
93
+ cx=GOOGLE_CSE_ID,
94
+ searchType="image",
95
+ num=1, # Get one result per request
96
+ start=start_index # Start at different positions
97
+ ).execute()
98
+
99
+ if "items" in res and res["items"]:
100
+ item = res["items"][0]
101
+ # Skip duplicates
102
+ if item["link"] not in seen_urls:
103
+ image_links.append(item["link"])
104
+ seen_urls.add(item["link"])
105
+
106
+ print(f"Unique image fetch time: {time.time() - start_time:.2f} seconds")
107
+ return image_links[:num] # Return only the requested number
108
  except Exception as e:
109
  print(f"Error in image fetching: {e}")
110
  return []
 
361
  gr.HTML("""
362
  <div class="system-info">
363
  <strong>Performance Optimized for High-RAM Systems</strong>
 
364
  <ul>
 
365
  <li>Adaptive resource allocation based on request type</li>
 
366
  </ul>
367
  </div>
368
  """)