Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from huggingface_hub import InferenceClient, login
|
4 |
+
|
5 |
+
login(os.getenv("HUGGINGFACEHUB_API_TOKEN"))
|
6 |
+
|
7 |
+
|
8 |
+
repo_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
9 |
+
|
10 |
+
from huggingface_hub import InferenceClient
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
def image_generator(text_input, style):
|
15 |
+
|
16 |
+
system_input = f"You are an expert prompt engineer with artistic flair. "
|
17 |
+
user_input = f"Write a concise prompt for a {style} image containing {text_input}. Only return the prompt."
|
18 |
+
|
19 |
+
messages = [
|
20 |
+
{"role": "system", "content": system_input},
|
21 |
+
{"role": "user", "content": user_input},
|
22 |
+
]
|
23 |
+
|
24 |
+
client = InferenceClient( repo_id, )
|
25 |
+
|
26 |
+
chat_completion = client.chat_completion(
|
27 |
+
messages=messages,
|
28 |
+
max_tokens=500,
|
29 |
+
)
|
30 |
+
prompt = chat_completion.choices[0].message.content
|
31 |
+
|
32 |
+
client = InferenceClient()
|
33 |
+
|
34 |
+
image = client.text_to_image(
|
35 |
+
prompt=prompt,
|
36 |
+
model="stabilityai/stable-diffusion-xl-base-1.0",
|
37 |
+
guidance_scale=8,
|
38 |
+
seed=42,
|
39 |
+
)
|
40 |
+
return prompt, image
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
with gr.Blocks() as demo:
|
45 |
+
with gr.Row():
|
46 |
+
with gr.Column():
|
47 |
+
input_text = gr.Textbox(label="Prompt")
|
48 |
+
style = gr.Radio(["fun", "interesting"])
|
49 |
+
prompt = gr.Textbox(interactive=False, visible=True, label="Refined prompt")
|
50 |
+
output_image = gr.Image(interactive=False, label="Result")
|
51 |
+
|
52 |
+
with gr.Row():
|
53 |
+
reset = gr.ClearButton([input_text])
|
54 |
+
submit = gr.Button("Submit")
|
55 |
+
with gr.Column():
|
56 |
+
submit.click(fn=image_generator, inputs=[input_text, style], outputs=[prompt, output_image])
|
57 |
+
|
58 |
+
examples = gr.Examples(
|
59 |
+
examples=[
|
60 |
+
["a llama and a cookbook", "fun"],
|
61 |
+
["a squirrel", "interesting"],
|
62 |
+
],
|
63 |
+
inputs=[input_text, style]),
|
64 |
+
|
65 |
+
|
66 |
+
if __name__ == "__main__":
|
67 |
+
demo.launch()
|