-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrans.py
77 lines (68 loc) · 2.36 KB
/
trans.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
from flask import Flask, request
import requests
import json
import re
app = Flask(__name__)
@app.route('/translate', methods=['get'])
def translate():
content = request.args.get('text')
api_key = "AIzaSyAj1k9TCdLF2PL3Gz6lFGTYSZwLptUE9ig"
# Set the HTTP proxy
proxies = {
'http': 'http://127.0.0.1:7890',
'https': 'http://127.0.0.1:7890',
}
Language="zh"
# Set the request body
request_body = {
"contents": [
{
"parts": [
{
"text":"You are a professional,authentic translation engine,only returns translations.\nFor example:\n<Start>\nHello <Keep This Symbol>\nWorld <Keep This Symbol>\n<End>\nThe translation is:\n<Start>\n你好<Keep This Symbol>\n世界<Keep This Symbol>\n<End>\n\nTranslate the content to "+Language+" Language:\n\n<Start>"+content+"<End>"
}
]
}
],
"generationConfig": {
"temperature": 0.9,
"topK": 1,
"topP": 1,
"maxOutputTokens": 2048,
"stopSequences": []
},
"safetySettings": [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_ONLY_HIGH"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_ONLY_HIGH"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_ONLY_HIGH"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_ONLY_HIGH"
}
]
}
# Make the request
response = requests.post(
"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.0-pro:generateContent?key=" + api_key,
headers={"Content-Type": "application/json"},
json=request_body,
proxies=proxies,
)
# Get the response data
response_data = response.json()
# Print the translated text
trans=response_data["candidates"][0]["content"]["parts"][0]["text"]
trans = re.sub(r"<Start>|<End>", "", trans)
# Perform translation here
return trans
if __name__ == '__main__':
app.run(host='127.0.0.1', port=5000)