-
Notifications
You must be signed in to change notification settings - Fork 0
/
gemini.py
executable file
·103 lines (74 loc) · 2.42 KB
/
gemini.py
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
94
95
96
97
98
99
100
101
102
import google.generativeai as genai
import os
import argparse
import PIL.Image
import re
from dotenv import load_dotenv
# import textwrap
load_dotenv('/usr/gemini/config/.env')
# Fetch API key from environment variable
GOOGLE_API_KEY = os.getenv('API_KEY')
# Configure the generative AI library with the API key
genai.configure(api_key=GOOGLE_API_KEY)
# Initialize the generative model
model = genai.GenerativeModel('gemini-1.5-pro-latest')
# Parse command line arguments
def arg_parse():
"""
Parses command line arguments to extract the prompt.
Returns:
str: The prompt extracted from command line arguments.
"""
parser = argparse.ArgumentParser(description='Gemini AI for terminal')
parser.add_argument('-i', '--img', type=str, help='Path to an image')
parser.add_argument('words', nargs='*', type=str, help='The words to print')
# Parse the command-line arguments
args = parser.parse_args()
if args.img:
if args.words:
phrase = ' '.join(args.words)
img = PIL.Image.open(args.img)
prompt = [phrase, img]
return prompt
else:
return "No valid prompt provided."
# Join the words into a single phrase if no other options are provided
if args.words:
phrase = ' '.join(args.words)
return phrase
# Default message if no valid arguments are provided
return "No valid arguments provided."
def get_response(prompt):
response = model.generate_content(
prompt,
generation_config = genai.types.GenerationConfig(
candidate_count = 1,
temperature = 0.5
),
stream=True
)
for chunk in response:
to_display(chunk.text)
# Format text for display
def to_display(text):
"""
Formats the generated text for display.
Args:
text (str): The generated text to be formatted.
Returns:
str: The formatted text ready for display.
"""
text = text.replace("##", "")
# Add ANCI styles to the output
text = re.sub(r'\*\*(.*?)\*\*', r'\033[1m\1\033[0m', text, flags=re.DOTALL)
text = re.sub(r'\*(.*?)\*', r'\033[3m\1\033[0m', text, flags=re.DOTALL)
text = re.sub(r'^( \*.*)$', r'\n\1', text, flags=re.MULTILINE)
print(text)
def main():
"""
Main function to execute the program.
"""
prompt = arg_parse()
get_response(prompt)
if __name__ == "__main__":
main()