-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
171 lines (137 loc) · 7.27 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import streamlit as st
import numpy as np
from PIL import Image
import time
from scraping_functions.pipeline import scrape_archetypes, dataset_cleaner, process_archetype_input
import pandas as pd
import os
# import requests
from apikey import MYAPIKEY
from sdai import get_stable_diffusion_image
from text_eda_all_script import comprehensive_eda
data_for_text = pd.read_csv("training_data_final/all_training_cards.csv")
# Function to generate random text
def generate_text():
text = np.random.choice(['Hello!', 'How are you?', 'Streamlit is fun!', 'Random text generator.'])
return text
# Function to generate a random image
def generate_image():
# Generate a random image using numpy (for demo purposes)
random_image = np.random.randint(0, 255, size=(300, 300, 3), dtype=np.uint8)
return random_image
# Main function to run the Streamlit app
def main():
# If provided, EDA will be available
# ARCHETYPE_PROVIDED = False
st.set_page_config(
page_title="Yu-Gi-Oh! Card Generator",
page_icon="https://upload.wikimedia.org/wikipedia/en/2/2b/Yugioh_Card_Back.jpg",
layout="centered",
initial_sidebar_state="expanded",
menu_items={
'Get help': 'https://github.com/nogibjj/Generating-Yu-Gi-Oh-Monsters-From-Archetypes/',
'Report a bug': "https://github.com/nogibjj/Generating-Yu-Gi-Oh-Monsters-From-Archetypes/",
'About': "# Made by Eric Rios and Shin Jiwon. This is an *extremely* cool app! Our repository is https://github.com/nogibjj/Generating-Yu-Gi-Oh-Monsters-From-Archetypes/"
}
)
image = Image.open("logo.jpg")
resized_image = image.resize((500, 500))
left_co, cent_co,last_co = st.columns(3)
with cent_co:
st.image(image) # Set the width to the desired value
st.title("Welcome to the Yu-Gi-Oh! Card Archetype Generator")
st.markdown(
"""
Welcome to the Yu-Gi-Oh! Card Archetype Generator. \n
Here you can generate cards by providing the keywords of the archetype that you are interested in generating (with the Stability Ai API).
Under Generator Builder, provide the keyword and press "Get Archetype". \n
For demonstration purposes, we have a small showcase of the content generated by the GAN model, which is quite blurry, as well as the stable diffusion model found on huggingface (the UNET tutorial variant).
To contrast with our homemade solutions, we have also included a showcase of the card monsters generated by the Stability AI API, frontrunners of Stable Diffusion. \n
"""
)
card_database = pd.read_csv("training_data_final/all_training_cards.csv")
user_database = pd.read_csv("training_data_final/all_training_cards.csv")
with st.sidebar:
st.title("Generator Builder")
st.write("""Please enter the keyword of the archetype you would like to generate with Stability AI API.
Here are some examples to get you started. Scroll below and to the right to see them.
1) Dark Magician""")
user_input = st.text_input("Enter Archetype Here")
user_gen_input = ""
if st.button("Get Archetype"):
if user_input == "" or user_input == None or len(user_input) < 3:
st.warning("Please enter an archetype.")
else:
# ARCHETYPE_PROVIDED = True
with st.spinner(f"Getting data for {user_input}..."):
scrape_archetypes(user_input, data_path="user_data/imgs", csv_path="user_data", csv_name="user_training_cards.csv")
print("Scraping for", user_input, "completed!")
print("Starting to clean datasets of bad images")
dataset_cleaner(dataset_path = "user_data/imgs", csv_path = "user_data" + os.path.sep + "user_training_cards.csv")
# scrape_archetypes(user_input, data_path="user_data")
st.write(f"Here is the archetype for {user_input}")
st.success("Done")
user_input = process_archetype_input(user_input)
# user_database = pd.read_csv("user_data/user_training_cards.csv")
user_database = pd.read_csv("training_data_final/all_training_cards.csv")
if st.button("Prepare Generation"):
user_gen_input = st.text_input("Enter Archetype To Generate Here")
# Button to generate random text
if st.button("Show Archetype Card"):
# random_text = generate_text()
filtered = user_database[user_database["name"].str.contains(user_input)]
random_card = filtered.sample()
# st.write(f"Random Text: {random_text}")
# choose a random card from the card_database
# random_card = card_database.sample()
# path = "user_data" + os.path.sep + random_card["image_path"].values[0]
path = random_card["image_path"].values[0]
print(random_card["image_path"].values[0])
# st.write("Random Image: " + path)
st.image(path, caption=random_card["name"].values[0], use_column_width=True)
st.write(random_card["desc"].values[0])
# st.write(user_input)
# print(user_input)
# st.write(path)
# Button to generate random image
user_gen_input = st.text_input("Enter Archetype To Generate Here")
if st.button("Generate Image"):
random_image = generate_image()
filtered = user_database[user_database["name"].str.contains(user_input)]
random_card = filtered.sample()
prompt = f"Make a Yu-Gi-Oh Card with a {user_gen_input}"
st.write(prompt)
path = random_card["image_path"].values[0]
with open(path, 'rb') as f:
an_image = Image.open(f)
# st.write("Random Image: " + path)
img = get_stable_diffusion_image(
prompt = prompt,
output_format = "png",
MYAPIKEY= MYAPIKEY,
target_name="user_im",
file_name = path,
)
new_image_path = "streamlit_images" + os.path.sep + "user_im.png"
# # choose a random card from the card_database
# random_card = card_database.sample()
# path = "user_imgs" + os.path.sep + str(random_card["id"].values[0]) + ".jpg"
# st.write("Random Image: " + path)
st.image(new_image_path, use_column_width=True)
st.title("Data Analysis for YGO Cards Provided", anchor="data-analysis")
st.markdown(
"""
We used 50% of all cards to comply with fair use, and the sampling was stratified to preserved the distribution of the original dataset."
"""
)
# st.write(")
# st.write(f"Archetype: {user_input}")
if st.button("Show Data Analysis"):
print("Starting data analysis")
comprehensive_eda(data_for_text, save_path="streamlit_images")
st.image("streamlit_images/top_frequencies.png", caption="Top Frequencies", use_column_width=True)
st.image("streamlit_images/top_pos_tags.png", caption="Top POS Tags", use_column_width=True)
st.image("streamlit_images/top_meaningful_words.png", caption="Top Meaningful Words", use_column_width=True)
st.image("streamlit_images/sentiment_distribution.png", caption="Sentiment Distribution", use_column_width=True)
if __name__ == '__main__':
main()