djrana commited on
Commit
92a57ae
1 Parent(s): 527cdd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -30
app.py CHANGED
@@ -1,47 +1,32 @@
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
47
- iface.launch()
 
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
+ # Initialize a list to store the history of generated prompts
12
+ history = []
 
13
 
14
+ # Function to generate text based on input prompt and record the history
15
  def generate_text(prompt):
16
+ generated_text = pipe(prompt, max_length=77)[0]["generated_text"]
17
+ # Append the generated prompt and its result to the history list
18
+ history.append({"prompt": prompt, "generated_text": generated_text})
19
+ return generated_text
20
 
21
+ # Create a Gradio interface with history recording
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  iface = gr.Interface(
23
+ fn=generate_text,
24
+ inputs=gr.Textbox(lines=5, label="Prompt"),
25
+ outputs=gr.Textbox(label="Output", show_copy_button=True),
26
  title="AI Art Prompt Generator",
27
  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.",
28
+ api_name="predict"
29
  )
30
 
31
  # Launch the interface
32
+ iface.launch(show_api=True)