fantaxy commited on
Commit
b89f79c
ยท
verified ยท
1 Parent(s): 4614879

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -35
app.py CHANGED
@@ -3,35 +3,73 @@ from gradio_client import Client
3
  import os
4
  import logging
5
  import requests
6
- import base64
 
 
7
  # ๋กœ๊น… ์„ค์ •
8
  logging.basicConfig(level=logging.INFO)
9
 
10
  # API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
11
  api_client = Client("http://211.233.58.202:7960/")
12
 
13
- # Zapier ์›นํ›… URL
14
- WEBHOOK_URL = "https://hooks.zapier.com/hooks/catch/14523965/264pyhj/"
 
 
 
 
 
 
 
 
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- def download_image(image_url):
18
- response = requests.get(image_url)
19
  if response.status_code == 200:
20
- return response.content
 
21
  else:
22
- raise Exception(f"Failed to download image: {response.status_code}")
23
-
24
- def send_to_webhook(prompt, image_content):
25
- payload = {
26
- "prompt": prompt,
27
- "image": base64.b64encode(image_content).decode('utf-8')
28
- }
29
- try:
30
- response = requests.post(WEBHOOK_URL, json=payload)
31
- response.raise_for_status()
32
- logging.info(f"Successfully sent data to webhook. Status code: {response.status_code}")
33
- except requests.exceptions.RequestException as e:
34
- logging.error(f"Failed to send data to webhook: {e}")
35
 
36
  def respond(message, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
37
  logging.info(f"Received message: {message}, seed: {seed}, randomize_seed: {randomize_seed}, "
@@ -58,18 +96,18 @@ def respond(message, seed, randomize_seed, width, height, guidance_scale, num_in
58
  # ์ด๋ฏธ์ง€ URL ์ƒ์„ฑ (์‹ค์ œ ์„œ๋ฒ„ URL๋กœ ๋ณ€๊ฒฝ ํ•„์š”)
59
  image_url = f"http://211.233.58.202:7960/file={image_path}"
60
 
61
- # ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ
62
- image_content = download_image(image_url)
63
-
64
- # ์›นํ›…์œผ๋กœ ๋ฐ์ดํ„ฐ ์ „์†ก
65
- send_to_webhook(message, image_content)
66
 
67
- return image_url
68
  else:
69
  raise ValueError("Unexpected API response format")
70
  except Exception as e:
71
  logging.error("Error during API request: %s", str(e))
72
- return "Failed to generate image due to an error."
 
 
 
73
 
74
  css = """
75
  footer {
@@ -77,8 +115,6 @@ footer {
77
  }
78
  """
79
 
80
-
81
-
82
  # ์ด๋ฏธ์ง€ ์ƒ์„ฑ์„ ์œ„ํ•œ ์˜ˆ์ œ ํ”„๋กฌํ”„ํŠธ
83
  examples = [
84
  ["A glamorous young woman with long, wavy blonde hair and smokey eye makeup, posing in a luxury hotel room. Sheโ€™s wearing a sparkly gold cocktail dress and holding up a white card with 'openfree.ai' written on it in elegant calligraphy. Soft, warm lighting creates a luxurious atmosphere. ", "q1.webp"],
@@ -103,15 +139,11 @@ examples = [
103
  ["A fantasy map of a fictional world, with detailed terrain and cities.", "q19.webp"]
104
  ]
105
 
106
- def use_prompt(prompt):
107
- return prompt
108
-
109
  with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
110
-
111
-
112
  with gr.Row():
113
  input_text = gr.Textbox(label="Enter your prompt for image generation")
114
  output_image = gr.Image(label="Generated Image")
 
115
 
116
  with gr.Row():
117
  seed = gr.Slider(minimum=0, maximum=1000000, step=1, label="Seed", value=123)
@@ -143,8 +175,8 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
143
  input_text.submit(
144
  fn=respond,
145
  inputs=[input_text, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
146
- outputs=output_image
147
  )
148
 
149
  if __name__ == "__main__":
150
- demo.launch()
 
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
 
15
+ # Notion API ์„ค์ •
16
+ NOTION_API_KEY = "secret_MpVfJphbfo4599fdczYfMYKNOpyzCcvkhhzk3lgTfVk"
17
+ NOTION_DATABASE_ID = "88c9bdadcb2044129af77d5932e1a82a"
18
+ NOTION_API_URL = "https://api.notion.com/v1"
19
+
20
+ headers = {
21
+ "Authorization": f"Bearer {NOTION_API_KEY}",
22
+ "Content-Type": "application/json",
23
+ "Notion-Version": "2022-06-28"
24
+ }
25
 
26
+ def upload_to_notion(prompt, image_url):
27
+ current_time = datetime.now().isoformat()
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
+ response = requests.post(f"{NOTION_API_URL}/pages", headers=headers, data=json.dumps(data))
66
+
67
  if response.status_code == 200:
68
+ logging.info("Successfully added to Notion")
69
+ return response.json()["url"]
70
  else:
71
+ logging.error(f"Failed to add to Notion: {response.status_code}, {response.text}")
72
+ raise Exception("Failed to add to Notion")
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  def respond(message, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
75
  logging.info(f"Received message: {message}, seed: {seed}, randomize_seed: {randomize_seed}, "
 
96
  # ์ด๋ฏธ์ง€ URL ์ƒ์„ฑ (์‹ค์ œ ์„œ๋ฒ„ URL๋กœ ๋ณ€๊ฒฝ ํ•„์š”)
97
  image_url = f"http://211.233.58.202:7960/file={image_path}"
98
 
99
+ # Notion์— ์—…๋กœ๋“œ
100
+ notion_url = upload_to_notion(message, image_url)
 
 
 
101
 
102
+ return image_url, notion_url
103
  else:
104
  raise ValueError("Unexpected API response format")
105
  except Exception as e:
106
  logging.error("Error during API request: %s", str(e))
107
+ return "Failed to generate image due to an error.", None
108
+
109
+ def use_prompt(prompt):
110
+ return prompt
111
 
112
  css = """
113
  footer {
 
115
  }
116
  """
117
 
 
 
118
  # ์ด๋ฏธ์ง€ ์ƒ์„ฑ์„ ์œ„ํ•œ ์˜ˆ์ œ ํ”„๋กฌํ”„ํŠธ
119
  examples = [
120
  ["A glamorous young woman with long, wavy blonde hair and smokey eye makeup, posing in a luxury hotel room. Sheโ€™s wearing a sparkly gold cocktail dress and holding up a white card with 'openfree.ai' written on it in elegant calligraphy. Soft, warm lighting creates a luxurious atmosphere. ", "q1.webp"],
 
139
  ["A fantasy map of a fictional world, with detailed terrain and cities.", "q19.webp"]
140
  ]
141
 
 
 
 
142
  with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
 
 
143
  with gr.Row():
144
  input_text = gr.Textbox(label="Enter your prompt for image generation")
145
  output_image = gr.Image(label="Generated Image")
146
+ notion_link = gr.Textbox(label="Notion Page Link")
147
 
148
  with gr.Row():
149
  seed = gr.Slider(minimum=0, maximum=1000000, step=1, label="Seed", value=123)
 
175
  input_text.submit(
176
  fn=respond,
177
  inputs=[input_text, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
178
+ outputs=[output_image, notion_link]
179
  )
180
 
181
  if __name__ == "__main__":
182
+ demo.launch(server_name="0.0.0.0", server_port=7860)