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

Integrate Google Gemini API for character generation and update requirements

Browse files
Files changed (2) hide show
  1. app.py +15 -26
  2. requirements.txt +2 -1
app.py CHANGED
@@ -5,10 +5,13 @@ import torch
5
  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 huggingface_hub import InferenceClient
9
 
10
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
 
 
 
 
12
  pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to(device)
13
  good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=torch.bfloat16).to(device)
14
  # pipe.enable_sequential_cpu_offload()
@@ -17,10 +20,6 @@ good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfold
17
  # pipe.to(torch.float16)
18
  pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
19
 
20
- llm_client = InferenceClient(
21
- provider="fireworks-ai",
22
- )
23
-
24
  ds = load_dataset("MohamedRashad/FinePersonas-Lite", split="train")
25
 
26
  prompt_template = """Generate a character with this persona description: {persona_description}
@@ -44,16 +43,10 @@ Don't write anything else except the character description in json format and do
44
  world_description_prompt = "Generate a unique and random world description (Don't Write anything else except the world description)."
45
 
46
  def get_random_world_description():
47
- response = llm_client.chat.completions.create(
48
- model="Qwen/Qwen3-235B-A22B",
49
- messages=[
50
- {
51
- "role": "user",
52
- "content": world_description_prompt
53
- }
54
- ],
55
  )
56
- result = response.choices[0].message.content
57
  if "</think>" in result:
58
  result = result[result.index("</think>")+len("</think>"):].strip()
59
  return result
@@ -76,18 +69,14 @@ def infer_flux(character_json):
76
  yield image
77
 
78
  def generate_character(world_description, persona_description, progress=gr.Progress(track_tqdm=True)):
79
- response = llm_client.chat.completions.create(
80
- model="Qwen/Qwen3-235B-A22B",
81
- messages=[
82
- {
83
- "role": "user",
84
- "content": prompt_template.format(
85
- persona_description=persona_description, world_description=world_description
86
- )
87
- }
88
- ],
89
  )
90
- result = response.choices[0].message.content
91
  if "</think>" in result:
92
  result = result[result.index("</think>")+len("</think>"):].strip()
93
  output = json.loads(result)
@@ -95,7 +84,7 @@ def generate_character(world_description, persona_description, progress=gr.Progr
95
 
96
  app_description = """
97
  - This app generates a character in JSON format based on a persona description and a world description.
98
- - 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 [Qwen3-235B-A22B](https://huggingface.co/Qwen/Qwen3-235B-A22B).
99
  - The persona description is randomly selected from the [FinePersonas-Lite](https://huggingface.co/datasets/MohamedRashad/FinePersonas-Lite) dataset.
100
 
101
  **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.
 
5
  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)
17
  # pipe.enable_sequential_cpu_offload()
 
20
  # pipe.to(torch.float16)
21
  pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
22
 
 
 
 
 
23
  ds = load_dataset("MohamedRashad/FinePersonas-Lite", split="train")
24
 
25
  prompt_template = """Generate a character with this persona description: {persona_description}
 
43
  world_description_prompt = "Generate a unique and random world description (Don't Write anything else except the world description)."
44
 
45
  def get_random_world_description():
46
+ response = llm_client.models.generate_content(
47
+ model="gemini-2.5-flash", contents=world_description_prompt
 
 
 
 
 
 
48
  )
49
+ result = response.text
50
  if "</think>" in result:
51
  result = result[result.index("</think>")+len("</think>"):].strip()
52
  return result
 
69
  yield image
70
 
71
  def generate_character(world_description, persona_description, progress=gr.Progress(track_tqdm=True)):
72
+ prompt_content = prompt_template.format(
73
+ persona_description=persona_description,
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)
 
84
 
85
  app_description = """
86
  - This app generates a character in JSON format based on a persona description and a world description.
87
+ - 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 Google Gemini 2.5 Flash.
88
  - The persona description is randomly selected from the [FinePersonas-Lite](https://huggingface.co/datasets/MohamedRashad/FinePersonas-Lite) dataset.
89
 
90
  **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.
requirements.txt CHANGED
@@ -6,4 +6,5 @@ torch
6
  diffusers
7
  gradio_client
8
  datasets
9
- numpy
 
 
6
  diffusers
7
  gradio_client
8
  datasets
9
+ numpy
10
+ google-genai