-
-
Notifications
You must be signed in to change notification settings - Fork 876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable Grok API #347
Labels
enhancement
New feature or request
Comments
自己写的:
class GrokTranslator(BaseTranslator):
name = "grok"
envs = {
"GROK_BASE_URL": "https://api.x.ai/v1/chat/completions",
"GROK_API_KEY": None,
"GROK_MODEL": "grok-beta",
}
def __init__(self, lang_in, lang_out, model):
if not model:
model = os.getenv("GROK_MODEL", self.envs["GROK_MODEL"])
super().__init__(lang_in, lang_out, model)
self.api_key = os.getenv("GROK_API_KEY", self.envs["GROK_API_KEY"])
self.endpoint = os.getenv("GROK_BASE_URL", self.envs["GROK_BASE_URL"])
if not self.api_key:
raise ValueError("GROK_API_KEY environment variable is not set")
self.session = requests.Session()
self.session.headers.update({
'Authorization': f'Bearer {self.api_key}',
'Content-Type': 'application/json'
})
def prompt(self, text):
return [
{
"role": "system",
"content": "You are a professional machine translation engine specialized in translating deep learning and machine learning academic papers. You should maintain high accuracy in technical terms translation.",
},
{
"role": "user",
"content": f"Translate the following markdown source text to {self.lang_out}. Keep the formula notation {{v*}} unchanged. Output translation directly without any additional text.\nSource Text: {text}\nTranslated Text:", # noqa: E501
},
]
def translate(self, text):
data = {
"messages": self.prompt(text),
"model": self.model,
"stream": False,
"temperature": 0
}
response = self.session.post(self.endpoint, json=data)
if response.status_code == 200:
return response.json()['choices'][0]['message']['content'].strip()
else:
logging.error(f"请求失败,状态码: {response.status_code}")
logging.error(response.text)
return "TRANSLATION ERROR" from pdf2zh import translate
from pdf2zh.translator import GrokTranslator
service = "grok"
import os
# 设置代理7890
os.environ["http_proxy"] = "http://127.0.0.1:7890"
os.environ["https_proxy"] = "http://127.0.0.1:7890"
os.environ["GROK_API_KEY"] = ""
# 设置最后几页不翻译
# 获取pdf页数
import PyPDF2
pdf_reader = PyPDF2.PdfReader(r"d:\DeskTop\transunet.pdf")
page_count = len(pdf_reader.pages)
# 设置最后几页不翻译
not_translated_pages = 1
pages = [i for i in range(page_count - not_translated_pages)]
params = {"lang_in": "en", "lang_out": "zh", "service": service, "thread": 2, "pages": pages}
file_mono, file_dual = translate(files=[r"d:\DeskTop\transunet.pdf"], **params)[0] |
兼容openai api,可以直接使用openai那个方法。 |
我一开始就是这样做的,但是总是报错,之前尝试GPT-academic的时候也是不行。但是使用沉浸式翻译的时候就可以。 |
|
This was referenced Dec 30, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
希望加入Grok API
The text was updated successfully, but these errors were encountered: