djrana commited on
Commit
527cdd9
1 Parent(s): 208d806

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -11
app.py CHANGED
@@ -1,27 +1,46 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
3
 
4
  # Load the pipeline for text generation
5
- pipe = pipeline(
6
  "text-generation",
7
  model="Ar4ikov/gpt2-650k-stable-diffusion-prompt-generator",
8
  tokenizer="gpt2"
9
  )
10
 
 
 
 
 
11
  # Function to generate text based on input prompt
12
  def generate_text(prompt):
13
- # Generate multiple outputs for the same prompt
14
- generated_texts = [pipe(prompt, max_length=77)[0]["generated_text"] for _ in range(5)]
15
- return generated_texts
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- # Create a Gradio interface
18
  iface = gr.Interface(
19
- fn=generate_text,
20
- inputs=gr.Textbox(lines=5, label="Prompt"),
21
- outputs=[gr.Textbox(label=f"Output {i+1}", readonly=True, multiline=True) for i in range(5)],
22
  title="AI Art Prompt Generator",
23
- description="This tool generates multiple outputs for a given prompt using the AI Art Prompt Generator model.",
24
- allow_flagging=False
25
  )
26
 
27
  # Launch the interface
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
+ from PIL import Image
4
 
5
  # Load the pipeline for text generation
6
+ text_generator = pipeline(
7
  "text-generation",
8
  model="Ar4ikov/gpt2-650k-stable-diffusion-prompt-generator",
9
  tokenizer="gpt2"
10
  )
11
 
12
+ # Load tokenizer and model for image generation
13
+ tokenizer = AutoTokenizer.from_pretrained("stablediffusionapi/juggernaut-xl-v8")
14
+ model = AutoModelForCausalLM.from_pretrained("stablediffusionapi/juggernaut-xl-v8")
15
+
16
  # Function to generate text based on input prompt
17
  def generate_text(prompt):
18
+ return text_generator(prompt, max_length=77)[0]["generated_text"]
19
+
20
+ # Function to generate image based on input text
21
+ def generate_image(text):
22
+ # Tokenize input text
23
+ input_ids = tokenizer.encode(text, return_tensors="pt")
24
+
25
+ # Generate image conditioned on input text
26
+ output = model.generate(input_ids, do_sample=True, max_length=128, num_return_sequences=1)
27
+
28
+ # Decode generated image tokens to get image
29
+ image_bytes = tokenizer.decode(output[0], skip_special_tokens=True)
30
+
31
+ # Convert image bytes to PIL image
32
+ image = Image.open(image_bytes)
33
+
34
+ return image
35
 
36
+ # Create Gradio interface
37
  iface = gr.Interface(
38
+ fn=[generate_text, generate_image],
39
+ inputs=["textbox", "textbox"],
40
+ outputs=["textbox", "image"],
41
  title="AI Art Prompt Generator",
42
+ description="Art Prompt Generator is a user-friendly interface designed to optimize input for AI Art Generator or Creator. For faster generation speeds, it's recommended to load the model locally with GPUs, as the online demo at Hugging Face Spaces utilizes CPU, resulting in slower processing times.",
43
+ theme="huggingface"
44
  )
45
 
46
  # Launch the interface