throaway2854 commited on
Commit
0c5b16e
·
verified ·
1 Parent(s): 4b77b13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -6
app.py CHANGED
@@ -2,13 +2,22 @@ import gradio as gr
2
  import random
3
  import json
4
  import os
5
- from huggingface_hub import HfApi, hf_hub_download, upload_file
 
6
 
7
- REPO_ID = "throaway2854/promptGen" # Replace with your actual Space name
8
  DATA_FILE = "saved_data.json"
9
  IMAGES_DIR = "character_images"
10
 
 
 
 
 
 
 
 
11
  def load_data():
 
12
  try:
13
  file_path = hf_hub_download(repo_id=REPO_ID, filename=DATA_FILE)
14
  with open(file_path, 'r') as f:
@@ -21,6 +30,7 @@ def load_data():
21
  }
22
 
23
  def save_data(data):
 
24
  with open(DATA_FILE, 'w') as f:
25
  json.dump(data, f)
26
  api = HfApi()
@@ -32,6 +42,7 @@ def save_data(data):
32
  )
33
 
34
  def save_character_image(name, image):
 
35
  if not os.path.exists(IMAGES_DIR):
36
  os.makedirs(IMAGES_DIR)
37
  image_path = os.path.join(IMAGES_DIR, f"{name}.png")
@@ -137,10 +148,8 @@ def create_ui():
137
  with gr.Column():
138
  output = gr.Textbox(label="Generated Prompt", lines=5)
139
 
140
- with gr.Row():
141
- char_images = [char_data["image"] for char_data in data["characters"].values() if char_data["image"]]
142
- char_names = [char_name for char_name, char_data in data["characters"].items() if char_data["image"]]
143
- gr.Gallery(value=char_images, label="Character Images", show_label=True, elem_id="char_gallery", columns=2, rows=2, height="auto")
144
 
145
  with gr.TabItem("Character Creation"):
146
  with gr.Row():
 
2
  import random
3
  import json
4
  import os
5
+ from huggingface_hub import HfApi, hf_hub_download, upload_file, create_repo
6
+ from huggingface_hub.utils import RepositoryNotFoundError
7
 
8
+ REPO_ID = "throaway2854/promptGen"
9
  DATA_FILE = "saved_data.json"
10
  IMAGES_DIR = "character_images"
11
 
12
+ def ensure_repo_exists():
13
+ try:
14
+ api = HfApi()
15
+ api.repo_info(repo_id=REPO_ID, repo_type="space")
16
+ except RepositoryNotFoundError:
17
+ create_repo(REPO_ID, repo_type="space", space_sdk="gradio")
18
+
19
  def load_data():
20
+ ensure_repo_exists()
21
  try:
22
  file_path = hf_hub_download(repo_id=REPO_ID, filename=DATA_FILE)
23
  with open(file_path, 'r') as f:
 
30
  }
31
 
32
  def save_data(data):
33
+ ensure_repo_exists()
34
  with open(DATA_FILE, 'w') as f:
35
  json.dump(data, f)
36
  api = HfApi()
 
42
  )
43
 
44
  def save_character_image(name, image):
45
+ ensure_repo_exists()
46
  if not os.path.exists(IMAGES_DIR):
47
  os.makedirs(IMAGES_DIR)
48
  image_path = os.path.join(IMAGES_DIR, f"{name}.png")
 
148
  with gr.Column():
149
  output = gr.Textbox(label="Generated Prompt", lines=5)
150
 
151
+ char_images = [char_data["image"] for char_data in data["characters"].values() if char_data["image"]]
152
+ gr.Gallery(value=char_images, label="Character Images", show_label=True, elem_id="char_gallery", columns=2, rows=2, height="auto")
 
 
153
 
154
  with gr.TabItem("Character Creation"):
155
  with gr.Row():