Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,21 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from gradio_client import Client
|
| 3 |
import os
|
|
|
|
| 4 |
import logging
|
|
|
|
|
|
|
|
|
|
| 5 |
import requests
|
| 6 |
-
import json
|
| 7 |
from datetime import datetime
|
| 8 |
|
| 9 |
# 로깅 설정
|
| 10 |
logging.basicConfig(level=logging.INFO)
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# API 클라이언트 설정
|
| 13 |
api_client = Client("http://211.233.58.202:7960/")
|
| 14 |
|
|
@@ -17,64 +24,37 @@ NOTION_API_KEY = "secret_MpVfJphbfo4599fdczYfMYKNOpyzCcvkhhzk3lgTfVk"
|
|
| 17 |
NOTION_DATABASE_ID = "88c9bdad-cb20-4412-9af7-7d5932e1a82a"
|
| 18 |
NOTION_API_URL = "https://api.notion.com/v1"
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
"Content-Type": "application/json",
|
| 23 |
-
"Notion-Version": "2022-06-28" # 최신 버전으로 업데이트
|
| 24 |
-
}
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
data = {
|
| 30 |
-
"parent": {"database_id": NOTION_DATABASE_ID},
|
| 31 |
-
"properties": {
|
| 32 |
-
"Name": {
|
| 33 |
-
"title": [
|
| 34 |
-
{
|
| 35 |
-
"text": {
|
| 36 |
-
"content": f"Generated Image - {current_time}"
|
| 37 |
-
}
|
| 38 |
-
}
|
| 39 |
-
]
|
| 40 |
-
},
|
| 41 |
-
"Prompt": {
|
| 42 |
-
"rich_text": [
|
| 43 |
-
{
|
| 44 |
-
"text": {
|
| 45 |
-
"content": prompt
|
| 46 |
-
}
|
| 47 |
-
}
|
| 48 |
-
]
|
| 49 |
-
}
|
| 50 |
-
},
|
| 51 |
-
"children": [
|
| 52 |
-
{
|
| 53 |
-
"object": "block",
|
| 54 |
-
"type": "image",
|
| 55 |
-
"image": {
|
| 56 |
-
"type": "external",
|
| 57 |
-
"external": {
|
| 58 |
-
"url": image_url
|
| 59 |
-
}
|
| 60 |
-
}
|
| 61 |
-
}
|
| 62 |
-
]
|
| 63 |
-
}
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
def respond(message, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 79 |
logging.info(f"Received message: {message}, seed: {seed}, randomize_seed: {randomize_seed}, "
|
| 80 |
f"width: {width}, height: {height}, guidance_scale: {guidance_scale}, "
|
|
@@ -82,7 +62,7 @@ def respond(message, seed, randomize_seed, width, height, guidance_scale, num_in
|
|
| 82 |
|
| 83 |
try:
|
| 84 |
# 이미지 생성 요청
|
| 85 |
-
result =
|
| 86 |
prompt=message,
|
| 87 |
seed=seed,
|
| 88 |
randomize_seed=randomize_seed,
|
|
@@ -97,19 +77,39 @@ def respond(message, seed, randomize_seed, width, height, guidance_scale, num_in
|
|
| 97 |
# 결과 확인 및 처리
|
| 98 |
if isinstance(result, tuple) and len(result) >= 1:
|
| 99 |
image_path = result[0]
|
| 100 |
-
# 이미지 URL 생성 (실제 서버 URL로 변경 필요)
|
| 101 |
image_url = f"http://211.233.58.202:7960/file={image_path}"
|
| 102 |
|
| 103 |
-
# Notion
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
|
|
|
|
| 107 |
else:
|
| 108 |
raise ValueError("Unexpected API response format")
|
| 109 |
except Exception as e:
|
| 110 |
logging.error("Error during API request: %s", str(e))
|
| 111 |
return "Failed to generate image due to an error.", None
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
def use_prompt(prompt):
|
| 114 |
return prompt
|
| 115 |
|
|
@@ -143,11 +143,14 @@ examples = [
|
|
| 143 |
["A fantasy map of a fictional world, with detailed terrain and cities.", "q19.webp"]
|
| 144 |
]
|
| 145 |
|
|
|
|
|
|
|
| 146 |
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
|
|
|
|
|
|
|
| 147 |
with gr.Row():
|
| 148 |
input_text = gr.Textbox(label="Enter your prompt for image generation")
|
| 149 |
output_image = gr.Image(label="Generated Image")
|
| 150 |
-
notion_link = gr.Textbox(label="Notion Page Link")
|
| 151 |
|
| 152 |
with gr.Row():
|
| 153 |
seed = gr.Slider(minimum=0, maximum=1000000, step=1, label="Seed", value=123)
|
|
@@ -179,8 +182,8 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
|
|
| 179 |
input_text.submit(
|
| 180 |
fn=respond,
|
| 181 |
inputs=[input_text, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 182 |
-
outputs=
|
| 183 |
)
|
| 184 |
|
| 185 |
if __name__ == "__main__":
|
| 186 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
import logging
|
| 4 |
+
from notion_client import Client
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from gradio_client import Client as GradioClient
|
| 7 |
import requests
|
|
|
|
| 8 |
from datetime import datetime
|
| 9 |
|
| 10 |
# 로깅 설정
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
| 12 |
|
| 13 |
+
# 환경 변수 로드
|
| 14 |
+
load_dotenv()
|
| 15 |
+
|
| 16 |
+
# 로깅 설정
|
| 17 |
+
logging.basicConfig(level=logging.INFO)
|
| 18 |
+
|
| 19 |
# API 클라이언트 설정
|
| 20 |
api_client = Client("http://211.233.58.202:7960/")
|
| 21 |
|
|
|
|
| 24 |
NOTION_DATABASE_ID = "88c9bdad-cb20-4412-9af7-7d5932e1a82a"
|
| 25 |
NOTION_API_URL = "https://api.notion.com/v1"
|
| 26 |
|
| 27 |
+
# Notion 클라이언트 초기화
|
| 28 |
+
notion = Client(auth=NOTION_API_KEY)
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Gradio API 클라이언트 설정
|
| 31 |
+
gradio_client = GradioClient("http://211.233.58.202:7960/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
def create_notion_database():
|
| 34 |
+
new_database = notion.databases.create(
|
| 35 |
+
parent={"type": "page_id", "page_id": NOTION_PAGE_ID},
|
| 36 |
+
title=[{"type": "text", "text": {"content": "Generated Images"}}],
|
| 37 |
+
properties={
|
| 38 |
+
"Name": {"title": {}},
|
| 39 |
+
"Prompt": {"rich_text": {}},
|
| 40 |
+
"Image": {"url": {}},
|
| 41 |
+
"Created At": {"date": {}}
|
| 42 |
+
}
|
| 43 |
+
)
|
| 44 |
+
return new_database.id
|
| 45 |
+
|
| 46 |
+
def add_page_to_notion_database(database_id, name, prompt, image_url):
|
| 47 |
+
new_page = notion.pages.create(
|
| 48 |
+
parent={"database_id": database_id},
|
| 49 |
+
properties={
|
| 50 |
+
"Name": {"title": [{"text": {"content": name}}]},
|
| 51 |
+
"Prompt": {"rich_text": [{"text": {"content": prompt}}]},
|
| 52 |
+
"Image": {"url": image_url},
|
| 53 |
+
"Created At": {"date": {"start": datetime.now().isoformat()}}
|
| 54 |
+
}
|
| 55 |
+
)
|
| 56 |
+
return new_page.url
|
| 57 |
|
|
|
|
| 58 |
def respond(message, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 59 |
logging.info(f"Received message: {message}, seed: {seed}, randomize_seed: {randomize_seed}, "
|
| 60 |
f"width: {width}, height: {height}, guidance_scale: {guidance_scale}, "
|
|
|
|
| 62 |
|
| 63 |
try:
|
| 64 |
# 이미지 생성 요청
|
| 65 |
+
result = gradio_client.predict(
|
| 66 |
prompt=message,
|
| 67 |
seed=seed,
|
| 68 |
randomize_seed=randomize_seed,
|
|
|
|
| 77 |
# 결과 확인 및 처리
|
| 78 |
if isinstance(result, tuple) and len(result) >= 1:
|
| 79 |
image_path = result[0]
|
|
|
|
| 80 |
image_url = f"http://211.233.58.202:7960/file={image_path}"
|
| 81 |
|
| 82 |
+
# Notion 데이터베이스 생성 (이미 존재한다면 기존 ID 사용)
|
| 83 |
+
database_id = os.getenv("NOTION_DATABASE_ID")
|
| 84 |
+
if not database_id:
|
| 85 |
+
database_id = create_notion_database()
|
| 86 |
+
os.environ["NOTION_DATABASE_ID"] = database_id
|
| 87 |
+
|
| 88 |
+
# Notion에 페이지 추가
|
| 89 |
+
page_url = add_page_to_notion_database(
|
| 90 |
+
database_id,
|
| 91 |
+
f"Generated Image - {datetime.now().isoformat()}",
|
| 92 |
+
message,
|
| 93 |
+
image_url
|
| 94 |
+
)
|
| 95 |
|
| 96 |
+
logging.info(f"Added to Notion: {page_url}")
|
| 97 |
+
return image_url, page_url
|
| 98 |
else:
|
| 99 |
raise ValueError("Unexpected API response format")
|
| 100 |
except Exception as e:
|
| 101 |
logging.error("Error during API request: %s", str(e))
|
| 102 |
return "Failed to generate image due to an error.", None
|
| 103 |
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
def use_prompt(prompt):
|
| 114 |
return prompt
|
| 115 |
|
|
|
|
| 143 |
["A fantasy map of a fictional world, with detailed terrain and cities.", "q19.webp"]
|
| 144 |
]
|
| 145 |
|
| 146 |
+
|
| 147 |
+
|
| 148 |
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
|
| 149 |
+
|
| 150 |
+
|
| 151 |
with gr.Row():
|
| 152 |
input_text = gr.Textbox(label="Enter your prompt for image generation")
|
| 153 |
output_image = gr.Image(label="Generated Image")
|
|
|
|
| 154 |
|
| 155 |
with gr.Row():
|
| 156 |
seed = gr.Slider(minimum=0, maximum=1000000, step=1, label="Seed", value=123)
|
|
|
|
| 182 |
input_text.submit(
|
| 183 |
fn=respond,
|
| 184 |
inputs=[input_text, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 185 |
+
outputs=output_image
|
| 186 |
)
|
| 187 |
|
| 188 |
if __name__ == "__main__":
|
| 189 |
+
demo.launch()
|