fantaxy commited on
Commit
84ba97a
·
verified ·
1 Parent(s): 3396e4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -19
app.py CHANGED
@@ -3,11 +3,10 @@ from gradio_client import Client
3
  import os
4
  import logging
5
  import requests
6
- import pyimgur
7
  from PIL import Image
8
  import io
9
  import tempfile
10
- import shutil
11
 
12
  # 로깅 설정
13
  logging.basicConfig(level=logging.INFO)
@@ -20,20 +19,21 @@ WEBHOOK_URL = "https://connect.pabbly.com/workflow/sendwebhookdata/IjU3NjUwNTY0M
20
 
21
  # Imgur 클라이언트 ID
22
  IMGUR_CLIENT_ID = "여기에_당신의_IMGUR_CLIENT_ID를_입력하세요"
23
- imgur_client = pyimgur.Imgur(IMGUR_CLIENT_ID)
24
-
25
- def convert_to_png(image_path):
26
- with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as temp_file:
27
- with Image.open(image_path) as img:
28
- img.save(temp_file.name, 'PNG')
29
- return temp_file.name
30
 
31
  def upload_to_imgur(image_path):
32
  try:
33
- png_path = convert_to_png(image_path)
34
- uploaded_image = imgur_client.upload_image(png_path, title="Generated Image")
35
- os.remove(png_path) # 임시 PNG 파일 삭제
36
- return uploaded_image.link
 
 
 
 
 
 
 
 
37
  except Exception as e:
38
  logging.error(f"Failed to upload image to Imgur: {e}")
39
  return None
@@ -72,14 +72,9 @@ def respond(message, seed, randomize_seed, width, height, guidance_scale, num_in
72
  # 결과 확인 및 처리
73
  if isinstance(result, tuple) and len(result) >= 1:
74
  local_image_path = result[0]
75
- # 임시 디렉토리에 이미지 복사
76
- with tempfile.NamedTemporaryFile(suffix='.webp', delete=False) as temp_file:
77
- shutil.copy2(local_image_path, temp_file.name)
78
- temp_image_path = temp_file.name
79
 
80
  # Imgur에 이미지 업로드
81
- imgur_url = upload_to_imgur(temp_image_path)
82
- os.remove(temp_image_path) # 임시 파일 삭제
83
 
84
  if imgur_url:
85
  # Webhook으로 데이터 전송
@@ -94,6 +89,7 @@ def respond(message, seed, randomize_seed, width, height, guidance_scale, num_in
94
  return "Failed to generate image due to an error."
95
 
96
 
 
97
  css = """
98
  footer {
99
  visibility: hidden;
 
3
  import os
4
  import logging
5
  import requests
 
6
  from PIL import Image
7
  import io
8
  import tempfile
9
+ import base64
10
 
11
  # 로깅 설정
12
  logging.basicConfig(level=logging.INFO)
 
19
 
20
  # Imgur 클라이언트 ID
21
  IMGUR_CLIENT_ID = "여기에_당신의_IMGUR_CLIENT_ID를_입력하세요"
 
 
 
 
 
 
 
22
 
23
  def upload_to_imgur(image_path):
24
  try:
25
+ with open(image_path, "rb") as file:
26
+ im_bytes = file.read()
27
+ im_b64 = base64.b64encode(im_bytes).decode("utf-8")
28
+
29
+ url = "https://api.imgur.com/3/image"
30
+ payload = {'image': im_b64, 'type': 'base64'}
31
+ headers = {'Authorization': f'Client-ID {IMGUR_CLIENT_ID}'}
32
+
33
+ response = requests.post(url, headers=headers, data=payload)
34
+ response.raise_for_status()
35
+
36
+ return response.json()['data']['link']
37
  except Exception as e:
38
  logging.error(f"Failed to upload image to Imgur: {e}")
39
  return None
 
72
  # 결과 확인 및 처리
73
  if isinstance(result, tuple) and len(result) >= 1:
74
  local_image_path = result[0]
 
 
 
 
75
 
76
  # Imgur에 이미지 업로드
77
+ imgur_url = upload_to_imgur(local_image_path)
 
78
 
79
  if imgur_url:
80
  # Webhook으로 데이터 전송
 
89
  return "Failed to generate image due to an error."
90
 
91
 
92
+
93
  css = """
94
  footer {
95
  visibility: hidden;