CineAI commited on
Commit
a0f740f
1 Parent(s): 79b3561

Create vlm.py

Browse files
Files changed (1) hide show
  1. vlm/vlm.py +55 -0
vlm/vlm.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import Any
3
+ from vlm.vlm_interface import VLMIterface
4
+ import google.generativeai as genai
5
+ from dotenv import load_dotenv
6
+
7
+ # Load environment variables from a .env file
8
+ load_dotenv()
9
+ # Fetch Google API key from environment variables
10
+ _api = os.getenv("GOOGLE_API_KEY")
11
+ NAME = "gemini-1.5-flash"
12
+
13
+ class VLM(VLMIterface):
14
+ """
15
+ This class implements the interface for a Visual Language Model (VLM) using Google's generative AI API.
16
+ It inherits from the VLMInterface and configures the Google API to interact with the generative model.
17
+ """
18
+
19
+ def __init__(self) -> None:
20
+ """
21
+ Initialize the VLM object by configuring the Google generative AI model using the provided API key.
22
+ """
23
+ super().__init__()
24
+ # Configure the generative AI model with the provided API key
25
+ genai.configure(api_key=_api)
26
+ # Initialize the model (Gemini 1.5 Flash) from Google's generative AI
27
+ self.vlm = genai.GenerativeModel(NAME)
28
+
29
+ def execution(self) -> Any | None:
30
+ """
31
+ Execute the VLM model and return the model object.
32
+ If there is an error (e.g., invalid API key or connection issue), handle the exception gracefully.
33
+ """
34
+ try:
35
+ return self.vlm
36
+ except Exception as e:
37
+ print(f"Something went wrong with the API: {e}")
38
+ return None
39
+
40
+ def model_name(self) -> str:
41
+ return NAME
42
+
43
+ def __str__(self) -> str:
44
+ """
45
+ Return a human-readable string representation of the VLM object.
46
+ Useful for informal descriptions or when printing the object.
47
+ """
48
+ return f"VLM Model: {self.vlm.model_name}"
49
+
50
+ def __repr__(self) -> str:
51
+ """
52
+ Return an official string representation of the VLM object.
53
+ This should ideally be a valid Python expression that can recreate the object.
54
+ """
55
+ return f"VLM(api_key='****', model_name='{self.vlm.model_name}')"