wcx commited on
Commit
8f3b56b
·
1 Parent(s): 1adbff4

Init commit

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +1 -0
  2. README.md +5 -6
  3. app.py +121 -0
  4. avatar_generation.py +79 -0
  5. constants.py +156 -0
  6. data/object_pic/AlarmClock.png +0 -0
  7. data/object_pic/Apple.png +0 -0
  8. data/object_pic/BaseballBat.png +0 -0
  9. data/object_pic/BasketBall.png +0 -0
  10. data/object_pic/Book.png +0 -0
  11. data/object_pic/Boots.png +0 -0
  12. data/object_pic/Bottle.png +0 -0
  13. data/object_pic/Bowl.png +0 -0
  14. data/object_pic/Box.png +0 -0
  15. data/object_pic/Bread.png +0 -0
  16. data/object_pic/ButterKnife.png +0 -0
  17. data/object_pic/Candle.png +0 -0
  18. data/object_pic/CellPhone.png +0 -0
  19. data/object_pic/Cloth.png +0 -0
  20. data/object_pic/CreditCard.png +0 -0
  21. data/object_pic/Cup.png +0 -0
  22. data/object_pic/DishSponge.png +0 -0
  23. data/object_pic/Egg.png +0 -0
  24. data/object_pic/Fork.png +0 -0
  25. data/object_pic/Kettle.png +0 -0
  26. data/object_pic/KeyChain.png +0 -0
  27. data/object_pic/Knife.png +0 -0
  28. data/object_pic/Ladle.png +0 -0
  29. data/object_pic/Laptop.png +0 -0
  30. data/object_pic/Lettuce.png +0 -0
  31. data/object_pic/Mug.png +0 -0
  32. data/object_pic/Newspaper.png +0 -0
  33. data/object_pic/Pan.png +0 -0
  34. data/object_pic/PaperTowelRoll.png +0 -0
  35. data/object_pic/Pen.png +0 -0
  36. data/object_pic/Pencil.png +0 -0
  37. data/object_pic/PepperShaker.png +0 -0
  38. data/object_pic/Phone.png +0 -0
  39. data/object_pic/Pillow.png +0 -0
  40. data/object_pic/Plate.png +0 -0
  41. data/object_pic/Plunger.png +0 -0
  42. data/object_pic/Pot.png +0 -0
  43. data/object_pic/Potato.png +0 -0
  44. data/object_pic/RemoteControl.png +0 -0
  45. data/object_pic/SaltShaker.png +0 -0
  46. data/object_pic/SoapBar.png +0 -0
  47. data/object_pic/SoapBottle.png +0 -0
  48. data/object_pic/Spatula.png +0 -0
  49. data/object_pic/Spoon.png +0 -0
  50. data/object_pic/SprayBottle.png +0 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.jpg filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,13 +1,12 @@
1
  ---
2
- title: DynamicSceneGeneration
3
- emoji: 🦀
4
- colorFrom: indigo
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 4.37.2
8
  app_file: app.py
9
  pinned: false
10
- license: apache-2.0
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: DynamicGeneration
3
+ emoji: 😻
4
+ colorFrom: green
5
+ colorTo: pink
6
  sdk: gradio
7
+ sdk_version: 4.31.3
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from generate_person import init_display_person_dataframe, schedule_to_df, update_schedules, visualize_person_info
2
+ from generate_scene import person_info_to_description, reset_person_page, visualize_dynamic_generate, person_info_to_elements
3
+ from io import BytesIO
4
+ import pickle
5
+ from utils import read_character
6
+ import gradio as gr
7
+ from pathlib import Path
8
+
9
+ def add_character_click(character_file: str, person_name_to_file: dict[str, list[str|Path|dict]], person_name_to_info: dict[str, dict], person_name_to_description: dict[str, str], *reset_inputs):
10
+ # we can test this by add person and check if it is one the person choice dropdown
11
+ character_dict = pickle.load(open(character_file, 'rb'))
12
+ _, person_info, _ = read_character(character_dict)
13
+ person_name = person_info['name']
14
+ person_name_to_file[person_name] = character_dict
15
+ person_name_to_info[person_name] = person_info
16
+ person_name_to_description[person_name] = person_info_to_description(person_info)
17
+ return [
18
+ person_name_to_file, person_name_to_info, person_name_to_description, gr.update(choices=sorted(person_name_to_description.values())), gr.update(visible=False)
19
+ ] + reset_person_page(*reset_inputs)
20
+
21
+
22
+ def display_person_file(person_description_dropdown: str, person_name_to_file: dict[str, list[str|Path|dict]], person_name_to_info: dict[str, dict], person_name_to_description: dict[str, str]):
23
+ # get person name
24
+ person_name = [person_name for person_name, description in person_name_to_description.items() if description == person_description_dropdown][0]
25
+ # get person file
26
+ person_file = person_name_to_file[person_name]
27
+ _, _, schedules = read_character(person_file)
28
+ schedule_updates = update_schedules(schedules)
29
+
30
+ person_info = person_name_to_info[person_name]
31
+ return list(person_info_to_elements(person_info)) + schedule_updates
32
+
33
+ def create_person_page():
34
+ with gr.Row(visible=False) as row:
35
+ image = gr.Image(width=200, scale=0.25, show_label=False, interactive=False)
36
+ with gr.Column():
37
+ name = gr.Markdown()
38
+ age = gr.Markdown()
39
+ gender = gr.Markdown()
40
+ personality = gr.Markdown()
41
+ routine = gr.Markdown()
42
+ return row, (image, name, age, gender, personality, routine)
43
+
44
+ def person_info_page(person_name_to_file: gr.State, person_name_to_info: gr.State, person_name_to_description: gr.State):
45
+ with gr.Blocks():
46
+ upload_button = gr.UploadButton()
47
+ person_description_dropdown = gr.Dropdown(label="Person Description", choices=sorted(person_name_to_description.value.values()))
48
+ person_row, person_elements = create_person_page()
49
+ schedule_df_list, schedule_tab_list = init_display_person_dataframe()
50
+ person_description_dropdown.change(
51
+ fn=display_person_file,
52
+ inputs=[person_description_dropdown, person_name_to_file, person_name_to_info, person_name_to_description],
53
+ outputs=list(person_elements) + schedule_df_list + schedule_tab_list,
54
+ )
55
+ person_description_dropdown.change(
56
+ fn=lambda : gr.update(visible=True),
57
+ outputs=[person_row],
58
+ )
59
+ return person_description_dropdown, upload_button
60
+
61
+
62
+ def visualize():
63
+ with gr.Blocks() as demo:
64
+ with gr.Tab("Create Person"):
65
+ add_character_button, character_file, person_name_to_file, person_name_to_info, person_name_to_description = visualize_person_info()
66
+ with gr.Tab("Person Info"):
67
+ person_description_dropdown, upload_button = person_info_page(person_name_to_file, person_name_to_info, person_name_to_description)
68
+ with gr.Tab("Generate Scene"):
69
+ reset_inputs, reset_outputs = visualize_dynamic_generate(person_name_to_file, person_name_to_info, person_name_to_description)
70
+ add_character_button.click(
71
+ fn=add_character_click,
72
+ inputs=[character_file, person_name_to_file, person_name_to_info, person_name_to_description] + reset_inputs,
73
+ outputs=[person_name_to_file, person_name_to_info, person_name_to_description, person_description_dropdown, add_character_button] + reset_outputs
74
+ )
75
+ upload_button.upload(
76
+ fn=add_character_click,
77
+ inputs=[upload_button, person_name_to_file, person_name_to_info, person_name_to_description] + reset_inputs,
78
+ outputs=[person_name_to_file, person_name_to_info, person_name_to_description, person_description_dropdown, add_character_button] + reset_outputs
79
+ )
80
+ demo.launch()
81
+
82
+ if __name__ == "__main__":
83
+ # with gr.Blocks() as demo:
84
+ # with gr.Tab("Create Person"):
85
+ # visualize_person_info()
86
+ # with gr.Tab("Generate Scene"):
87
+ # visualize_dynamic_generate()
88
+ visualize()
89
+
90
+ # your_dictionary = {
91
+ # 'key1': 'value1',
92
+ # 'key2': 'value2',
93
+ # 'key3': 'value3'
94
+ # }
95
+
96
+ # # 将字典序列化为 pickle 格式的 bytes 对象
97
+ # def serialize_dictionary_to_bytes_obj(dictionary):
98
+ # # 使用 pickle 将字典序列化为 bytes 对象
99
+ # pickle_bytes = pickle.dumps(dictionary)
100
+
101
+ # # 返回 bytes 对象和文件名
102
+ # return pickle_bytes
103
+
104
+
105
+ # def process_and_return_file_path(dictionary):
106
+ # # 创建一个临时文件,该文件在关闭时会被自动删除
107
+ # with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
108
+ # # 向临时文件写入数据
109
+ # tmp_file.write(serialize_dictionary_to_bytes_obj(dictionary))
110
+ # # 获取临时文件的路径
111
+ # temp_file_path = tmp_file.name
112
+
113
+ # # 返回文件路径
114
+ # # 注意:这里我们不关闭文件,因为我们需要文件路径有效
115
+ # # 但这意味着文件在操作系统级别不会自动删除,需要手动管理
116
+ # return temp_file_path
117
+
118
+ # with gr.Blocks() as demo:
119
+ # file_path = process_and_return_file_path(your_dictionary)
120
+ # gr.File(file_path, label="Download File")
121
+ # demo.launch()
avatar_generation.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+ from pathlib import Path
3
+ import os
4
+ import requests
5
+ import tqdm
6
+
7
+ def generate_image(prompt, api_key: str, n=1, size="1024x1024"):
8
+ """
9
+ Generates an image using OpenAI's DALL-E API.
10
+
11
+ :param prompt: The prompt to generate an image for.
12
+ :param api_key: Your OpenAI API key.
13
+ :param n: The number of images to generate.
14
+ :param size: The size of the generated images.
15
+ :return: The response from the API call.
16
+ """
17
+ headers = {
18
+ "Content-Type": "application/json",
19
+ "Authorization": f"Bearer {api_key}"
20
+ }
21
+ payload = {
22
+ "model": "dall-e-3",
23
+ "prompt": prompt,
24
+ "n": n,
25
+ "size": size
26
+ }
27
+
28
+ response = requests.post("https://api.openai.com/v1/images/generations",
29
+ headers=headers,
30
+ json=payload)
31
+ return response
32
+
33
+ def get_prompt(persona: dict):
34
+ prompt = f"""A character named {persona['name']} is a {persona['age']} years old. The basic information of this character is as follows:
35
+ - Name: {persona['name']}
36
+ - Age: {persona['age']}
37
+ - Gender: {persona['gender']}
38
+ - Routine: {persona['routine']}
39
+ - Personality: {', '.join(persona['personality'])}
40
+ - Occupation: {persona['occupation']}
41
+ - Thoughts: {persona['thoughts']}
42
+ - Lifestyle: {persona['lifestyle']}
43
+ Please generate a full-face photo of this character.
44
+ """
45
+ return prompt
46
+
47
+ def get_persona_avatar_bytes(persona: dict, api_key: str) -> bytes:
48
+ prompt = get_prompt(persona)
49
+ response = generate_image(prompt, api_key)
50
+ if response.status_code == 200:
51
+ # 这里可以添加代码来处理响应体,例如保存图像或进一步的处理
52
+ image_data = response.json()['data']
53
+ assert len(image_data) == 1
54
+ image_url = image_data[0]['url']
55
+ # download image
56
+ image_response = requests.get(image_url)
57
+ image = image_response.content
58
+ return image
59
+ else:
60
+ print(f"Failed to generate image: {response.status_code} - {response.text}")
61
+
62
+ def generate_for_cache(api_key: str):
63
+ root_path = Path("static/exist_characters")
64
+ character_dirs = os.listdir(root_path)
65
+ character_dirs = [root_path / character_dir for character_dir in character_dirs]
66
+
67
+ for character_dir in tqdm.tqdm(character_dirs):
68
+ character_path = character_dir / f"{character_dir.name}.pkl"
69
+ character_data = pickle.load(open(character_path, 'rb'))
70
+ image_path = character_dir / f"avatar.jpg"
71
+ image_bytes = get_persona_avatar_bytes(character_data['persona'], api_key)
72
+ if image_bytes:
73
+ with open(image_path, 'wb') as f:
74
+ f.write(image_bytes)
75
+
76
+
77
+ if __name__ == "__main__":
78
+ api_key = os.getenv("OPENAI_API_KEY")
79
+ generate_for_cache(api_key)
constants.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ OBJECTS = {
2
+ 'AlarmClock',
3
+ 'Apple',
4
+ 'ArmChair',
5
+ 'BaseballBat',
6
+ 'BasketBall',
7
+ 'Bathtub',
8
+ 'BathtubBasin',
9
+ 'Bed',
10
+ 'Blinds',
11
+ 'Book',
12
+ 'Boots',
13
+ 'Bowl',
14
+ 'Box',
15
+ 'Bread',
16
+ 'ButterKnife',
17
+ 'Cabinet',
18
+ 'Candle',
19
+ 'Cart',
20
+ 'CD',
21
+ 'CellPhone',
22
+ 'Chair',
23
+ 'Cloth',
24
+ 'CoffeeMachine',
25
+ 'CounterTop',
26
+ 'CreditCard',
27
+ 'Cup',
28
+ 'Curtains',
29
+ 'Desk',
30
+ 'DeskLamp',
31
+ 'DishSponge',
32
+ 'Drawer',
33
+ 'Dresser',
34
+ 'Egg',
35
+ 'FloorLamp',
36
+ 'Footstool',
37
+ 'Fork',
38
+ 'Fridge',
39
+ 'GarbageCan',
40
+ 'Glassbottle',
41
+ 'HandTowel',
42
+ 'HandTowelHolder',
43
+ 'HousePlant',
44
+ 'Kettle',
45
+ 'KeyChain',
46
+ 'Knife',
47
+ 'Ladle',
48
+ 'Laptop',
49
+ 'LaundryHamper',
50
+ 'LaundryHamperLid',
51
+ 'Lettuce',
52
+ 'LightSwitch',
53
+ 'Microwave',
54
+ 'Mirror',
55
+ 'Mug',
56
+ 'Newspaper',
57
+ 'Ottoman',
58
+ 'Painting',
59
+ 'Pan',
60
+ 'PaperTowel',
61
+ 'PaperTowelRoll',
62
+ 'Pen',
63
+ 'Pencil',
64
+ 'PepperShaker',
65
+ 'Pillow',
66
+ 'Plate',
67
+ 'Plunger',
68
+ 'Poster',
69
+ 'Pot',
70
+ 'Potato',
71
+ 'RemoteControl',
72
+ 'Safe',
73
+ 'SaltShaker',
74
+ 'ScrubBrush',
75
+ 'Shelf',
76
+ 'ShelvingUnit',
77
+ 'ShowerDoor',
78
+ 'ShowerGlass',
79
+ 'Sink',
80
+ 'SinkBasin',
81
+ 'SoapBar',
82
+ 'SoapBottle',
83
+ 'Sofa',
84
+ 'Spatula',
85
+ 'Spoon',
86
+ 'SprayBottle',
87
+ 'Statue',
88
+ 'StoveBurner',
89
+ 'StoveKnob',
90
+ 'DiningTable',
91
+ 'CoffeeTable',
92
+ 'SideTable',
93
+ 'TeddyBear',
94
+ 'Television',
95
+ 'TennisRacket',
96
+ 'TissueBox',
97
+ 'Toaster',
98
+ 'Toilet',
99
+ 'ToiletPaper',
100
+ 'ToiletPaperHanger',
101
+ 'ToiletPaperRoll',
102
+ 'Tomato',
103
+ 'Towel',
104
+ 'TowelHolder',
105
+ 'TVStand',
106
+ 'Vase',
107
+ 'Watch',
108
+ 'WateringCan',
109
+ 'Window',
110
+ 'WineBottle',
111
+ 'WashingMachine'
112
+ }
113
+
114
+ RECEPTACLES = {
115
+ 'BathtubBasin',
116
+ 'Bowl',
117
+ 'Cup',
118
+ 'Drawer',
119
+ 'Mug',
120
+ 'Plate',
121
+ 'Shelf',
122
+ 'ShelvingUnit',
123
+ 'Sink',
124
+ 'SinkBasin',
125
+ 'Box',
126
+ 'Cabinet',
127
+ 'CoffeeMachine',
128
+ 'CounterTop',
129
+ 'Fridge',
130
+ 'GarbageCan',
131
+ 'HandTowelHolder',
132
+ 'Microwave',
133
+ 'PaintingHanger',
134
+ 'Pan',
135
+ 'Pot',
136
+ 'StoveBurner',
137
+ 'DiningTable',
138
+ 'CoffeeTable',
139
+ 'SideTable',
140
+ 'ToiletPaperHanger',
141
+ 'TowelHolder',
142
+ 'Safe',
143
+ 'BathtubBasin',
144
+ 'ArmChair',
145
+ 'Toilet',
146
+ 'Sofa',
147
+ 'Ottoman',
148
+ 'Dresser',
149
+ 'LaundryHamper',
150
+ 'Desk',
151
+ 'Bed',
152
+ 'Cart',
153
+ 'TVStand',
154
+ 'Toaster',
155
+ 'WashingMachine'
156
+ }
data/object_pic/AlarmClock.png ADDED
data/object_pic/Apple.png ADDED
data/object_pic/BaseballBat.png ADDED
data/object_pic/BasketBall.png ADDED
data/object_pic/Book.png ADDED
data/object_pic/Boots.png ADDED
data/object_pic/Bottle.png ADDED
data/object_pic/Bowl.png ADDED
data/object_pic/Box.png ADDED
data/object_pic/Bread.png ADDED
data/object_pic/ButterKnife.png ADDED
data/object_pic/Candle.png ADDED
data/object_pic/CellPhone.png ADDED
data/object_pic/Cloth.png ADDED
data/object_pic/CreditCard.png ADDED
data/object_pic/Cup.png ADDED
data/object_pic/DishSponge.png ADDED
data/object_pic/Egg.png ADDED
data/object_pic/Fork.png ADDED
data/object_pic/Kettle.png ADDED
data/object_pic/KeyChain.png ADDED
data/object_pic/Knife.png ADDED
data/object_pic/Ladle.png ADDED
data/object_pic/Laptop.png ADDED
data/object_pic/Lettuce.png ADDED
data/object_pic/Mug.png ADDED
data/object_pic/Newspaper.png ADDED
data/object_pic/Pan.png ADDED
data/object_pic/PaperTowelRoll.png ADDED
data/object_pic/Pen.png ADDED
data/object_pic/Pencil.png ADDED
data/object_pic/PepperShaker.png ADDED
data/object_pic/Phone.png ADDED
data/object_pic/Pillow.png ADDED
data/object_pic/Plate.png ADDED
data/object_pic/Plunger.png ADDED
data/object_pic/Pot.png ADDED
data/object_pic/Potato.png ADDED
data/object_pic/RemoteControl.png ADDED
data/object_pic/SaltShaker.png ADDED
data/object_pic/SoapBar.png ADDED
data/object_pic/SoapBottle.png ADDED
data/object_pic/Spatula.png ADDED
data/object_pic/Spoon.png ADDED
data/object_pic/SprayBottle.png ADDED