multimodalart HF Staff commited on
Commit
ea02521
·
verified ·
1 Parent(s): 3cd1f91

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -0
app.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import fal_client
3
+ import os
4
+ from huggingface_hub import HfApi, whoami
5
+ from gradio.processing_utils import file_to_temp_file
6
+
7
+ try:
8
+ FAL_KEY = os.getenv("FAL_KEY")
9
+ except AttributeError:
10
+ FAL_KEY = ""
11
+
12
+
13
+ def get_fal_key():
14
+ if not FAL_KEY:
15
+ raise gr.Error("FAL_KEY is not set. Please add it to your secrets.")
16
+ return FAL_KEY
17
+
18
+
19
+ def generate_image(prompt, image=None):
20
+ """
21
+ Generates an image from a text prompt or edits an existing image.
22
+ If an image is provided, it performs image-to-image generation.
23
+ Otherwise, it performs text-to-image generation.
24
+ """
25
+ get_fal_key()
26
+
27
+ if image:
28
+ # Image-to-Image
29
+ temp_file_path = file_to_temp_file(image, dir=".")
30
+ result = fal_client.run(
31
+ "fal-ai/nano-banana/edit",
32
+ arguments={
33
+ "prompt": prompt,
34
+ "image_url": temp_file_path,
35
+ },
36
+ )
37
+ else:
38
+ # Text-to-Image
39
+ result = fal_client.run(
40
+ "fal-ai/nano-banana",
41
+ arguments={
42
+ "prompt": prompt,
43
+ },
44
+ )
45
+ return result["images"][0]["url"]
46
+
47
+
48
+ def multi_image_edit(prompt, images):
49
+ """
50
+ Edits multiple images based on a text prompt.
51
+ """
52
+ get_fal_key()
53
+
54
+ if not images:
55
+ raise gr.Error("Please upload at least one image.")
56
+
57
+ output_images = []
58
+ for image in images:
59
+ temp_file_path = file_to_temp_file(image, dir=".")
60
+ result = fal_client.run(
61
+ "fal-ai/nano-banana/edit",
62
+ arguments={
63
+ "prompt": prompt,
64
+ "image_url": temp_file_path,
65
+ },
66
+ )
67
+ output_images.append(result["images"][0]["url"])
68
+
69
+ return output_images
70
+
71
+
72
+ with gr.Blocks() as demo:
73
+ gr.Markdown("# Nano Banana Image Generation")
74
+ gr.Markdown(
75
+ "Generate or edit images using the FAL Nano Banana model. "
76
+ "Login with Hugging Face to use the app."
77
+ )
78
+
79
+ login_button = gr.LoginButton()
80
+ main_interface = gr.Blocks(visible=False)
81
+
82
+ with main_interface:
83
+ with gr.Tabs():
84
+ with gr.TabItem("Generate"):
85
+ with gr.Row():
86
+ with gr.Column():
87
+ prompt_input = gr.Textbox(label="Prompt")
88
+ image_input = gr.Image(type="filepath", label="Input Image (Optional)")
89
+ generate_button = gr.Button("Generate")
90
+ with gr.Column():
91
+ generate_output = gr.Image(label="Output")
92
+ generate_button.click(
93
+ generate_image,
94
+ inputs=[prompt_input, image_input],
95
+ outputs=[generate_output],
96
+ )
97
+
98
+ with gr.TabItem("Multi-Image Edit"):
99
+ with gr.Row():
100
+ with gr.Column():
101
+ multi_prompt_input = gr.Textbox(label="Prompt")
102
+ multi_image_input = gr.Gallery(label="Input Images")
103
+ multi_edit_button = gr.Button("Edit Images")
104
+ with gr.Column():
105
+ multi_image_output = gr.Gallery(label="Output Images")
106
+ multi_edit_button.click(
107
+ multi_image_edit,
108
+ inputs=[multi_prompt_input, multi_image_input],
109
+ outputs=[multi_image_output],
110
+ )
111
+
112
+ def show_interface(token):
113
+ if token:
114
+ try:
115
+ whoami(token)
116
+ return gr.update(visible=True)
117
+ except Exception:
118
+ return gr.update(visible=False)
119
+ return gr.update(visible=False)
120
+
121
+ login_button.login(show_interface, None, main_interface)
122
+
123
+
124
+ if __name__ == "__main__":
125
+ demo.launch()