File size: 3,277 Bytes
72eef4f
f0b4404
 
72eef4f
 
 
 
 
 
 
 
 
 
 
 
6af31ea
 
72eef4f
 
 
 
 
 
85354fe
72eef4f
 
 
 
 
 
 
 
 
 
85354fe
72eef4f
 
 
 
 
 
 
 
 
 
 
 
 
6af31ea
72eef4f
 
6af31ea
72eef4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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)