multimodalart HF Staff commited on
Commit
200ac58
·
verified ·
1 Parent(s): 394f2f1

Update app.py

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