update
Browse files
app.py
CHANGED
|
@@ -7,15 +7,15 @@ import torch
|
|
| 7 |
|
| 8 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 9 |
|
| 10 |
-
checkpoint = "microsoft/git-base"
|
| 11 |
-
processor = AutoProcessor.from_pretrained(checkpoint)
|
| 12 |
-
model = AutoModelForCausalLM.from_pretrained(checkpoint)
|
| 13 |
-
|
| 14 |
openai.organization = os.getenv("API_ORG")
|
| 15 |
openai.api_key = os.getenv("API_KEY")
|
| 16 |
app_password = os.getenv("APP_PASSWORD")
|
| 17 |
app_username = os.getenv("APP_USERNAME")
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def generate(input_image):
|
| 20 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
inputs = processor(images=input_image, return_tensors="pt").to(device)
|
|
|
|
| 7 |
|
| 8 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
openai.organization = os.getenv("API_ORG")
|
| 11 |
openai.api_key = os.getenv("API_KEY")
|
| 12 |
app_password = os.getenv("APP_PASSWORD")
|
| 13 |
app_username = os.getenv("APP_USERNAME")
|
| 14 |
|
| 15 |
+
checkpoint = "openai/clip-vit-base-patch32"
|
| 16 |
+
processor = AutoProcessor.from_pretrained(checkpoint)
|
| 17 |
+
model = AutoModelForCausalLM.from_pretrained(checkpoint)
|
| 18 |
+
|
| 19 |
def generate(input_image):
|
| 20 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
inputs = processor(images=input_image, return_tensors="pt").to(device)
|
app2.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
openai.organization = os.getenv("API_ORG")
|
| 7 |
+
openai.api_key = os.getenv("API_KEY")
|
| 8 |
+
app_password = os.getenv("APP_PASSWORD")
|
| 9 |
+
app_username = os.getenv("APP_USERNAME")
|
| 10 |
+
|
| 11 |
+
def generate_prompt(input):
|
| 12 |
+
prompt = """You are a prompt writing support system for image generation AI.
|
| 13 |
+
From the user's input, You output prompt in English that should be input to the image generation AI, imagining its intent as much as possible.
|
| 14 |
+
You are not allowed to ask questions of the user.
|
| 15 |
+
You will always output only brief prompt in English to be input to the image generation AI.
|
| 16 |
+
Your output will always English.
|
| 17 |
+
Input from user:
|
| 18 |
+
"""
|
| 19 |
+
response = openai.ChatCompletion.create(
|
| 20 |
+
model = "gpt-3.5-turbo",
|
| 21 |
+
messages = [{"role": "system", "content": prompt+input}],
|
| 22 |
+
max_tokens=256
|
| 23 |
+
)
|
| 24 |
+
generated_text = response['choices'][0]['message']['content'].strip()
|
| 25 |
+
return "Make the illustration a photo: "+generated_text
|
| 26 |
+
|
| 27 |
+
def get_related_caption(prompt):
|
| 28 |
+
url = "https://api.irasutoya.nibo.sh/semantic-search"
|
| 29 |
+
params = {'q': prompt}
|
| 30 |
+
headers = {"content-type": "application/json"}
|
| 31 |
+
r = requests.get(url, params=params, headers=headers)
|
| 32 |
+
data = r.json()
|
| 33 |
+
return data['illustrations'][0]['description']
|
| 34 |
+
|
| 35 |
+
def generate(prompt):
|
| 36 |
+
caption = get_related_caption(prompt)
|
| 37 |
+
generated_prompt = generate_prompt(caption)
|
| 38 |
+
response = openai.Image.create(
|
| 39 |
+
prompt=generated_prompt,
|
| 40 |
+
n=1,
|
| 41 |
+
size="256x256"
|
| 42 |
+
)
|
| 43 |
+
return caption, generated_prompt, response['data'][0]['url']
|
| 44 |
+
|
| 45 |
+
with gr.Blocks() as demo:
|
| 46 |
+
with gr.Column():
|
| 47 |
+
with gr.Row():
|
| 48 |
+
with gr.Column():
|
| 49 |
+
prompt_text = gr.Textbox(lines=5, label="Prompt")
|
| 50 |
+
prompt_examples = gr.Examples(
|
| 51 |
+
examples=[
|
| 52 |
+
"ใใฎใใฎๅฑฑ",
|
| 53 |
+
"ใใใฎใใฎ้",
|
| 54 |
+
"ใ่ๅญใฎๅฎถ",
|
| 55 |
+
],
|
| 56 |
+
inputs=[prompt_text],
|
| 57 |
+
outputs=None,
|
| 58 |
+
)
|
| 59 |
+
btn = gr.Button(value="Generate Image")
|
| 60 |
+
|
| 61 |
+
with gr.Column():
|
| 62 |
+
caption = gr.Textbox(lines=5, label="Related Caption")
|
| 63 |
+
generated_prompt = gr.Textbox(lines=5, label="Generated Prompt")
|
| 64 |
+
out_image = gr.components.Image(type="filepath", label="Generated Image")
|
| 65 |
+
|
| 66 |
+
btn.click(generate, inputs=[prompt_text], outputs=[caption, generated_prompt, out_image])
|
| 67 |
+
demo.load()
|
| 68 |
+
|
| 69 |
+
demo.launch(share=True, auth=(app_username, app_password))
|