Kvikontent commited on
Commit
2a04c3f
1 Parent(s): 2e589e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -46
app.py CHANGED
@@ -1,54 +1,27 @@
1
  import gradio as gr
2
- import random
3
- from PIL import Image, ImageDraw
 
 
4
 
5
- def smooth_color(color, threshold):
6
- if color == " ":
7
- return random.choice(["FF0000", "32FF00", "00FFFF", "FFFFFF", "000000"])
8
- return color
9
 
 
10
  def generate_color(prompt):
11
- color_red = " "
12
- color_green = " "
13
- color_blue = " "
14
- color_white = " "
15
- color_none = " "
16
 
17
- red = ['e', 'f', 'g', 'i', 'j', 'k', 'm', 'n', 'p', 'r']
18
- green = ['c', 'd', 'h', 'l', 'o', 'q', 's', 'u', 'y', 'w']
19
- blue = ['a', 'b', 't', 'v', 'x', 'z']
20
- sym = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "<", ">", "/", "'", "-", "+", "="]
21
-
22
- redded = []
23
- greened = []
24
- blued = []
25
- whited = []
26
- nothing = []
27
 
28
- for letter in prompt:
29
- if letter in red:
30
- redded.append(letter)
31
- elif letter in green:
32
- greened.append(letter)
33
- elif letter in blue:
34
- blued.append(letter)
35
- elif letter == " ":
36
- whited.append(random.choice(sym))
37
- else:
38
- nothing.append(letter)
39
 
40
- color_red = smooth_color(color_red, len(redded))
41
- color_green = smooth_color(color_green, len(greened))
42
- color_blue = smooth_color(color_blue, len(blued))
43
- color_white = smooth_color(color_white, len(whited))
44
- color_none = smooth_color(color_none, len(nothing))
45
-
46
- # Convert the color to an image using PIL
47
- image = Image.new('RGB', (100, 100), color="#"+color_red+color_green+color_blue)
48
- draw = ImageDraw.Draw(image)
49
- draw.text((20, 40), "".join(whited), fill="#"+color_white)
50
-
51
- return image
52
 
53
- iface = gr.Interface(generate_color, "text", "image", theme="soft")
54
- iface.launch()
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import GPT2Tokenizer, GPT2LMHeadModel
4
+ import matplotlib.pyplot as plt
5
+ import numpy as np
6
 
7
+ # Load pre-trained GPT-2 model and tokenizer
8
+ model = GPT2LMHeadModel.from_pretrained("gpt2")
9
+ tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
 
10
 
11
+ # Define a function to generate color based on text prompt
12
  def generate_color(prompt):
13
+ input_ids = tokenizer.encode(prompt, return_tensors='pt')
14
+ output = model.generate(input_ids, max_length=50, num_return_sequences=1, no_repeat_ngram_size=2)
15
+ color_name = tokenizer.decode(output[0], skip_special_tokens=True)
 
 
16
 
17
+ # Create an image with the generated color
18
+ color = [int(ord(char) * 255 / 122) for char in color_name[:3]]
19
+ img = np.full((100, 100, 3), color, dtype=np.uint8)
 
 
 
 
 
 
 
20
 
21
+ return img
 
 
 
 
 
 
 
 
 
 
22
 
23
+ # Create Gradio interface
24
+ inputs = gr.Textbox(lines=2, label="Enter a text prompt (e.g., 'a color that represents happiness'):")
25
+ output = gr.Image(type="numpy", label="Generated color:")
 
 
 
 
 
 
 
 
 
26
 
27
+ gr.Interface(fn=generate_color, inputs=inputs, outputs=output, title="AI Color Generator", description="Generate a color based on a text prompt using GPT-2 model.").launch()