feat: add translation option
Browse files- demo.py +24 -5
- trans_deepl.ipynb +107 -0
demo.py
CHANGED
@@ -5,10 +5,12 @@ from PIL import Image
|
|
5 |
from io import BytesIO
|
6 |
from dotenv import load_dotenv
|
7 |
import os
|
|
|
8 |
|
9 |
load_dotenv()
|
10 |
ENDPOINT_URL = "https://api.runpod.ai/v2/qkqui1t394hjws/runsync"
|
11 |
-
|
|
|
12 |
|
13 |
def encode_image_to_base64(image):
|
14 |
buffered = BytesIO()
|
@@ -17,7 +19,16 @@ def encode_image_to_base64(image):
|
|
17 |
return img_str
|
18 |
|
19 |
|
20 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
img_base64 = encode_image_to_base64(image)
|
22 |
payload = {
|
23 |
"input": {
|
@@ -28,7 +39,7 @@ def critic_image(image):
|
|
28 |
}
|
29 |
|
30 |
headers = {
|
31 |
-
"Authorization":
|
32 |
"Content-Type": "application/json"
|
33 |
}
|
34 |
|
@@ -36,12 +47,20 @@ def critic_image(image):
|
|
36 |
response = requests.post(ENDPOINT_URL, json=payload, headers=headers)
|
37 |
result = response.json()
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
|
42 |
demo = gr.Interface(
|
43 |
fn=critic_image,
|
44 |
-
inputs=
|
|
|
|
|
|
|
45 |
outputs="text",
|
46 |
title="Gemmarte",
|
47 |
description="Upload an image and get a visual analysis in text form from the Gemmarte model."
|
|
|
5 |
from io import BytesIO
|
6 |
from dotenv import load_dotenv
|
7 |
import os
|
8 |
+
import deepl
|
9 |
|
10 |
load_dotenv()
|
11 |
ENDPOINT_URL = "https://api.runpod.ai/v2/qkqui1t394hjws/runsync"
|
12 |
+
INFERENCE_API_KEY = os.getenv("INFERENCE_API_KEY")
|
13 |
+
TRANSLATE_API_KEY = os.getenv("TRANSLATE_API_KEY")
|
14 |
|
15 |
def encode_image_to_base64(image):
|
16 |
buffered = BytesIO()
|
|
|
19 |
return img_str
|
20 |
|
21 |
|
22 |
+
def translate_en_to_ko(text):
|
23 |
+
translator = deepl.Translator(TRANSLATE_API_KEY)
|
24 |
+
try:
|
25 |
+
result = translator.translate_text(text, target_lang="KO")
|
26 |
+
return result.text
|
27 |
+
except deepl.DeepLException as e:
|
28 |
+
return f"๋ฒ์ญ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
29 |
+
|
30 |
+
|
31 |
+
def critic_image(image, language):
|
32 |
img_base64 = encode_image_to_base64(image)
|
33 |
payload = {
|
34 |
"input": {
|
|
|
39 |
}
|
40 |
|
41 |
headers = {
|
42 |
+
"Authorization": INFERENCE_API_KEY,
|
43 |
"Content-Type": "application/json"
|
44 |
}
|
45 |
|
|
|
47 |
response = requests.post(ENDPOINT_URL, json=payload, headers=headers)
|
48 |
result = response.json()
|
49 |
|
50 |
+
analysis_result = result['output']['result'].strip()
|
51 |
+
|
52 |
+
if language == "KO":
|
53 |
+
return translate_en_to_ko(analysis_result) # ํ๊ตญ์ด๋ก ๋ฒ์ญ ํ ๋ฐํ
|
54 |
+
else:
|
55 |
+
return analysis_result # ์์ด ๊ทธ๋๋ก ๋ฐํ
|
56 |
|
57 |
|
58 |
demo = gr.Interface(
|
59 |
fn=critic_image,
|
60 |
+
inputs=[
|
61 |
+
gr.Image(type="pil"),
|
62 |
+
gr.Radio(choices=["EN", "KO"], label="Select Language", value="EN")
|
63 |
+
],
|
64 |
outputs="text",
|
65 |
title="Gemmarte",
|
66 |
description="Upload an image and get a visual analysis in text form from the Gemmarte model."
|
trans_deepl.ipynb
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [
|
8 |
+
{
|
9 |
+
"name": "stdout",
|
10 |
+
"output_type": "stream",
|
11 |
+
"text": [
|
12 |
+
"์ด๋ฏธ์ง ๋ถ์ ์ค...\n",
|
13 |
+
"\n",
|
14 |
+
"์์ด ๋ถ์ ๊ฒฐ๊ณผ:\n",
|
15 |
+
"\n",
|
16 |
+
"The painting features a couple passionately embracing, set against a rich, gold-leaf background. The figures are depicted in a stylized manner, with elongated bodies and flowing hair. The woman's face is partially obscured by a veil, adding a sense of mystery and intrigue to the composition. The man's face is turned towards the woman, his eyes closed in a moment of intimacy. The couple's bodies are intertwined, creating a sense of unity and connection. The use of gold leaf throughout the painting adds a luxurious and ethereal quality, enhancing the overall romantic atmosphere of the piece. The intricate patterns and textures in the background further emphasize the couple's connection and the overall sense of opulence and beauty in the painting.\n",
|
17 |
+
"\n",
|
18 |
+
"ํ๊ตญ์ด๋ก ๋ฒ์ญ ์ค...\n",
|
19 |
+
"\n",
|
20 |
+
"ํ๊ตญ์ด ๋ฒ์ญ ๊ฒฐ๊ณผ:\n",
|
21 |
+
"\n",
|
22 |
+
"์ด ๊ทธ๋ฆผ์ ํ๋ถํ ๊ธ๋ฐ์ ๋ฐฐ๊ฒฝ์ผ๋ก ์ด์ ์ ์ผ๋ก ํฌ์นํ๋ ๋ถ๋ถ์ ๋ชจ์ต์ ๋ด๊ณ ์์ต๋๋ค. ์ธ๋ฌผ์ ๊ธธ์ญํ ๋ชธ๊ณผ ํ๋ฅด๋ ๋จธ๋ฆฌ์นด๋ฝ์ผ๋ก ์์ํ๋ ๋ฐฉ์์ผ๋ก ๋ฌ์ฌ๋์ด ์์ต๋๋ค. ์ฌ์ฑ์ ์ผ๊ตด์ ๋ฒ ์ผ๋ก ๋ถ๋ถ์ ์ผ๋ก ๊ฐ๋ ค์ ธ ์์ด ๊ตฌ๋์ ์ ๋น๊ฐ๊ณผ ์๋ชจ๋ฅผ ๋ํฉ๋๋ค. ๋จ์์ ์ผ๊ตด์ ์ฌ์ ์ชฝ์ผ๋ก ํฅํ๊ณ ์์ผ๋ฉฐ, ์น๋ฐ๊ฐ์ ๋๋ผ๋ฉฐ ๋์ ๊ฐ๊ณ ์์ต๋๋ค. ๋ ์ฌ๋์ ๋ชธ์ ์๋ก ์ฝํ ์์ด ์ผ์ฒด๊ฐ๊ณผ ์ฐ๊ฒฐ๊ฐ์ ์์๋
๋๋ค. ๊ทธ๋ฆผ ์ ์ฒด์ ๊ธ๋ฐ์ ์ฌ์ฉํ์ฌ ๊ณ ๊ธ์ค๋ฝ๊ณ ๋ฏธ๋ฌํ ๋๋์ ๋ํ์ฌ ์ํ์ ์ ์ฒด์ ์ธ ๋ก๋งจํฑํ ๋ถ์๊ธฐ๋ฅผ ๋์ฑ ๋๋ณด์ด๊ฒ ํฉ๋๋ค. ๋ฐฐ๊ฒฝ์ ๋ณต์กํ ํจํด๊ณผ ์ง๊ฐ์ ๋ถ๋ถ์ ์ฐ๊ฒฐ๊ณผ ๊ทธ๋ฆผ์ ์ ๋ฐ์ ์ธ ํ๋ คํจ๊ณผ ์๋ฆ๋ค์์ ๋์ฑ ๊ฐ์กฐํฉ๋๋ค.\n"
|
23 |
+
]
|
24 |
+
}
|
25 |
+
],
|
26 |
+
"source": [
|
27 |
+
"import requests\n",
|
28 |
+
"import base64\n",
|
29 |
+
"import deepl\n",
|
30 |
+
"\n",
|
31 |
+
"# RunPod API ์ค์ \n",
|
32 |
+
"runpod_url = \"https://api.runpod.ai/v2/qkqui1t394hjws/runsync\"\n",
|
33 |
+
"runpod_headers = {\n",
|
34 |
+
" \"Authorization\": \"C51NZTZUOIPSB3NNECOGREZLBHGTZL13FW9L6U5Y\",\n",
|
35 |
+
" \"Content-Type\": \"application/json\"\n",
|
36 |
+
"}\n",
|
37 |
+
"\n",
|
38 |
+
"# DeepL API ์ค์ \n",
|
39 |
+
"deepl_auth_key = \"fb94505a-9cf7-40cc-95af-8513f31794d1:fx\"\n",
|
40 |
+
"\n",
|
41 |
+
"def encode_image(image_path):\n",
|
42 |
+
" with open(image_path, \"rb\") as image_file:\n",
|
43 |
+
" return base64.b64encode(image_file.read()).decode('utf-8')\n",
|
44 |
+
"\n",
|
45 |
+
"def analyze_image(image_path):\n",
|
46 |
+
" base64_image = encode_image(image_path)\n",
|
47 |
+
" data = {\n",
|
48 |
+
" \"input\": {\n",
|
49 |
+
" \"max_new_tokens\": 512,\n",
|
50 |
+
" \"category\": \"General Visual Analysis\",\n",
|
51 |
+
" \"image\": base64_image\n",
|
52 |
+
" }\n",
|
53 |
+
" }\n",
|
54 |
+
" response = requests.post(runpod_url, headers=runpod_headers, json=data)\n",
|
55 |
+
" if response.status_code == 200:\n",
|
56 |
+
" return response.json()['output']['result']\n",
|
57 |
+
" else:\n",
|
58 |
+
" return f\"์ด๋ฏธ์ง ๋ถ์ ์คํจ. ์ํ ์ฝ๋: {response.status_code}\"\n",
|
59 |
+
"\n",
|
60 |
+
"def translate_en_to_ko(text):\n",
|
61 |
+
" translator = deepl.Translator(deepl_auth_key)\n",
|
62 |
+
" try:\n",
|
63 |
+
" result = translator.translate_text(text, target_lang=\"KO\")\n",
|
64 |
+
" return result.text\n",
|
65 |
+
" except deepl.DeepLException as e:\n",
|
66 |
+
" return f\"๋ฒ์ญ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}\"\n",
|
67 |
+
"\n",
|
68 |
+
"def main():\n",
|
69 |
+
" image_path = r\"C:\\Users\\user\\Desktop\\GemmArte-main\\dataset\\images\\2.png\"\n",
|
70 |
+
" \n",
|
71 |
+
" print(\"์ด๋ฏธ์ง ๋ถ์ ์ค...\")\n",
|
72 |
+
" analysis_result = analyze_image(image_path)\n",
|
73 |
+
" print(\"\\n์์ด ๋ถ์ ๊ฒฐ๊ณผ:\")\n",
|
74 |
+
" print(analysis_result)\n",
|
75 |
+
"\n",
|
76 |
+
" print(\"\\nํ๊ตญ์ด๋ก ๋ฒ์ญ ์ค...\")\n",
|
77 |
+
" korean_result = translate_en_to_ko(analysis_result)\n",
|
78 |
+
" print(\"\\nํ๊ตญ์ด ๋ฒ์ญ ๊ฒฐ๊ณผ:\")\n",
|
79 |
+
" print(korean_result)\n",
|
80 |
+
"\n",
|
81 |
+
"if __name__ == \"__main__\":\n",
|
82 |
+
" main()"
|
83 |
+
]
|
84 |
+
}
|
85 |
+
],
|
86 |
+
"metadata": {
|
87 |
+
"kernelspec": {
|
88 |
+
"display_name": "nlp",
|
89 |
+
"language": "python",
|
90 |
+
"name": "python3"
|
91 |
+
},
|
92 |
+
"language_info": {
|
93 |
+
"codemirror_mode": {
|
94 |
+
"name": "ipython",
|
95 |
+
"version": 3
|
96 |
+
},
|
97 |
+
"file_extension": ".py",
|
98 |
+
"mimetype": "text/x-python",
|
99 |
+
"name": "python",
|
100 |
+
"nbconvert_exporter": "python",
|
101 |
+
"pygments_lexer": "ipython3",
|
102 |
+
"version": "3.11.10"
|
103 |
+
}
|
104 |
+
},
|
105 |
+
"nbformat": 4,
|
106 |
+
"nbformat_minor": 2
|
107 |
+
}
|