MohamedRashad commited on
Commit
3930868
·
1 Parent(s): ef495e7

Enhance character generation by expanding CharacterDescription model and updating prompt template for JSON output

Browse files
Files changed (1) hide show
  1. app.py +20 -23
app.py CHANGED
@@ -6,11 +6,23 @@ from diffusers import FluxPipeline, AutoencoderKL
6
  from live_preview_helpers import flux_pipe_call_that_returns_an_iterable_of_images
7
  import spaces
8
  from google import genai
 
9
 
10
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
 
12
- # Configure Gemini API
13
  llm_client = genai.Client()
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to(device)
16
  good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=torch.bfloat16).to(device)
@@ -22,22 +34,11 @@ pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_ret
22
 
23
  ds = load_dataset("MohamedRashad/FinePersonas-Lite", split="train")
24
 
25
- prompt_template = """Generate a character with this persona description: {persona_description}
26
- In a world with this description: {world_description}
27
-
28
- Write the character in json format with the following fields:
29
- - name: The name of the character
30
- - background: The background of the character
31
- - appearance: The appearance of the character
32
- - personality: The personality of the character
33
- - skills_and_abilities: The skills and abilities of the character
34
- - goals: The goals of the character
35
- - conflicts: The conflicts of the character
36
- - backstory: The backstory of the character
37
- - current_situation: The current situation of the character
38
- - spoken_lines: The spoken lines of the character (list of strings)
39
-
40
- Don't write anything else except the character description in json format and don't include '```'.
41
  """
42
 
43
  world_description_prompt = "Generate a unique and random world description (Don't Write anything else except the world description)."
@@ -74,13 +75,9 @@ def generate_character(world_description, persona_description, progress=gr.Progr
74
  world_description=world_description
75
  )
76
  response = llm_client.models.generate_content(
77
- model="gemini-2.5-flash", contents=prompt_content
78
  )
79
- result = response.text
80
- if "</think>" in result:
81
- result = result[result.index("</think>")+len("</think>"):].strip()
82
- output = json.loads(result)
83
- return output
84
 
85
  app_description = """
86
  - This app generates a character in JSON format based on a persona description and a world description.
 
6
  from live_preview_helpers import flux_pipe_call_that_returns_an_iterable_of_images
7
  import spaces
8
  from google import genai
9
+ from pydantic import BaseModel
10
 
11
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
 
13
+ # Initialize Google Gemini API client
14
  llm_client = genai.Client()
15
+ class CharacterDescription(BaseModel):
16
+ name: str
17
+ background: str
18
+ appearance: str
19
+ personality: str
20
+ skills_and_abilities: str
21
+ goals: str
22
+ conflicts: str
23
+ backstory: str
24
+ current_situation: str
25
+ spoken_lines: list[str]
26
 
27
  pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to(device)
28
  good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=torch.bfloat16).to(device)
 
34
 
35
  ds = load_dataset("MohamedRashad/FinePersonas-Lite", split="train")
36
 
37
+ prompt_template = """Generate a character with this persona description:
38
+ {persona_description}
39
+ ---
40
+ In a world with this description:
41
+ {world_description}
 
 
 
 
 
 
 
 
 
 
 
42
  """
43
 
44
  world_description_prompt = "Generate a unique and random world description (Don't Write anything else except the world description)."
 
75
  world_description=world_description
76
  )
77
  response = llm_client.models.generate_content(
78
+ model="gemini-2.5-flash", contents=prompt_content, config={"response_mime_type": "application/json", "response_schema": CharacterDescription}
79
  )
80
+ return response.parsed
 
 
 
 
81
 
82
  app_description = """
83
  - This app generates a character in JSON format based on a persona description and a world description.