from __future__ import annotations import gradio as gr import logging logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s') import subprocess def runcmd(command): ret = subprocess.run(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding="utf-8",timeout=60) if ret.returncode == 0: print("success:",ret) else: print("error:",ret) runcmd("pip3 install --upgrade clueai") import clueai cl = clueai.Client("", check_api_key=False) ''' #luck_t2i_btn_1, #luck_s2i_btn_1, #luck_i2i_btn_1, #luck_ici_btn_1{ color: #fff; --tw-gradient-from: #BED336; --tw-gradient-stops: var(--tw-gradient-from), var(--tw-gradient-to); --tw-gradient-to: #BED336; border-color: #BED336; } #luck_easy_btn_1, #luck_iti_btn_1, #luck_tsi_btn_1, #luck_isi_btn_1{ color: #fff; --tw-gradient-from: #BED336; --tw-gradient-stops: var(--tw-gradient-from), var(--tw-gradient-to); --tw-gradient-to: #BED336; border-color: #BED336; } ''' css=''' .container { max-width: 800px; margin: auto; } #gen_btn_1{ color: #fff; --tw-gradient-from: #f44336; --tw-gradient-stops: var(--tw-gradient-from), var(--tw-gradient-to); --tw-gradient-to: #ff9800; border-color: #ff9800; } #t2i_btn_1, #s2i_btn_1, #i2i_btn_1, #ici_btn_1, #easy_btn_1, #iti_btn_1, #tsi_btn_1, #isi_btn_1{ color: #fff; --tw-gradient-from: #f44336; --tw-gradient-stops: var(--tw-gradient-from), var(--tw-gradient-to); --tw-gradient-to: #ff9800; border-color: #ff9800; } #import_t2i_btn_1, #import_s2i_btn_1, #import_i2i_btn_1, #import_ici_btn_1{ color: #fff; --tw-gradient-from: #BED336; --tw-gradient-stops: var(--tw-gradient-from), var(--tw-gradient-to); --tw-gradient-to: #BED336; border-color: #BED336; } #import_easy_btn_1, #import_iti_btn_1, #import_tsi_btn_1, #import_isi_btn_1{ color: #fff; --tw-gradient-from: #BED336; --tw-gradient-stops: var(--tw-gradient-from), var(--tw-gradient-to); --tw-gradient-to: #BED336; border-color: #BED336; } #record_btn{ } #record_btn > div > button > span { width: 2.375rem; height: 2.375rem; } #record_btn > div > button > span > span { width: 2.375rem; height: 2.375rem; } audio { margin-bottom: 10px; } div#record_btn > .mt-6{ margin-top: 0!important; } div#record_btn > .mt-6 button { font-size: 1em; width: 100%; padding: 20px; height: 60px; } div#txt2img_tab { color: #BED336; } ''' task_styles = [] examples_list = [] task_style_to_task_prefix = {} import csv def read_examples(input_file): header = True with open(input_file) as finput: csv_input = csv.reader(finput) for line in csv_input: if header: header = False continue task_style, task_prefix, example = line task_styles.append(task_style) task_style_to_task_prefix[task_style] = task_prefix examples_list.append([task_style, example]) read_examples("./examples.csv") #print(task_styles) def preprocess(text, task): if task == "问答": text = text.replace("?", ":").replace("?", ":") text = text + ":" return task_style_to_task_prefix[task] + "\n" + text + "\n答案:" def inference_gen(text, task, do_sample, top_p, top_k, max_token, temperature, beam_size, length_penalty): text = preprocess(text, task) generate_config = { "do_sample": do_sample, "top_p": top_p, "top_k": top_k, "max_length": max_token, "temperature": temperature, "num_beams": beam_size, "length_penalty": length_penalty } #print(generate_config) #print(text) try: prediction = cl.generate( model_name='clueai-base', prompt=text, generate_config=generate_config) except Exception as e: logger.error(f"error, {e}") return return prediction.generations[0].text t2i_default_img_path_list = [] import base64, requests from io import BytesIO from PIL import Image def inference_image(text, n_text, guidance_scale, style, shape, clarity, steps, shape_scale): try: res = requests.get(f"https://www.clueai.cn/clueai/hf_text2image?text={text}&negative_prompt={n_text}\ &guidance_scale={guidance_scale}&num_inference_steps={steps}\ &style={style}&shape={shape}&clarity={clarity}&shape_scale={shape_scale}") except Exception as e: logger.error(f"error, {e}") return json_dict = res.json() file_path_list = [] for i, image in enumerate(json_dict["images"]): image = image.encode('utf-8') binary_data = base64.b64decode(image) img_data = BytesIO(binary_data) img = Image.open(img_data) file_path_list.append(img) return file_path_list image_styles = ['无', '细节大师', '对称美', '虚拟引擎', '空间感', '机械风格', '形状艺术', '治愈', '电影构图', '电影构图(治愈)', '荒芜感', '漫画', '逃离艺术', '斯皮尔伯格', '幻想', '杰作', '壁画', '朦胧', '黑白(3d)', '梵高', '毕加索', '莫奈', '丰子恺', '现代', '欧美'] with gr.Blocks(css=css, title="ClueAI") as demo: gr.Markdown('