-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgemini.py
188 lines (147 loc) · 5.86 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import google_handlers
import google.generativeai as genai
import os
from dotenv import load_dotenv
load_dotenv()
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel('gemini-pro')
def get_response(prompt, generation_config=None):
"""Get a response text from the model for the given prompt.
Args
----
- `prompt`: Prompt for the model.
- `generation_config`: GenerationConfig object for the model.
Returns
-------
- `str`: Response text from the model.
"""
response = model.generate_content(
contents=prompt, generation_config=generation_config)
return response.text
def get_config(temperature=0.9, top_k=32, top_p=1.0):
"""Returns the generation config object for the model.
Args
----
- `temperature`: Randomness of the output. High -> diverse & creative, Low -> focused & accurate.
- Range: 0.0 to 1.0
- Default: 0.9
- `top_k`: Top k tokens to consider at each step. Higher -> more diverse, Lower -> more focused.
- Range: 1 to 40
- Default: 32
- `top_p`: Nucleus sampling. Higher -> more diverse, Lower -> more focused.
- Range: 0.0 to 1.0
- Default: 1.0
Returns
-------
- GenerationConfig object
"""
return genai.GenerationConfig(
temperature=temperature,
top_k=top_k,
top_p=top_p
)
def preprocess(text):
"""Preprocess the text to remove any unwanted characters that can cause issues.
Keep only alphanumeric characters, spaces, and punctuation.
Args
----
- `text`: Text to be preprocessed.
Returns
-------
- `str`: Preprocessed text.
"""
return ''.join(ch for ch in text if ch.isalnum() or ch.isspace() or ch in [',', '.', '!', '?', ':', ';', '-', '(', ')', '"', "'"])
def handler(celebrity, say_what, model_parameters, target_language, audio_path):
"""
1. Attach a pretext to the prompt (description of the website and task)
2. Get config object using the model parameters dictionary
3. Get response from the model
4. Translate the response to the target_language & generate audio
5. Return the response text & audio path
Args
----
- `celebrity`: Name of the celebrity.
- `say_what`: What the celebrity should say.
- `model_parameters`: Dictionary containing the model parameters.
- `target_language`: Language to translate the response to.
- `audio_path`: Path to save the audio file.
Returns
-------
- `str`: Translated response text.
- `str`: Path to the audio file.
"""
# Structure the prompt
prompt = f"""You are working on a celebrity mimicking website. You need to generate text that very closely resembles the style, thought process, and humor of a celebrity. The text should be funny, engaging, deep, and thought-provoking. The response text should be plain text and keep it under 200 words.
Celebrity: {celebrity}
I want them to say: {say_what}
"""
print(f"⌨️ Prompt:\n{prompt}")
# Generate config object
generation_config = get_config(
temperature=model_parameters.get("temperature", 0.9),
top_k=model_parameters.get("top_k", 32),
top_p=model_parameters.get("top_p", 1.0)
)
# Get response from gemini model
response = get_response(prompt, generation_config)
response = preprocess(response)
# Get translation & audio
translation = google_handlers.translate_message(response, target_language)
audio_path = google_handlers.make_audio(
translation, target_language, audio_path)
return translation, audio_path
def fine_tune(text, fine_tune, original_lang):
"""Fine-tune the model with the given text.
Args
----
- `text`: Text to fine-tune the model.
- `fine_tune`: Fine-tune value. Can be "Funny", "More detailed", etc.
Returns
-------
- `str`: Fine-tuned response text.
"""
# Structure the prompt
prompt = f"""Fine tune the following text to make it: '{fine_tune}'
Input text:
{text}
"""
print(f"⌨️ Prompt:\n{prompt}")
# Return response from gemini model
return preprocess(get_response(prompt))
if __name__ == "__main__":
def test_handler():
celebrity = "Marcus Aurelius"
say_what = "Thoughts on the modern world & Gen Z"
model_parameters = {
"temperature": 0.9,
"top_k": 32,
"top_p": 1.0
}
target_languages = ["Marathi", "Hindi", "Japanese", "Gujarati"]
audio_paths = [
"./static/audio/marcus_aurelius_marathi.mp3",
"./static/audio/marcus_aurelius_hindi.mp3",
"./static/audio/marcus_aurelius_japanese.mp3",
"./static/audio/marcus_aurelius_gujarati.mp3"
]
for target_language, audio_path in zip(target_languages, audio_paths):
response, audio_path = handler(
celebrity, say_what, model_parameters, target_language, audio_path)
print(f"Response ({target_language}): {response}")
print(f"Audio path ({target_language}): {audio_path}")
# Save to markdown file
with open(f"response_{target_language}.md", "w", encoding="utf-8") as f:
f.write(f"# Trying gemini model\n\n")
f.write(f"## Input\n\n")
f.write(f"- **Celebrity**: {celebrity}\n")
f.write(f"- **Say what**: {say_what}\n")
f.write(f"- **Model parameters**: {model_parameters}\n")
f.write(f"- **Target language**: {target_language}\n")
f.write(f"- **Audio path**: {audio_path}\n\n")
f.write(f"## Response\n\n")
f.write(f"{response}\n\n")
f.write(f"## Audio path\n\n")
f.write(f"[Audio file]({audio_path})\n\n")
print(f"Saved to response_{target_language}.md")
test_handler()