-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.py
34 lines (29 loc) · 1023 Bytes
/
translate.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
import openai
from PIL import Image
import pytesseract
# Set your OpenAI API key
openai.api_key = 'your-openai-api-key'
def translate_polish_image(image_path):
# Open the image file
with Image.open(image_path) as img:
# Use Tesseract to do OCR on the image
text = pytesseract.image_to_string(img, lang='pol')
# Translate the extracted text using OpenAI's GPT-4 model
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": f"Translate the following Polish text to English:\n\n{text}"}
],
max_tokens=1000,
n=1,
stop=None,
temperature=0.7,
)
# Get the translation from the response
translation = response['choices'][0]['message']['content'].strip()
return translation
# Example usage
image_path = 'path_to_your_image.jpeg'
translation = translate_polish_image(image_path)
print(translation)