Spaces:
Sleeping
Sleeping
Kvikontent
commited on
Commit
•
2a04c3f
1
Parent(s):
2e589e2
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,27 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
return color
|
9 |
|
|
|
10 |
def generate_color(prompt):
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
color_white = " "
|
15 |
-
color_none = " "
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
sym = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "<", ">", "/", "'", "-", "+", "="]
|
21 |
-
|
22 |
-
redded = []
|
23 |
-
greened = []
|
24 |
-
blued = []
|
25 |
-
whited = []
|
26 |
-
nothing = []
|
27 |
|
28 |
-
|
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 |
-
|
41 |
-
|
42 |
-
|
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 |
-
|
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()
|
|