File size: 1,973 Bytes
a0f740f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from typing import Any
from vlm.vlm_interface import VLMIterface
import google.generativeai as genai
from dotenv import load_dotenv

# Load environment variables from a .env file
load_dotenv()
# Fetch Google API key from environment variables
_api = os.getenv("GOOGLE_API_KEY")
NAME = "gemini-1.5-flash"

class VLM(VLMIterface):
    """
    This class implements the interface for a Visual Language Model (VLM) using Google's generative AI API.
    It inherits from the VLMInterface and configures the Google API to interact with the generative model.
    """

    def __init__(self) -> None:
        """
        Initialize the VLM object by configuring the Google generative AI model using the provided API key.
        """
        super().__init__()
        # Configure the generative AI model with the provided API key
        genai.configure(api_key=_api)
        # Initialize the model (Gemini 1.5 Flash) from Google's generative AI
        self.vlm = genai.GenerativeModel(NAME)

    def execution(self) -> Any | None:
        """
        Execute the VLM model and return the model object.
        If there is an error (e.g., invalid API key or connection issue), handle the exception gracefully.
        """
        try:
            return self.vlm
        except Exception as e:
            print(f"Something went wrong with the API: {e}")
            return None
    
    def model_name(self) -> str:
        return NAME

    def __str__(self) -> str:
        """
        Return a human-readable string representation of the VLM object.
        Useful for informal descriptions or when printing the object.
        """
        return f"VLM Model: {self.vlm.model_name}"

    def __repr__(self) -> str:
        """
        Return an official string representation of the VLM object.
        This should ideally be a valid Python expression that can recreate the object.
        """
        return f"VLM(api_key='****', model_name='{self.vlm.model_name}')"