Upload folder using huggingface_hub
Browse files- .DS_Store +0 -0
- README.md +23 -8
- app.py +103 -0
- img/cca.step1./350/256/244/347/237/245/350/256/241/347/256/227/345/271/277/345/221/212.png +0 -0
- img/cca.step2./350/256/244/347/237/245/350/256/241/347/256/227/345/271/277/345/221/212.png +0 -0
- img/cca.step3./350/256/244/347/237/245/350/256/241/347/256/227/345/271/277/345/221/212.png +0 -0
- img/cca.step4./350/256/244/347/237/245/350/256/241/347/256/227/345/271/277/345/221/212.png +0 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
README.md
CHANGED
@@ -1,12 +1,27 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji: 🦀
|
4 |
-
colorFrom: indigo
|
5 |
-
colorTo: yellow
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 4.40.0
|
8 |
app_file: app.py
|
9 |
-
|
|
|
10 |
---
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Suanfamama_Cognitive_Computational_Advertising
|
|
|
|
|
|
|
|
|
|
|
3 |
app_file: app.py
|
4 |
+
sdk: gradio
|
5 |
+
sdk_version: 4.29.0
|
6 |
---
|
7 |
|
8 |
+
# 基于 gradio & Huggingface的 认知计算广告创作 算法
|
9 |
+
## 核心算法创作步骤
|
10 |
+
1. 步骤一:认知
|
11 |
+
* 对广告主要有深刻认知,如需求,预算及折扣等
|
12 |
+
2. 步骤二:计算 - 编写脚本和分镜
|
13 |
+
![](./img/cca.step1.认知计算广告.png)
|
14 |
+
3. 步骤三:计算 - 生成关键帧、修图、确认关键帧
|
15 |
+
![](./img/cca.step2.认知计算广告.png)
|
16 |
+
4. 步骤四:计算 - AI拍摄
|
17 |
+
![](./img/cca.step3.认知计算广告.png)
|
18 |
+
5. 步骤五:计算 - 配音、剪切及后期
|
19 |
+
![](./img/cca.step4.认知计算广告.png)
|
20 |
+
6. 步骤六:广告
|
21 |
+
* 人工审阅及智能投放
|
22 |
+
|
23 |
+
## 技术路线
|
24 |
+
* 使用 python gradio lib 进行AI应用构建
|
25 |
+
|
26 |
+
## How to Deploy
|
27 |
+
* gradio deploy
|
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from io import BytesIO
|
3 |
+
import IPython
|
4 |
+
import json
|
5 |
+
import os
|
6 |
+
from PIL import Image
|
7 |
+
import requests
|
8 |
+
import time
|
9 |
+
import getpass
|
10 |
+
|
11 |
+
# Get the API key
|
12 |
+
STABILITY_API_KEY = os.environ.get('STABILITY_API_KEY') # Try to get from environment variable
|
13 |
+
if not STABILITY_API_KEY:
|
14 |
+
STABILITY_API_KEY = getpass.getpass('Enter your API Key: ') # Prompt if not found
|
15 |
+
|
16 |
+
def send_generation_request(
|
17 |
+
host,
|
18 |
+
params,
|
19 |
+
):
|
20 |
+
headers = {
|
21 |
+
"Accept": "image/*",
|
22 |
+
"Authorization": f"Bearer {STABILITY_API_KEY}"
|
23 |
+
}
|
24 |
+
|
25 |
+
# Encode parameters
|
26 |
+
files = {}
|
27 |
+
image = params.pop("image", None)
|
28 |
+
mask = params.pop("mask", None)
|
29 |
+
if image is not None and image != '':
|
30 |
+
files["image"] = open(image, 'rb')
|
31 |
+
if mask is not None and mask != '':
|
32 |
+
files["mask"] = open(mask, 'rb')
|
33 |
+
if len(files)==0:
|
34 |
+
files["none"] = ''
|
35 |
+
|
36 |
+
# Send request
|
37 |
+
print(f"Sending REST request to {host}...")
|
38 |
+
response = requests.post(
|
39 |
+
host,
|
40 |
+
headers=headers,
|
41 |
+
files=files,
|
42 |
+
data=params
|
43 |
+
)
|
44 |
+
if not response.ok:
|
45 |
+
raise Exception(f"HTTP {response.status_code}: {response.text}")
|
46 |
+
|
47 |
+
return response
|
48 |
+
|
49 |
+
def generate_image(prompt, negative_prompt, aspect_ratio, seed, output_format):
|
50 |
+
"""Generates an image using the SD3 API."""
|
51 |
+
|
52 |
+
host = os.environ["STABILITY_HOST"]
|
53 |
+
|
54 |
+
params = {
|
55 |
+
"prompt": prompt,
|
56 |
+
"negative_prompt": negative_prompt,
|
57 |
+
"aspect_ratio": aspect_ratio,
|
58 |
+
"seed": seed,
|
59 |
+
"output_format": output_format,
|
60 |
+
"model": "sd3-medium", # You can change the model here
|
61 |
+
"mode": "text-to-image"
|
62 |
+
}
|
63 |
+
|
64 |
+
response = send_generation_request(host, params)
|
65 |
+
|
66 |
+
# Decode response
|
67 |
+
output_image = response.content
|
68 |
+
finish_reason = response.headers.get("finish-reason")
|
69 |
+
seed = response.headers.get("seed")
|
70 |
+
|
71 |
+
# Check for NSFW classification
|
72 |
+
if finish_reason == 'CONTENT_FILTERED':
|
73 |
+
raise Warning("Generation failed NSFW classifier")
|
74 |
+
|
75 |
+
# Convert image to PIL format
|
76 |
+
image = Image.open(BytesIO(output_image))
|
77 |
+
|
78 |
+
return image
|
79 |
+
|
80 |
+
"""
|
81 |
+
Cost Rule
|
82 |
+
- SD3 Large: 2B, 6.5 credits each
|
83 |
+
- SD3 Large Turbo: 8B, 4 credits each
|
84 |
+
- SD3 Medium: 8B, 3.5 credits each
|
85 |
+
"""
|
86 |
+
|
87 |
+
# Create the Gradio interface
|
88 |
+
interface = gr.Interface(
|
89 |
+
fn=generate_image,
|
90 |
+
inputs=[
|
91 |
+
gr.Textbox(label="正向提示词", placeholder="This dreamlike digital art captures a vibrant, kaleidoscopic bird in a lush rainforest"),
|
92 |
+
gr.Textbox(label="负向提示词", placeholder="Optional"),
|
93 |
+
gr.Dropdown(label="长宽比", choices=["21:9", "16:9", "3:2", "5:4", "1:1", "4:5", "2:3", "9:16", "9:21"], value="16:9"),
|
94 |
+
gr.Number(label="生成算法随机种子", value=0),
|
95 |
+
gr.Dropdown(label="输出格式", choices=["jpeg", "png"], value="png")
|
96 |
+
],
|
97 |
+
outputs="image",
|
98 |
+
title="Stable Diffusion 3 Image Generator",
|
99 |
+
description="Generate images with Stable Diffusion 3. Type a prompt and see the magic!"
|
100 |
+
)
|
101 |
+
|
102 |
+
# Launch the interface
|
103 |
+
interface.launch(share=True)
|
img/cca.step1./350/256/244/347/237/245/350/256/241/347/256/227/345/271/277/345/221/212.png
ADDED
![]() |
img/cca.step2./350/256/244/347/237/245/350/256/241/347/256/227/345/271/277/345/221/212.png
ADDED
![]() |
img/cca.step3./350/256/244/347/237/245/350/256/241/347/256/227/345/271/277/345/221/212.png
ADDED
![]() |
img/cca.step4./350/256/244/347/237/245/350/256/241/347/256/227/345/271/277/345/221/212.png
ADDED
![]() |