PSNbst commited on
Commit
d9fb779
·
verified ·
1 Parent(s): 615607b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -16
app.py CHANGED
@@ -12,14 +12,22 @@ CAMERA_ANGLES = ["low-angle shot", "close-up shot", "bird's-eye view", "wide-ang
12
  QUALITY_PROMPTS = ["8k", "ultra-realistic", "high detail", "cinematic lighting", "award-winning"]
13
 
14
  # ========== 工具函数 ==========
15
- def load_candidates_from_file(file):
16
  """
17
- 从上传的文件中读取候选项。
18
  """
19
- if file is None:
20
- return []
21
- lines = file.decode("utf-8").split("\n")
22
- return [line.strip() for line in lines if line.strip()]
 
 
 
 
 
 
 
 
23
 
24
  def get_random_item(candidates):
25
  """
@@ -44,7 +52,7 @@ def generate_natural_language_description(tags, api_key=None):
44
  {"role": "user", "content": f"Here are the tags: {tags}\nPlease generate a vivid, imaginative scene description."}
45
  ],
46
  max_tokens=300,
47
- temperature=0.8,
48
  )
49
  return response["choices"][0]["message"]["content"].strip()
50
  except Exception as e:
@@ -59,15 +67,15 @@ def generate_natural_language_description(tags, api_key=None):
59
  f"with a {tags.get('expression', 'neutral expression')}, holding {tags.get('items', 'something')} among {tags.get('other_details', 'subtle details')}."
60
  )
61
 
62
- def generate_prompt(action_file, style_file, artist_file, character_file, api_key, selected_categories):
63
  """
64
  生成随机提示词和描述。
65
  """
66
  # 从文件加载候选项
67
- actions = load_candidates_from_file(action_file) if action_file else []
68
- styles = load_candidates_from_file(style_file) if style_file else []
69
- artists = load_candidates_from_file(artist_file) if artist_file else []
70
- characters = load_candidates_from_file(character_file) if character_file else []
71
 
72
  # 确定角色类型
73
  number_of_characters = ", ".join(selected_categories) if selected_categories else random.choice(["1girl", "1boy"])
@@ -114,8 +122,10 @@ def gradio_interface():
114
  with gr.Row():
115
  action_file = gr.File(label="Upload Action File (Optional)", file_types=[".txt"])
116
  style_file = gr.File(label="Upload Style File (Optional)", file_types=[".txt"])
117
- artist_file = gr.File(label="Upload Artist File (Optional)", file_types=[".txt"])
118
- character_file = gr.File(label="Upload Character File (Optional)", file_types=[".txt"])
 
 
119
 
120
  # 角色类型选择
121
  selected_categories = gr.CheckboxGroup(
@@ -134,7 +144,7 @@ def gradio_interface():
134
  # 按钮动作
135
  generate_button.click(
136
  generate_prompt,
137
- inputs=[action_file, style_file, artist_file, character_file, api_key_input, selected_categories],
138
  outputs=[tags_output, description_output],
139
  )
140
 
@@ -142,4 +152,5 @@ def gradio_interface():
142
 
143
  # 启动 Gradio 应用
144
  if __name__ == "__main__":
145
- gradio_interface().launch()
 
 
12
  QUALITY_PROMPTS = ["8k", "ultra-realistic", "high detail", "cinematic lighting", "award-winning"]
13
 
14
  # ========== 工具函数 ==========
15
+ def load_candidates_from_files(files):
16
  """
17
+ 从多个文件中加载候选项。
18
  """
19
+ all_lines = []
20
+ if files:
21
+ for file in files:
22
+ # 如果 `file` 是路径
23
+ if isinstance(file, str):
24
+ with open(file, "r", encoding="utf-8") as f:
25
+ all_lines.extend([line.strip() for line in f if line.strip()])
26
+ # 如果 `file` 是 Gradio 的文件对象
27
+ elif hasattr(file, "name"):
28
+ with open(file.name, "r", encoding="utf-8") as f:
29
+ all_lines.extend([line.strip() for line in f if line.strip()])
30
+ return all_lines
31
 
32
  def get_random_item(candidates):
33
  """
 
52
  {"role": "user", "content": f"Here are the tags: {tags}\nPlease generate a vivid, imaginative scene description."}
53
  ],
54
  max_tokens=300,
55
+ temperature=0.7,
56
  )
57
  return response["choices"][0]["message"]["content"].strip()
58
  except Exception as e:
 
67
  f"with a {tags.get('expression', 'neutral expression')}, holding {tags.get('items', 'something')} among {tags.get('other_details', 'subtle details')}."
68
  )
69
 
70
+ def generate_prompt(action_file, style_file, artist_files, character_files, api_key, selected_categories):
71
  """
72
  生成随机提示词和描述。
73
  """
74
  # 从文件加载候选项
75
+ actions = load_candidates_from_files([action_file]) if action_file else []
76
+ styles = load_candidates_from_files([style_file]) if style_file else []
77
+ artists = load_candidates_from_files(artist_files) if artist_files else []
78
+ characters = load_candidates_from_files(character_files) if character_files else []
79
 
80
  # 确定角色类型
81
  number_of_characters = ", ".join(selected_categories) if selected_categories else random.choice(["1girl", "1boy"])
 
122
  with gr.Row():
123
  action_file = gr.File(label="Upload Action File (Optional)", file_types=[".txt"])
124
  style_file = gr.File(label="Upload Style File (Optional)", file_types=[".txt"])
125
+
126
+ with gr.Row():
127
+ artist_files = gr.File(label="Upload Artist Files (Multiple Allowed)", file_types=[".txt"], file_types_description="Text Files", multiple=True)
128
+ character_files = gr.File(label="Upload Character Files (Multiple Allowed)", file_types=[".txt"], file_types_description="Text Files", multiple=True)
129
 
130
  # 角色类型选择
131
  selected_categories = gr.CheckboxGroup(
 
144
  # 按钮动作
145
  generate_button.click(
146
  generate_prompt,
147
+ inputs=[action_file, style_file, artist_files, character_files, api_key_input, selected_categories],
148
  outputs=[tags_output, description_output],
149
  )
150
 
 
152
 
153
  # 启动 Gradio 应用
154
  if __name__ == "__main__":
155
+ gradio_interface().launch(share=True)
156
+