Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,126 +1,41 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
#
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
if
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
"prompt": prompt,
|
43 |
-
},
|
44 |
-
)
|
45 |
-
return result["images"][0]["url"]
|
46 |
-
|
47 |
-
def multi_image_edit(prompt: str, images: list[str]) -> list[str]:
|
48 |
-
"""Edits multiple images based on a text prompt."""
|
49 |
-
get_fal_key()
|
50 |
-
|
51 |
-
if not images:
|
52 |
-
raise gr.Error("Please upload at least one image.")
|
53 |
-
|
54 |
-
output_images = []
|
55 |
-
for image_path in images:
|
56 |
-
# Upload each image and get its URL
|
57 |
-
image_url = fal_client.upload_file(image_path)
|
58 |
-
result = fal_client.run(
|
59 |
-
"fal-ai/nano-banana/edit",
|
60 |
-
arguments={
|
61 |
-
"prompt": prompt,
|
62 |
-
"image_url": image_url,
|
63 |
-
},
|
64 |
-
)
|
65 |
-
output_images.append(result["images"][0]["url"])
|
66 |
-
|
67 |
-
return output_images
|
68 |
-
|
69 |
-
# --- Gradio App UI ---
|
70 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
71 |
-
gr.Markdown("# Nano Banana Image Generation")
|
72 |
-
gr.Markdown("Generate or edit images with FAL. **Sign in with Hugging Face to begin.**")
|
73 |
-
|
74 |
-
login_button = gr.LoginButton()
|
75 |
-
|
76 |
-
# This entire Column will be hidden until the user logs in
|
77 |
-
main_interface = gr.Column(visible=False)
|
78 |
-
|
79 |
-
with main_interface:
|
80 |
-
gr.Markdown("## Welcome! You are logged in.")
|
81 |
-
with gr.Tabs():
|
82 |
-
with gr.TabItem("Generate"):
|
83 |
-
with gr.Row():
|
84 |
-
with gr.Column(scale=1):
|
85 |
-
prompt_input = gr.Textbox(label="Prompt", placeholder="A delicious looking pizza")
|
86 |
-
image_input = gr.Image(type="filepath", label="Input Image (Optional)")
|
87 |
-
generate_button = gr.Button("Generate", variant="primary")
|
88 |
-
with gr.Column(scale=1):
|
89 |
-
generate_output = gr.Image(label="Output")
|
90 |
-
|
91 |
-
generate_button.click(
|
92 |
-
generate_image,
|
93 |
-
inputs=[prompt_input, image_input],
|
94 |
-
outputs=[generate_output],
|
95 |
-
)
|
96 |
-
|
97 |
-
with gr.TabItem("Multi-Image Edit"):
|
98 |
-
with gr.Row():
|
99 |
-
with gr.Column(scale=1):
|
100 |
-
multi_prompt_input = gr.Textbox(label="Prompt", placeholder="Make it black and white")
|
101 |
-
multi_image_input = gr.Gallery(label="Input Images", file_types=["image"])
|
102 |
-
multi_edit_button = gr.Button("Edit Images", variant="primary")
|
103 |
-
with gr.Column(scale=1):
|
104 |
-
multi_image_output = gr.Gallery(label="Output Images")
|
105 |
-
|
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_app_on_login(profile: Optional[gr.OAuthProfile] = None):
|
113 |
-
"""
|
114 |
-
Controls the visibility of the main UI.
|
115 |
-
Gradio automatically injects `gr.OAuthProfile` if the user is logged in.
|
116 |
-
"""
|
117 |
-
if profile:
|
118 |
-
return gr.update(visible=True)
|
119 |
-
return gr.update(visible=False)
|
120 |
-
|
121 |
-
# When the app loads or the user logs in/out, this function will be called.
|
122 |
-
# It receives the user's profile (or None) and updates the UI visibility.
|
123 |
-
demo.load(show_app_on_login, inputs=None, outputs=main_interface)
|
124 |
-
|
125 |
-
if __name__ == "__main__":
|
126 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import list_models
|
3 |
+
|
4 |
+
|
5 |
+
def hello(profile: gr.OAuthProfile | None) -> str:
|
6 |
+
# ^ expect a gr.OAuthProfile object as input to get the user's profile
|
7 |
+
# if the user is not logged in, profile will be None
|
8 |
+
if profile is None:
|
9 |
+
return "I don't know you."
|
10 |
+
return f"Hello {profile.name}"
|
11 |
+
|
12 |
+
|
13 |
+
def list_private_models(profile: gr.OAuthProfile | None, oauth_token: gr.OAuthToken | None) -> str:
|
14 |
+
# ^ expect a gr.OAuthToken object as input to get the user's token
|
15 |
+
# if the user is not logged in, oauth_token will be None
|
16 |
+
if oauth_token is None:
|
17 |
+
return "Please log in to list private models."
|
18 |
+
models = [
|
19 |
+
f"{model.id} ({'private' if model.private else 'public'})"
|
20 |
+
for model in list_models(author=profile.username, token=oauth_token.token)
|
21 |
+
]
|
22 |
+
return "Models:\n\n" + "\n - ".join(models) + "."
|
23 |
+
|
24 |
+
|
25 |
+
with gr.Blocks() as demo:
|
26 |
+
gr.Markdown(
|
27 |
+
"# Gradio OAuth Space"
|
28 |
+
"\n\nThis Space is a demo for the **Sign in with Hugging Face** feature. "
|
29 |
+
"Duplicate this Space to get started."
|
30 |
+
"\n\nFor more details, check out:"
|
31 |
+
"\n- https://www.gradio.app/guides/sharing-your-app#o-auth-login-via-hugging-face"
|
32 |
+
"\n- https://huggingface.co/docs/hub/spaces-oauth"
|
33 |
+
)
|
34 |
+
gr.LoginButton()
|
35 |
+
# ^ add a login button to the Space
|
36 |
+
m1 = gr.Markdown()
|
37 |
+
m2 = gr.Markdown()
|
38 |
+
demo.load(hello, inputs=None, outputs=m1)
|
39 |
+
demo.load(list_private_models, inputs=None, outputs=m2)
|
40 |
+
|
41 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|