1Prarthana/MultiTask-AI-Model
0
1import os
2import json
3from PIL import Image
4
5import google.generativeai as genai
6
7# working directory path
8working_dir = os.path.dirname(os.path.abspath(__file__))
9
10# path of config_data file
11config_file_path = f"{working_dir}/Config.json"
12with open(config_file_path, "r") as f:
13 config_data = json.load(f)
14
15# config_data = json.load(open("Config.json"))
16
17# loading the GOOGLE_API_KEY
18GOOGLE_API_KEY = config_data["GOOGLE_API_KEY"]
19
20# configuring google.generativeai with API key
21genai.configure(api_key=GOOGLE_API_KEY)
22
23
24def load_gemini_pro_model():
25 gemini_pro_model = genai.GenerativeModel("gemini-2.0-flash")
26 return gemini_pro_model
27
28
29# get response from Gemini-Pro-Vision model - image/text to text
30def gemini_pro_vision_response(prompt, image):
31 gemini_pro_vision_model = genai.GenerativeModel("gemini-2.0-flash")
32 response = gemini_pro_vision_model.generate_content([prompt, image])
33 result = response.text
34 return result
35
36
37# get response from embeddings model - text to embeddings
38def embeddings_model_response(input_text):
39 embedding_model = "models/embedding-001"
40 embedding = genai.embed_content(model=embedding_model,
41 content=input_text,
42 task_type="retrieval_document")
43 embedding_list = embedding["embedding"]
44 return embedding_list
45
46
47# get response from Gemini-Pro model - text to text
48def gemini_pro_response(user_prompt):
49 gemini_pro_model = genai.GenerativeModel("gemini-2.0-flash")
50 response = gemini_pro_model.generate_content(user_prompt)
51 result = response.text
52 return result
53
54
55# result = gemini_pro_response("What is Machine Learning")
56# print(result)
57# print("-"*50)
58#
59#
60# image = Image.open("test_image.png")
61# result = gemini_pro_vision_response("Write a short caption for this image", image)
62# print(result)
63# print("-"*50)
64#
65#
66# result = embeddings_model_response("Machine Learning is a subset of Artificial Intelligence")
67# print(result)