pizb commited on
Commit
1e6ccdc
โ€ข
1 Parent(s): e89c667

feat: add translation option

Browse files
Files changed (2) hide show
  1. demo.py +24 -5
  2. 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
- API_KEY = os.getenv("API_KEY")
 
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 critic_image(image):
 
 
 
 
 
 
 
 
 
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": API_KEY,
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
- return result['output']['result'].strip()
 
 
 
 
 
40
 
41
 
42
  demo = gr.Interface(
43
  fn=critic_image,
44
- inputs=gr.Image(type="pil"),
 
 
 
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
+ }