glt3953 commited on
Commit
b82fb11
·
1 Parent(s): d658a3a

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +148 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pip install "modelscope[cv]" -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html
2
+ #pip install gradio
3
+ #pip install tensorflow
4
+
5
+ from tqdm import tqdm
6
+ from skimage import io
7
+ import datetime
8
+ import os
9
+ import gradio as gr
10
+ from PIL import Image
11
+ from translate import Translator
12
+ from gradio_client import Client
13
+ import json
14
+
15
+ # 初始化Translator对象,指定源语言和目标语言
16
+ translator = Translator(from_lang="zh", to_lang="en")
17
+
18
+ #获取当前北京时间
19
+ utc_dt = datetime.datetime.utcnow()
20
+ beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=8)))
21
+ formatted = beijing_dt.strftime("%Y-%m-%d_%H")
22
+ print(f"北京时间: {beijing_dt.year}年{beijing_dt.month}月{beijing_dt.day}日 "
23
+ f"{beijing_dt.hour}时{beijing_dt.minute}分{beijing_dt.second}秒")
24
+ #创建作品存放目录
25
+ works_path = 'works_text_image_api/' + formatted
26
+ if not os.path.exists(works_path):
27
+ os.makedirs(works_path)
28
+ print('作品目录:' + works_path)
29
+ #创建用户上传图片存放目录
30
+ user_upload_path = 'user_upload/' + formatted
31
+ if not os.path.exists(user_upload_path):
32
+ os.makedirs(user_upload_path)
33
+ print('用户图片目录:' + user_upload_path)
34
+
35
+ def get_size(h, w, max = 720):
36
+ if min(h, w) > max:
37
+ if h > w:
38
+ h, w = int(max * h / w), max
39
+ else:
40
+ h, w = max, int(max * w / h)
41
+
42
+ return h, w
43
+
44
+ def inference(original_prompt: str, image: Image) -> Image:
45
+ #调整图片尺寸,避免过大导致处理耗时过久
46
+ w, h = image.size
47
+ print(f'原图片宽:{w},高:{h}')
48
+ h, w = get_size(h, w, 720)
49
+ image = image.resize((w, h))
50
+ print(f'调整尺寸后图片宽:{w},高:{h}')
51
+
52
+ print('图片描述:' + original_prompt)
53
+ translate_prompt = translator.translate(original_prompt) #翻译为英文
54
+ print('translate_prompt:' + translate_prompt)
55
+
56
+ utc_dt = datetime.datetime.utcnow()
57
+ beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=8)))
58
+ formatted = beijing_dt.strftime("%Y-%m-%d_%H-%M-%S.%f")
59
+ image_path = user_upload_path + '/' + formatted + '.png'
60
+ print('用户图片:' + image_path)
61
+ image.save(image_path)
62
+
63
+ # https://huggingface.co/spaces/hysts/ControlNet-v1-1
64
+ client = Client("https://hysts-controlnet-v1-1.hf.space/")
65
+ result = client.predict(
66
+ image_path, # str (filepath or URL to image) in 'parameter_98' Image component
67
+ translate_prompt, # str in 'Prompt' Textbox component
68
+ "masterpiece, best quality, extremely detailed", # str in 'Additional prompt' Textbox component
69
+ "longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality", # str in 'Negative prompt' Textbox component
70
+ 1, # int | float (numeric value between 1 and 1) in 'Number of images' Slider component
71
+ 512, # int | float (numeric value between 256 and 512) in 'Image resolution' Slider component
72
+ 512, # int | float (numeric value between 128 and 512) in 'Preprocess resolution' Slider component
73
+ 20, # int | float (numeric value between 1 and 100) in 'Number of steps' Slider component
74
+ 9.0, # int | float (numeric value between 0.1 and 30.0) in 'Guidance scale' Slider component
75
+ 706138, # int | float (numeric value between 0 and 1000000) in 'Seed' Slider component
76
+ "HED", # str in 'Preprocessor' Radio component
77
+ api_name="/softedge"
78
+ )
79
+
80
+ print(result)
81
+ result += '/captions.json'
82
+
83
+ with open(result) as f:
84
+ data = json.load(f)
85
+
86
+ # data 为Python对象
87
+ print(data)
88
+
89
+ i = 0
90
+ for key in data.keys():
91
+ if i == 1:
92
+ result_path = key
93
+ break
94
+
95
+ i += 1
96
+
97
+ res_img = Image.open(result_path)
98
+ print('作品:' + result_path)
99
+
100
+ return res_img
101
+
102
+
103
+ css_style = "#fixed_size_img {height: 240px;} "
104
+
105
+ title = "人像创作 by宁侠"
106
+ description = '''
107
+ 我们提供的服务能够快速高效地将您提供的人像图片转化为栩栩如生的肖像图,您只需简单地输入图片描述,我们的服务便会根据您的要求对图片进行处理,让您获得一张高质量的肖像图。我们期待着为您提供最好的服务,并让您的体验更加愉快。
108
+ '''
109
+ examples_path = 'examples/'
110
+ examples = [[examples_path + 'input1.png'], [examples_path + 'input2.png'], [examples_path + 'input3.png'], [examples_path + 'input4.png']]
111
+
112
+ with gr.Blocks(title=title, css=css_style) as demo:
113
+ gr.HTML('''
114
+ <div style="text-align: center; max-width: 720px; margin: 0 auto;">
115
+ <div
116
+ style="
117
+ display: inline-flex;
118
+ align-items: center;
119
+ gap: 0.8rem;
120
+ font-size: 1.75rem;
121
+ "
122
+ >
123
+ <h1 style="font-family: PingFangSC; font-weight: 500; line-height: 1.5em; font-size: 32px; margin-bottom: 7px;">
124
+ 人像创作
125
+ </h1>
126
+ <h1 style="font-family: PingFangSC; font-weight: 500; line-height: 1.5em; font-size: 16px; margin-bottom: 7px;">
127
+ by宁侠
128
+ </h1>
129
+ </div>
130
+ <img id="overview" alt="overview" src="https://github.com/lllyasviel/ControlNet/raw/main/github_page/p5.png" />
131
+ </div>
132
+ ''')
133
+
134
+ gr.Markdown(description)
135
+ with gr.Row():
136
+ original_prompt = gr.Textbox(label="请输入图片描述", value="英俊青年的油画,杰作")
137
+ with gr.Row():
138
+ img_input = gr.Image(label="图片", type="pil", elem_id="fixed_size_img")
139
+ img_output = gr.Image(label="作品", type="pil", elem_id="fixed_size_img")
140
+ with gr.Row():
141
+ btn_submit = gr.Button(value="一键创作", elem_id="blue_btn")
142
+ # btn_clear = gr.Button(value="清除")
143
+
144
+ examples = gr.Examples(examples=examples, inputs=[img_input], outputs=img_output)
145
+ btn_submit.click(inference, inputs=[original_prompt, img_input], outputs=img_output)
146
+ # btn_clear清除画布
147
+
148
+ demo.queue(api_open=False).launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ translate