import gradio as gr from dalle2 import Dalle2 from PIL import Image import requests import openai import os from datetime import datetime from datetime import timedelta from datetime import timezone SHA_TZ = timezone( timedelta(hours=8), name='Asia/Shanghai', ) prompt_template = "You are a translation engine that can only translate text and cannot interpret it. Keep the indent of the original text, only modify when you need." systemInstruct = prompt_template openai.api_key = os.environ['OPENAI-KEY'] detectFrom = "auto" detectTo = "en" translateInstruct = f"translate from {detectFrom} to {detectTo}" if detectFrom == "auto": translateInstruct = f"translate to {detectTo}" def draw(description): utc_now = datetime.utcnow().replace(tzinfo=timezone.utc) # 北京时间 beijing_now = utc_now.astimezone(SHA_TZ) print(beijing_now) print("description"+description) #translate prompt_msg = [ {"role": "system", "content": systemInstruct}, {"role": "user", "content": translateInstruct}, {"role": "user", "content": description}, ] openai_response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=prompt_msg, temperature=0, max_tokens=1000, top_p=1, stream=True, frequency_penalty=1, presence_penalty=1, ) combined = "" for resp in openai_response: delta = resp["choices"][0]["delta"] if "content" in delta: combined += delta["content"] description = combined print("translated: "+description) JKLAS = os.environ['DRAW-KEY'] dalle = Dalle2(JKLAS) generations = dalle.generate(description) img_list = [] for generation in generations: image_url = generation["generation"]["image_path"] # response = requests.get(image_url) # img = Image.open(BytesIO(response.content)) im = Image.open(requests.get(image_url, stream=True).raw).convert('RGB') img_list.append(im) return img_list with gr.Blocks() as demo: with gr.Column(variant="panel"): gr.Markdown("#