Spaces:
Running
Running
sanbo commited on
Commit ·
be6b454
1
Parent(s): c93f60c
update sth. at 2024-11-26 19:27:55
Browse files- pdf2zh/translator.py +10 -6
pdf2zh/translator.py
CHANGED
|
@@ -259,13 +259,16 @@ class OpenAITranslator(BaseTranslator):
|
|
| 259 |
self.options = {"temperature": 0} # 随机采样可能会打断公式标记
|
| 260 |
# OPENAI_BASE_URL
|
| 261 |
# OPENAI_API_KEY
|
| 262 |
-
self.client = openai.OpenAI()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
def translate(self, text) -> str:
|
| 265 |
-
|
| 266 |
-
model
|
| 267 |
-
|
| 268 |
-
messages=[
|
| 269 |
{
|
| 270 |
"role": "system",
|
| 271 |
"content": "You are a professional,authentic machine translation engine.",
|
|
@@ -275,7 +278,8 @@ class OpenAITranslator(BaseTranslator):
|
|
| 275 |
"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
|
| 276 |
},
|
| 277 |
],
|
| 278 |
-
|
|
|
|
| 279 |
return response.choices[0].message.content.strip()
|
| 280 |
|
| 281 |
|
|
|
|
| 259 |
self.options = {"temperature": 0} # 随机采样可能会打断公式标记
|
| 260 |
# OPENAI_BASE_URL
|
| 261 |
# OPENAI_API_KEY
|
| 262 |
+
# self.client = openai.OpenAI()
|
| 263 |
+
self.api_url = "https://sanbo1200-duck2api.hf.space/completions" # 自定义API的URL
|
| 264 |
+
self.headers = {
|
| 265 |
+
"Content-Type": "application/json"
|
| 266 |
+
}
|
| 267 |
|
| 268 |
def translate(self, text) -> str:
|
| 269 |
+
data = {
|
| 270 |
+
"model": self.model,
|
| 271 |
+
"messages": [
|
|
|
|
| 272 |
{
|
| 273 |
"role": "system",
|
| 274 |
"content": "You are a professional,authentic machine translation engine.",
|
|
|
|
| 278 |
"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
|
| 279 |
},
|
| 280 |
],
|
| 281 |
+
"stream": False
|
| 282 |
+
}
|
| 283 |
return response.choices[0].message.content.strip()
|
| 284 |
|
| 285 |
|