dev-immersfy commited on
Commit
b0a2e54
·
verified ·
1 Parent(s): f127546

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +147 -0
  2. constants.py +1 -0
  3. requirements.txt +99 -0
app.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import boto3
4
+ import os
5
+ from typing import List, Dict
6
+ import constants
7
+
8
+ # Initialize S3 client
9
+ s3 = boto3.client("s3")
10
+
11
+ LORA_OPTIONS = ["Arjun", "Khushi", "Priya", "Ishaan"]
12
+
13
+
14
+ def extract_unique_characters(json_file: str):
15
+ try:
16
+ with open(json_file, "r") as f:
17
+ data = json.load(f)
18
+ characters = set()
19
+ for item in data.get("result", []):
20
+ if item.get("character"):
21
+ characters.add(item["character"])
22
+ return sorted(list(characters)) # Sort to maintain consistent order
23
+ except Exception as e:
24
+ print(f"Error reading JSON: {str(e)}")
25
+ return []
26
+
27
+
28
+ def save_episode_json(
29
+ comic_id: str, epi_num: int, json_file: str, *character_lora_pairs
30
+ ):
31
+ try:
32
+ # Read the uploaded JSON file
33
+ with open(json_file, "r") as f:
34
+ json_content = json.load(f)
35
+
36
+ # Save the episode JSON with fixed name episode_old.json
37
+ save_path = f"{comic_id}/episodes/episode-{epi_num}/episode_old.json"
38
+ s3.put_object(
39
+ Bucket=constants.aws_bucket,
40
+ Key=save_path,
41
+ Body=json.dumps(json_content),
42
+ ContentType="application/json",
43
+ )
44
+
45
+ # Read existing metadata from S3
46
+ try:
47
+ response = s3.get_object(
48
+ Bucket=constants.aws_bucket, Key="comic_details/comic_details.json"
49
+ )
50
+ metadata = json.loads(response["Body"].read().decode("utf-8"))
51
+ except:
52
+ # If file doesn't exist, create new metadata with the given structure
53
+ metadata = {"comics": []}
54
+
55
+ # Create character-lora mapping in order
56
+ char_lora = {}
57
+ num_pairs = len(character_lora_pairs) // 2
58
+ for i in range(num_pairs):
59
+ character = character_lora_pairs[i] # Get character from first half
60
+ lora = character_lora_pairs[i + num_pairs] # Get lora from second half
61
+ if character: # Only add if character name is not empty
62
+ char_lora[character] = lora
63
+
64
+ # Update or add comic entry
65
+ comic_found = False
66
+ for comic in metadata["comics"]:
67
+ if comic["comic_id"] == comic_id:
68
+ # Update existing comic
69
+ comic["char_lora"] = char_lora
70
+ if epi_num not in comic["episode_number"]:
71
+ comic["episode_number"].append(epi_num)
72
+ comic["is_generate"] = False # Always set to false
73
+ comic_found = True
74
+ break
75
+
76
+ if not comic_found:
77
+ # Add new comic entry
78
+ metadata["comics"].append(
79
+ {
80
+ "comic_id": comic_id,
81
+ "char_lora": char_lora,
82
+ "episode_number": [epi_num],
83
+ "is_generate": False, # Always set to false
84
+ }
85
+ )
86
+
87
+ # Save updated metadata back to S3
88
+ s3.put_object(
89
+ Bucket=constants.aws_bucket,
90
+ Key="comic_details/comic_details.json",
91
+ Body=json.dumps(metadata, indent=4),
92
+ ContentType="application/json",
93
+ )
94
+
95
+ return "Successfully saved episode and updated metadata!"
96
+ except Exception as e:
97
+ return f"Error: {str(e)}"
98
+
99
+
100
+ def update_character_fields(json_file: str):
101
+ characters = extract_unique_characters(json_file)
102
+ if len(characters) > 6:
103
+ print(
104
+ f"Warning: Found {len(characters)} characters, but only showing first 6. Extra characters: {characters[5:]}"
105
+ )
106
+ characters = characters[:6] # Take only first 5 characters
107
+ # Pad with empty strings if we have fewer than 5 characters
108
+ return characters + [""] * (6 - len(characters))
109
+
110
+
111
+ with gr.Blocks() as demo:
112
+ gr.Markdown("# Comic Episode Uploader")
113
+
114
+ with gr.Row():
115
+ comic_id = gr.Textbox(label="Comic ID")
116
+ episode_num = gr.Number(label="Episode Number", precision=0)
117
+
118
+ json_file = gr.File(label="Upload JSON File", file_types=[".json"])
119
+
120
+ # Create 5 fixed character-lora pairs
121
+ character_boxes = []
122
+ lora_dropdowns = []
123
+ for i in range(6):
124
+ with gr.Row():
125
+ character = gr.Textbox(label=f"Character Name {i+1}", interactive=True)
126
+ lora = gr.Dropdown(choices=LORA_OPTIONS, label=f"Lora Name {i+1}")
127
+ character_boxes.append(character)
128
+ lora_dropdowns.append(lora)
129
+
130
+ # Update character fields when JSON is uploaded
131
+ json_file.change(
132
+ fn=update_character_fields, inputs=[json_file], outputs=character_boxes
133
+ )
134
+
135
+ submit_btn = gr.Button("Submit")
136
+ output = gr.Textbox(label="Status")
137
+
138
+ submit_btn.click(
139
+ fn=save_episode_json,
140
+ inputs=[comic_id, episode_num, json_file] + character_boxes + lora_dropdowns,
141
+ outputs=output,
142
+ )
143
+
144
+ if __name__ == "__main__":
145
+ demo.launch(
146
+ auth=("admin", "Qrt@12*34#immersfy"), share=True, ssr_mode=False, debug=True
147
+ )
constants.py ADDED
@@ -0,0 +1 @@
 
 
1
+ aws_bucket = "blix-production"
requirements.txt ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==24.1.0
2
+ annotated-types==0.7.0
3
+ anyio==4.6.0
4
+ boto3==1.35.41
5
+ botocore==1.35.41
6
+ build==1.2.2.post1
7
+ cachetools==5.5.0
8
+ certifi==2024.8.30
9
+ cfgv==3.4.0
10
+ charset-normalizer==3.4.0
11
+ click==8.1.7
12
+ colorama==0.4.6
13
+ colorlog==6.8.2
14
+ dacite==1.9.2
15
+ distlib==0.3.9
16
+ distro==1.9.0
17
+ elevenlabs==1.57.0
18
+ exceptiongroup==1.2.2
19
+ fastapi==0.115.12
20
+ ffmpy==0.5.0
21
+ filelock==3.16.1
22
+ fsspec==2025.3.2
23
+ google-api-core==2.23.0
24
+ google-api-python-client==2.149.0
25
+ google-auth==2.35.0
26
+ google-auth-httplib2==0.2.0
27
+ google-auth-oauthlib==1.2.1
28
+ googleapis-common-protos==1.66.0
29
+ gradio==5.28.0
30
+ gradio_client==1.10.0
31
+ groovy==0.1.2
32
+ h11==0.14.0
33
+ httpcore==1.0.6
34
+ httplib2==0.22.0
35
+ httpx==0.27.2
36
+ huggingface-hub==0.30.2
37
+ identify==2.6.1
38
+ idna==3.10
39
+ Jinja2==3.1.4
40
+ jmespath==1.0.1
41
+ markdown-it-py==3.0.0
42
+ MarkupSafe==3.0.1
43
+ mdurl==0.1.2
44
+ nodeenv==1.9.1
45
+ numpy==2.2.5
46
+ oauth2client==4.1.3
47
+ oauthlib==3.2.2
48
+ openai==1.23.6
49
+ orjson==3.10.18
50
+ packaging==24.1
51
+ pandas==2.2.3
52
+ pillow==11.0.0
53
+ pip==25.0
54
+ pip-tools==7.4.1
55
+ platformdirs==4.3.6
56
+ pre_commit==4.0.1
57
+ proto-plus==1.25.0
58
+ protobuf==5.28.3
59
+ pyasn1==0.6.1
60
+ pyasn1_modules==0.4.1
61
+ pydantic==2.9.2
62
+ pydantic_core==2.23.4
63
+ PyDrive==1.3.1
64
+ pydub==0.25.1
65
+ Pygments==2.19.1
66
+ PyMySQL==1.1.1
67
+ pyparsing==3.2.0
68
+ pyproject_hooks==1.2.0
69
+ python-dateutil==2.9.0.post0
70
+ python-dotenv==1.0.1
71
+ python-multipart==0.0.20
72
+ pytz==2025.2
73
+ PyYAML==6.0.2
74
+ requests==2.32.3
75
+ requests-oauthlib==2.0.0
76
+ rich==14.0.0
77
+ rsa==4.9
78
+ ruff==0.11.7
79
+ s3transfer==0.10.3
80
+ safehttpx==0.1.6
81
+ semantic-version==2.10.0
82
+ setuptools==75.8.0
83
+ shellingham==1.5.4
84
+ six==1.16.0
85
+ sniffio==1.3.1
86
+ starlette==0.46.2
87
+ tomli==2.0.2
88
+ tomlkit==0.13.2
89
+ tqdm==4.66.5
90
+ typer==0.15.3
91
+ typing_extensions==4.12.2
92
+ tzdata==2025.2
93
+ uritemplate==4.1.1
94
+ urllib3==2.2.3
95
+ uvicorn==0.32.0
96
+ virtualenv==20.26.6
97
+ websocket-client==1.8.0
98
+ websockets==15.0.1
99
+ wheel==0.44.0