Shahadbal commited on
Commit
46f7283
·
verified ·
1 Parent(s): 6b49d30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -27
app.py CHANGED
@@ -1,37 +1,30 @@
1
  import gradio as gr
2
- from transformers import GPT2LMHeadModel, GPT2Tokenizer
 
3
 
4
- # Load pre-trained GPT-2 model and tokenizer
5
- model_name = "gpt2-medium" # You can use "gpt2", "gpt2-medium", "gpt2-large", or "gpt2-xl"
6
- model = GPT2LMHeadModel.from_pretrained(model_name)
7
- tokenizer = GPT2Tokenizer.from_pretrained(model_name)
8
 
9
- # Function to generate text based on input
10
- def generate_story(prompt, choice1="", choice2="", choice3=""):
11
- # Combine the prompt with the chosen path
12
- full_prompt = prompt + "\n" + choice1 + "\n" + choice2 + "\n" + choice3
13
- inputs = tokenizer.encode(full_prompt, return_tensors="pt")
14
- outputs = model.generate(inputs, max_length=150, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
15
- text = tokenizer.decode(outputs[0], skip_special_tokens=True)
16
- return text
17
 
18
  # Gradio interface
19
- def adventure_game(prompt, choice1, choice2, choice3):
20
- story = generate_story(prompt, choice1, choice2, choice3)
21
- return story
22
-
23
- # Create Gradio interface
24
  iface = gr.Interface(
25
- fn=adventure_game,
26
  inputs=[
27
- gr.Textbox(label="Adventure Start", placeholder="Enter the beginning of your adventure..."),
28
- gr.Textbox(label="Choice 1", placeholder="Enter your first choice..."),
29
- gr.Textbox(label="Choice 2", placeholder="Enter your second choice..."),
30
- gr.Textbox(label="Choice 3", placeholder="Enter your third choice...")
31
- ],
32
- outputs="text",
33
- title="Text-Based Adventure Game Generator",
34
- description="Create your own adventure by entering the beginning of a story and making choices to continue the adventure."
35
  )
36
 
37
  iface.launch()
 
1
  import gradio as gr
2
+ from diffusers import StableDiffusionPipeline
3
+ import torch
4
 
5
+ # Load a lightweight stable diffusion model
6
+ model_id = "CompVis/stable-diffusion-v1-4"
7
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
8
+ pipe = pipe.to("cuda") # Use GPU for faster generation, or .to("cpu") if no GPU is available
9
 
10
+ def generate_character(name, gender, trait, superpower):
11
+ description = f"A {gender} character named {name}, who is {trait}. Their superpower is {superpower}."
12
+ # Generate an image based on the description
13
+ image = pipe(description).images[0]
14
+ return image
 
 
 
15
 
16
  # Gradio interface
 
 
 
 
 
17
  iface = gr.Interface(
18
+ fn=generate_character,
19
  inputs=[
20
+ gr.Textbox(label="Name", placeholder="Enter the character's name"),
21
+ gr.Radio(label="Gender", choices=["male", "female", "non-binary"]),
22
+ gr.Textbox(label="Personality Trait", placeholder="e.g., brave, clever, kind"),
23
+ gr.Textbox(label="Superpower", placeholder="e.g., flying, invisibility, super strength")
24
+ ],
25
+ outputs="image",
26
+ title="Lightweight Character Generator",
27
+ description="Enter the details of your character and generate an image!"
28
  )
29
 
30
  iface.launch()