Skip to content
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

Open
x18-1 opened this issue Dec 26, 2024 · 1 comment
Open

Enable Grok API #347

x18-1 opened this issue Dec 26, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@x18-1
Copy link

x18-1 commented Dec 26, 2024

希望加入Grok API

@x18-1
Copy link
Author

x18-1 commented Dec 26, 2024

自己写的:

  1. 在pdf2zh.translator中加如:
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"
  1. 在pdf2zh.converty中进行注册
    image

  2. 运行一下测试代码:

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]

@Byaidu Byaidu added the enhancement New feature or request label Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants