File size: 2,114 Bytes
e89c667
 
 
 
 
 
 
1e6ccdc
e89c667
 
 
1e6ccdc
 
e89c667
 
 
 
 
 
 
 
1e6ccdc
 
 
 
 
 
 
 
 
1768ec2
e89c667
 
 
 
1768ec2
e89c667
 
 
 
 
1e6ccdc
e89c667
 
 
 
 
 
 
1e6ccdc
 
 
 
 
 
e89c667
 
1768ec2
 
 
 
 
 
 
e89c667
 
1e6ccdc
 
1768ec2
 
1e6ccdc
e89c667
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import gradio as gr
import requests
import base64
from PIL import Image
from io import BytesIO
from dotenv import load_dotenv
import os
import deepl

load_dotenv()
ENDPOINT_URL = "https://api.runpod.ai/v2/qkqui1t394hjws/runsync"
INFERENCE_API_KEY = os.getenv("INFERENCE_API_KEY")
TRANSLATE_API_KEY = os.getenv("TRANSLATE_API_KEY")

def encode_image_to_base64(image):
  buffered = BytesIO()
  image.save(buffered, format="JPEG")
  img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
  return img_str


def translate_en_to_ko(text):
  translator = deepl.Translator(TRANSLATE_API_KEY)
  try:
      result = translator.translate_text(text, target_lang="KO")
      return result.text
  except deepl.DeepLException as e:
      return f"๋ฒˆ์—ญ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"


def critic_image(image, language, category):
  img_base64 = encode_image_to_base64(image)
  payload = {
      "input": {
          "max_new_tokens": 512,
          "category": category,
          "image": img_base64
      }
  }

  headers = {
      "Authorization": INFERENCE_API_KEY,
      "Content-Type": "application/json"
  }
  

  response = requests.post(ENDPOINT_URL, json=payload, headers=headers)
  result = response.json()

  analysis_result = result['output']['result'].strip()

  if language == "KO":
      return translate_en_to_ko(analysis_result)  # ํ•œ๊ตญ์–ด๋กœ ๋ฒˆ์—ญ ํ›„ ๋ฐ˜ํ™˜
  else:
      return analysis_result  # ์˜์–ด ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜


categories = [
  'General Visual Analysis', 'Form and Shape', 'Symbolism and Iconography', 
  'Composition', 'Color Palette', 'Light and Shadow', 'Texture', 
  'Movement and Gesture', 'Line Quality', 'Perspective', 'Scale and Proportion'
]


demo = gr.Interface(
  fn=critic_image,
  inputs=[
    gr.Image(type="pil"),
    gr.Radio(choices=["EN", "KO"], label="Select Language", value="EN"),
    gr.Dropdown(choices=categories, label="Select Category", value="General Visual Analysis")
  ],
  outputs="text",
  title="Gemmarte",
  description="Upload an image and get a visual analysis in text form from the Gemmarte model." 
)


if __name__ == "__main__":
  demo.launch()