Spaces:
Sleeping
Sleeping
girishwangikar
commited on
Commit
•
a6545da
1
Parent(s):
c8e4067
Update app.py
Browse files
app.py
CHANGED
@@ -2,12 +2,13 @@ import gradio as gr
|
|
2 |
import random
|
3 |
import torch
|
4 |
import spaces
|
5 |
-
from diffusers import DiffusionPipeline
|
6 |
from langchain_groq import ChatGroq
|
7 |
from langchain.schema import HumanMessage, SystemMessage
|
8 |
import os
|
9 |
from PIL import Image
|
10 |
import numpy as np
|
|
|
|
|
11 |
|
12 |
# Set up API keys
|
13 |
GROQ_API_KEY = os.environ.get('GROQ_API_KEY')
|
@@ -15,12 +16,6 @@ GROQ_API_KEY = os.environ.get('GROQ_API_KEY')
|
|
15 |
# Set up LLM
|
16 |
llm = ChatGroq(temperature=0, model_name='llama-3.1-8b-instant', groq_api_key=GROQ_API_KEY)
|
17 |
|
18 |
-
# Set up DiffusionPipeline on CPU
|
19 |
-
dtype = torch.bfloat16
|
20 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
21 |
-
|
22 |
-
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
|
23 |
-
|
24 |
MAX_SEED = np.iinfo(np.int32).max
|
25 |
MAX_IMAGE_SIZE = 512
|
26 |
|
@@ -36,7 +31,6 @@ def generate_detailed_prompt(user_input):
|
|
36 |
Given a simple description, create an elaborate and detailed prompt that can be used to generate high-quality images.
|
37 |
Your response should be concise and no longer than 3 sentences.
|
38 |
Use the following examples as a guide for the level of detail and creativity expected:
|
39 |
-
|
40 |
""" + "\n\n".join([f"Input: {input}\nOutput: {output}" for input, output in few_shot_examples]))
|
41 |
|
42 |
human_message = HumanMessage(content=f"Generate a detailed image prompt based on this input, using no more than 3 sentences: {user_input}")
|
@@ -44,19 +38,27 @@ def generate_detailed_prompt(user_input):
|
|
44 |
response = llm([system_message, human_message])
|
45 |
return response.content
|
46 |
|
|
|
|
|
|
|
|
|
47 |
@spaces.GPU()
|
48 |
def generate_image(prompt, width=512, height=512, num_inference_steps=4):
|
49 |
seed = random.randint(0, MAX_SEED)
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
53 |
width=width,
|
54 |
height=height,
|
55 |
num_inference_steps=num_inference_steps,
|
56 |
-
|
57 |
-
|
58 |
-
)
|
59 |
-
|
|
|
|
|
60 |
return image
|
61 |
|
62 |
# Gradio UI setup
|
@@ -117,4 +119,4 @@ with gr.Blocks(css=css, theme='gradio/soft') as demo:
|
|
117 |
outputs=[result]
|
118 |
)
|
119 |
|
120 |
-
demo.launch(
|
|
|
2 |
import random
|
3 |
import torch
|
4 |
import spaces
|
|
|
5 |
from langchain_groq import ChatGroq
|
6 |
from langchain.schema import HumanMessage, SystemMessage
|
7 |
import os
|
8 |
from PIL import Image
|
9 |
import numpy as np
|
10 |
+
import base64
|
11 |
+
from io import BytesIO
|
12 |
|
13 |
# Set up API keys
|
14 |
GROQ_API_KEY = os.environ.get('GROQ_API_KEY')
|
|
|
16 |
# Set up LLM
|
17 |
llm = ChatGroq(temperature=0, model_name='llama-3.1-8b-instant', groq_api_key=GROQ_API_KEY)
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
MAX_SEED = np.iinfo(np.int32).max
|
20 |
MAX_IMAGE_SIZE = 512
|
21 |
|
|
|
31 |
Given a simple description, create an elaborate and detailed prompt that can be used to generate high-quality images.
|
32 |
Your response should be concise and no longer than 3 sentences.
|
33 |
Use the following examples as a guide for the level of detail and creativity expected:
|
|
|
34 |
""" + "\n\n".join([f"Input: {input}\nOutput: {output}" for input, output in few_shot_examples]))
|
35 |
|
36 |
human_message = HumanMessage(content=f"Generate a detailed image prompt based on this input, using no more than 3 sentences: {user_input}")
|
|
|
38 |
response = llm([system_message, human_message])
|
39 |
return response.content
|
40 |
|
41 |
+
# Initialize the schnell client
|
42 |
+
from huggingface_hub import InferenceClient
|
43 |
+
client = InferenceClient("black-forest-labs/FLUX.1-schnell")
|
44 |
+
|
45 |
@spaces.GPU()
|
46 |
def generate_image(prompt, width=512, height=512, num_inference_steps=4):
|
47 |
seed = random.randint(0, MAX_SEED)
|
48 |
+
|
49 |
+
# Use the schnell client to generate the image
|
50 |
+
result = client.text_to_image(
|
51 |
+
prompt,
|
52 |
+
negative_prompt="",
|
53 |
width=width,
|
54 |
height=height,
|
55 |
num_inference_steps=num_inference_steps,
|
56 |
+
guidance_scale=0.0,
|
57 |
+
seed=seed
|
58 |
+
)
|
59 |
+
|
60 |
+
# Convert the image to a PIL Image object
|
61 |
+
image = Image.open(BytesIO(result))
|
62 |
return image
|
63 |
|
64 |
# Gradio UI setup
|
|
|
119 |
outputs=[result]
|
120 |
)
|
121 |
|
122 |
+
demo.launch()
|