-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslator.py
More file actions
54 lines (40 loc) · 1.84 KB
/
translator.py
File metadata and controls
54 lines (40 loc) · 1.84 KB
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
import google.generativeai as genai
from dotenv import load_dotenv
import os
from gemini_context_manager import GeminiContextManager
load_dotenv()
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)
class Translator:
def __init__(self):
context_manager = GeminiContextManager()
# configure system prompt
context_manager.add_context("user",
"""
This is a system prompt.
You are a professional translator, who was hired to translate various medical documents from various languages to English.
there will be a few mistakes in the input text, due to the OCR process, so try to fix them as much as possible.
return the English translation of the text in the same format as the input.
IMPORTANT: DO NOT CHANGE THE FORMAT OF THE TEXT.
IMPORTANT: ONLY RETURN THE TRANSLATED TEXT. DO NOT RETURN ANYTHING ELSE, BEFORE OR AFTER THE TRANSLATED TEXT.
IMPORTANT: DO NOT RESPOND WITH MARKDOWN STYLE. ALWAYS RETURN BY PLAIN TEXT.
""")
context_manager.add_context("model","""
Certainly, I can help you with that.
""")
self.system_prompt = context_manager.get_context()
def translate(self, text, chat):
chat.history = self.system_prompt
response = chat.send_message(text)
return response.text
def print_history(self):
for message in self.chat.history:
print(f"Role: {message.role}, Text: {message.parts[0].text}")
def main():
translator = Translator()
response = translator.translate("通院履歴:なし, アレルギー:[杉花粉]")
response = translator.translate("通院履歴:あり, アレルギー:[猫]")
translator.print_history()
print(response)
if __name__ == "__main__":
main()