Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,8 +11,21 @@ import sys
|
|
| 11 |
|
| 12 |
# 로깅 설정
|
| 13 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', stream=sys.stdout)
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# API 클라이언트 설정
|
| 18 |
api_client = Client("http://211.233.58.202:7960/")
|
|
@@ -22,43 +35,35 @@ NOTION_TOKEN = os.getenv("NOTION_TOKEN")
|
|
| 22 |
NOTION_DATABASE_ID = "88c9bdadcb2044129af77d5932e1a82a"
|
| 23 |
|
| 24 |
def test_notion_connection():
|
|
|
|
| 25 |
if not NOTION_TOKEN:
|
| 26 |
-
|
| 27 |
return False
|
| 28 |
|
| 29 |
notion = NotionClient(auth=NOTION_TOKEN)
|
| 30 |
try:
|
| 31 |
-
|
| 32 |
user_info = notion.users.me()
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
db_info = notion.databases.retrieve(database_id=NOTION_DATABASE_ID)
|
| 37 |
-
|
| 38 |
|
| 39 |
return True
|
| 40 |
except Exception as e:
|
| 41 |
-
|
| 42 |
if hasattr(e, 'response'):
|
| 43 |
-
|
| 44 |
return False
|
| 45 |
|
| 46 |
-
# Notion 연결 테스트 실행
|
| 47 |
-
notion_connection_successful = test_notion_connection()
|
| 48 |
-
|
| 49 |
-
if notion_connection_successful:
|
| 50 |
-
notion = NotionClient(auth=NOTION_TOKEN)
|
| 51 |
-
else:
|
| 52 |
-
notion_logger.error("Failed to connect to Notion. The application will continue without Notion integration.")
|
| 53 |
-
notion = None
|
| 54 |
-
|
| 55 |
def add_to_notion(prompt, image_path):
|
| 56 |
if not notion:
|
| 57 |
-
|
| 58 |
return
|
| 59 |
|
| 60 |
try:
|
| 61 |
-
|
| 62 |
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 63 |
|
| 64 |
new_page = {
|
|
@@ -66,17 +71,17 @@ def add_to_notion(prompt, image_path):
|
|
| 66 |
"Image": {"url": f"http://211.233.58.202:7960/file={image_path}"},
|
| 67 |
"Date": {"date": {"start": now}}
|
| 68 |
}
|
| 69 |
-
|
| 70 |
|
| 71 |
response = notion.pages.create(parent={"database_id": NOTION_DATABASE_ID}, properties=new_page)
|
| 72 |
-
|
| 73 |
except Exception as e:
|
| 74 |
-
|
| 75 |
if hasattr(e, 'response'):
|
| 76 |
-
|
| 77 |
|
| 78 |
def respond(message, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 79 |
-
|
| 80 |
f"width: {width}, height: {height}, guidance_scale: {guidance_scale}, "
|
| 81 |
f"num_inference_steps: {num_inference_steps}")
|
| 82 |
|
|
@@ -92,7 +97,7 @@ def respond(message, seed, randomize_seed, width, height, guidance_scale, num_in
|
|
| 92 |
num_inference_steps=num_inference_steps,
|
| 93 |
api_name="/infer_t2i"
|
| 94 |
)
|
| 95 |
-
|
| 96 |
|
| 97 |
# 결과 확인 및 처리
|
| 98 |
if isinstance(result, tuple) and len(result) >= 1:
|
|
@@ -103,11 +108,12 @@ def respond(message, seed, randomize_seed, width, height, guidance_scale, num_in
|
|
| 103 |
else:
|
| 104 |
raise ValueError("Unexpected API response format")
|
| 105 |
except Exception as e:
|
| 106 |
-
|
| 107 |
return "Failed to generate image due to an error."
|
| 108 |
|
| 109 |
|
| 110 |
|
|
|
|
| 111 |
css = """
|
| 112 |
footer {
|
| 113 |
visibility: hidden;
|
|
@@ -177,11 +183,20 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
|
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
-
|
| 181 |
# ... (나머지 코드는 그대로 유지)
|
| 182 |
|
| 183 |
if __name__ == "__main__":
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
demo.launch()
|
|
|
|
| 11 |
|
| 12 |
# 로깅 설정
|
| 13 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', stream=sys.stdout)
|
| 14 |
+
logger = logging.getLogger(__name__)
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
import gradio as gr
|
| 18 |
+
from gradio_client import Client
|
| 19 |
+
import os
|
| 20 |
+
from notion_client import Client as NotionClient
|
| 21 |
+
import requests
|
| 22 |
+
from datetime import datetime
|
| 23 |
+
from PIL import Image
|
| 24 |
+
import io
|
| 25 |
+
logger.info("All modules imported successfully")
|
| 26 |
+
except ImportError as e:
|
| 27 |
+
logger.error(f"Error importing modules: {e}")
|
| 28 |
+
sys.exit(1)
|
| 29 |
|
| 30 |
# API 클라이언트 설정
|
| 31 |
api_client = Client("http://211.233.58.202:7960/")
|
|
|
|
| 35 |
NOTION_DATABASE_ID = "88c9bdadcb2044129af77d5932e1a82a"
|
| 36 |
|
| 37 |
def test_notion_connection():
|
| 38 |
+
logger.info("Starting Notion connection test")
|
| 39 |
if not NOTION_TOKEN:
|
| 40 |
+
logger.error("NOTION_TOKEN is not set. Please set the environment variable.")
|
| 41 |
return False
|
| 42 |
|
| 43 |
notion = NotionClient(auth=NOTION_TOKEN)
|
| 44 |
try:
|
| 45 |
+
logger.debug("Testing Notion connection...")
|
| 46 |
user_info = notion.users.me()
|
| 47 |
+
logger.info(f"Successfully connected to Notion. User info: {user_info}")
|
| 48 |
|
| 49 |
+
logger.debug("Retrieving database info...")
|
| 50 |
db_info = notion.databases.retrieve(database_id=NOTION_DATABASE_ID)
|
| 51 |
+
logger.info(f"Successfully retrieved database info: {db_info}")
|
| 52 |
|
| 53 |
return True
|
| 54 |
except Exception as e:
|
| 55 |
+
logger.error(f"Error connecting to Notion: {str(e)}")
|
| 56 |
if hasattr(e, 'response'):
|
| 57 |
+
logger.error(f"Notion API response: {e.response.text}")
|
| 58 |
return False
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
def add_to_notion(prompt, image_path):
|
| 61 |
if not notion:
|
| 62 |
+
logger.warning("Notion integration is not available. Skipping add_to_notion.")
|
| 63 |
return
|
| 64 |
|
| 65 |
try:
|
| 66 |
+
logger.debug(f"Adding to Notion - Prompt: {prompt}, Image Path: {image_path}")
|
| 67 |
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 68 |
|
| 69 |
new_page = {
|
|
|
|
| 71 |
"Image": {"url": f"http://211.233.58.202:7960/file={image_path}"},
|
| 72 |
"Date": {"date": {"start": now}}
|
| 73 |
}
|
| 74 |
+
logger.debug(f"New page properties: {new_page}")
|
| 75 |
|
| 76 |
response = notion.pages.create(parent={"database_id": NOTION_DATABASE_ID}, properties=new_page)
|
| 77 |
+
logger.info(f"Successfully added to Notion: {response}")
|
| 78 |
except Exception as e:
|
| 79 |
+
logger.error(f"Error adding to Notion: {str(e)}")
|
| 80 |
if hasattr(e, 'response'):
|
| 81 |
+
logger.error(f"Notion API response: {e.response.text}")
|
| 82 |
|
| 83 |
def respond(message, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 84 |
+
logger.info(f"Received message: {message}, seed: {seed}, randomize_seed: {randomize_seed}, "
|
| 85 |
f"width: {width}, height: {height}, guidance_scale: {guidance_scale}, "
|
| 86 |
f"num_inference_steps: {num_inference_steps}")
|
| 87 |
|
|
|
|
| 97 |
num_inference_steps=num_inference_steps,
|
| 98 |
api_name="/infer_t2i"
|
| 99 |
)
|
| 100 |
+
logger.info("API response received: %s", result)
|
| 101 |
|
| 102 |
# 결과 확인 및 처리
|
| 103 |
if isinstance(result, tuple) and len(result) >= 1:
|
|
|
|
| 108 |
else:
|
| 109 |
raise ValueError("Unexpected API response format")
|
| 110 |
except Exception as e:
|
| 111 |
+
logger.error("Error during API request: %s", str(e))
|
| 112 |
return "Failed to generate image due to an error."
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
+
|
| 117 |
css = """
|
| 118 |
footer {
|
| 119 |
visibility: hidden;
|
|
|
|
| 183 |
|
| 184 |
|
| 185 |
|
|
|
|
| 186 |
# ... (나머지 코드는 그대로 유지)
|
| 187 |
|
| 188 |
if __name__ == "__main__":
|
| 189 |
+
logger.info("Starting application")
|
| 190 |
+
logger.info(f"Notion Database ID: {NOTION_DATABASE_ID}")
|
| 191 |
+
logger.info(f"Notion Token (first 5 chars): {NOTION_TOKEN[:5] if NOTION_TOKEN else 'Not set'}")
|
| 192 |
+
|
| 193 |
+
notion_connection_successful = test_notion_connection()
|
| 194 |
+
|
| 195 |
+
if notion_connection_successful:
|
| 196 |
+
logger.info("Notion connection test passed. Starting Gradio interface...")
|
| 197 |
+
notion = NotionClient(auth=NOTION_TOKEN)
|
| 198 |
+
else:
|
| 199 |
+
logger.error("Failed to connect to Notion. The application will continue without Notion integration.")
|
| 200 |
+
notion = None
|
| 201 |
|
| 202 |
demo.launch()
|