Spaces:
Running
Running
from googleapiclient.discovery import build | |
# from dotenv import load_dotenv | |
# load_dotenv(r'C:\Users\Vaibhav Arora\Documents\MyExperimentsandCodes\APPS_WEBSITES\CANADA_WHOLESALE_PROJECT\GITHUB_REPOS\mvp-vue\wholesale-grocery-app\AIAPPS\.env') | |
from google import genai | |
from pydantic import BaseModel | |
import ast | |
import os | |
import json | |
import random | |
import time | |
class Categorise(BaseModel): | |
category: str | |
client = genai.Client(api_key=random.choice(json.loads(os.getenv("GEMINI_KEY_LIST")))) | |
def categorise(product): | |
client = genai.Client(api_key=random.choice(json.loads(os.getenv("GEMINI_KEY_LIST")))) | |
try: | |
response = client.models.generate_content( | |
model="gemini-2.5-flash-lite-preview-06-17", | |
contents=f"Categorise this product:{product} , into one of the following categories: `Fruits,Vegetables,Bakery`", | |
config={ | |
"response_mime_type": "application/json", | |
"response_schema": list[Categorise], | |
}, | |
) | |
except: | |
time.sleep(2) | |
response = client.models.generate_content( | |
model="gemini-2.5-flash", | |
contents=f"Categorise this product:{product} , into one of the following categories: `Fruits,Vegetables,Bakery`", | |
config={ | |
"response_mime_type": "application/json", | |
"response_schema": list[Categorise], | |
}, | |
) | |
return ast.literal_eval(response.text)[0]["category"] | |
def search_images(query: str, api_key: str, cse_id: str,no) -> dict | None: | |
""" | |
Performs an image search using the Google Custom Search API. | |
""" | |
print(f"Searching for images with query: '{query}'...") | |
try: | |
service = build("customsearch", "v1", developerKey="AIzaSyBntcCqrtL5tdpM3iIXzPvKydCvZx1KdqQ") | |
result = service.cse().list( | |
q=query, | |
cx="a2982aa5c06f54e66", | |
searchType='image', | |
num=no | |
).execute() | |
print("Search successful.") | |
return result | |
except Exception as e: | |
print(f"An error occurred during Google Search: {e}") | |
return None | |
def search_and_filter_images(query,no=2): | |
search_results = search_images(query, os.getenv("CSE_API_KEY"), os.getenv("CSE_ID"),no) | |
if search_results and 'items' in search_results: | |
top_10_items = search_results['items'] | |
print(f"Found {len(top_10_items)} image results. Downloading them...") | |
image_files_for_llm = [] | |
downloaded_filenames = [] | |
for i, item in enumerate(top_10_items): | |
image_url = item.get('link') | |
if not image_url: | |
continue | |
file_extension = os.path.splitext(image_url.split("?")[0])[-1] | |
if not file_extension: | |
file_extension = ".unknown" # Default extension | |
if not file_extension in [".jpeg", ".jpg", ".png", ".gif", ".bmp", ".webp"]: | |
continue | |
image_files_for_llm.append({ | |
"type": "image_url", | |
"image_url": f"{image_url}" | |
}) | |
# print(image_files_for_llm) | |
return (image_files_for_llm) | |