MohamedRashad commited on
Commit
7173d5d
1 Parent(s): 59a8e0f

chore: Refactor character generation and image inference in app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -20
app.py CHANGED
@@ -55,22 +55,10 @@ def get_random_world_description():
55
  def get_random_persona_description():
56
  return ds.shuffle().select([100])[0]["persona"]
57
 
58
- @spaces.GPU()
59
- def generate_character(world_description, persona_description, progress=gr.Progress(track_tqdm=True)):
60
- result = llm_client.predict(
61
- query=prompt_template.format(
62
- persona_description=persona_description, world_description=world_description
63
- ),
64
- history=[],
65
- system="You are Qwen, created by Alibaba Cloud. You are a helpful assistant.",
66
- api_name="/model_chat",
67
- )
68
- output = json.loads(result[1][0][-1])
69
- print("Character generated")
70
- print(json.dumps(output, indent=4))
71
-
72
  for image in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
73
- prompt=output["appearance"],
74
  guidance_scale=3.5,
75
  num_inference_steps=28,
76
  width=1024,
@@ -79,14 +67,26 @@ def generate_character(world_description, persona_description, progress=gr.Progr
79
  output_type="pil",
80
  good_vae=good_vae,
81
  ):
82
- yield image, output
83
- print("Character and image generated")
84
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  app_description = """
87
  - This app generates a character in JSON format based on a persona description and a world description.
88
  - The character's appearance is generated using [FLUX-dev](https://huggingface.co/black-forest-labs/FLUX.1-dev) and the character description is generated using [Qwen2.5-72B-Instruct](https://huggingface.co/Qwen/Qwen2.5-72B-Instruct).
89
  - The persona description is randomly selected from the [FinePersonas-Lite](https://huggingface.co/datasets/MohamedRashad/FinePersonas-Lite) dataset.
 
 
90
  """
91
 
92
  with gr.Blocks(title="Character Generator") as app:
@@ -103,7 +103,7 @@ with gr.Blocks(title="Character Generator") as app:
103
  random_persona_button = gr.Button(value="Get Random Persona Description", variant="secondary", scale=1)
104
  with gr.Row():
105
  character_image = gr.Image(label="Character Image")
106
- character = gr.JSON(label="Character Description")
107
 
108
  examples = gr.Examples(
109
  [
@@ -115,8 +115,8 @@ with gr.Blocks(title="Character Generator") as app:
115
  )
116
 
117
  submit_button.click(
118
- generate_character, [world_description, persona_description], outputs=[character_image, character]
119
- )
120
  random_world_button.click(
121
  get_random_world_description, outputs=[world_description]
122
  )
 
55
  def get_random_persona_description():
56
  return ds.shuffle().select([100])[0]["persona"]
57
 
58
+ @spaces.GPU(duration=75)
59
+ def infer_flux(character_json):
 
 
 
 
 
 
 
 
 
 
 
 
60
  for image in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
61
+ prompt=character_json["appearance"],
62
  guidance_scale=3.5,
63
  num_inference_steps=28,
64
  width=1024,
 
67
  output_type="pil",
68
  good_vae=good_vae,
69
  ):
70
+ yield image
 
71
 
72
+ def generate_character(world_description, persona_description, progress=gr.Progress(track_tqdm=True)):
73
+ result = llm_client.predict(
74
+ query=prompt_template.format(
75
+ persona_description=persona_description, world_description=world_description
76
+ ),
77
+ history=[],
78
+ system="You are Qwen, created by Alibaba Cloud. You are a helpful assistant.",
79
+ api_name="/model_chat",
80
+ )
81
+ output = json.loads(result[1][0][-1])
82
+ return output
83
 
84
  app_description = """
85
  - This app generates a character in JSON format based on a persona description and a world description.
86
  - The character's appearance is generated using [FLUX-dev](https://huggingface.co/black-forest-labs/FLUX.1-dev) and the character description is generated using [Qwen2.5-72B-Instruct](https://huggingface.co/Qwen/Qwen2.5-72B-Instruct).
87
  - The persona description is randomly selected from the [FinePersonas-Lite](https://huggingface.co/datasets/MohamedRashad/FinePersonas-Lite) dataset.
88
+
89
+ **Note:** I recommend starting with the world description (you can write one or loop over randomly generated ones) and then try different persona descriptions to generate interesting characters for the world you created.
90
  """
91
 
92
  with gr.Blocks(title="Character Generator") as app:
 
103
  random_persona_button = gr.Button(value="Get Random Persona Description", variant="secondary", scale=1)
104
  with gr.Row():
105
  character_image = gr.Image(label="Character Image")
106
+ character_json = gr.JSON(label="Character Description")
107
 
108
  examples = gr.Examples(
109
  [
 
115
  )
116
 
117
  submit_button.click(
118
+ generate_character, [world_description, persona_description], outputs=[character_json]
119
+ ).then(fn=infer_flux, inputs=[character_json], outputs=[character_image])
120
  random_world_button.click(
121
  get_random_world_description, outputs=[world_description]
122
  )