aroffe commited on
Commit
eb1e067
·
verified ·
1 Parent(s): 25bd36f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -29
app.py CHANGED
@@ -1,41 +1,67 @@
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()
 
1
  import gradio as gr
2
+ import os
3
 
4
+ from huggingface_hub import list_liked_repos
5
+ from diffusers import DiffusionPipeline
6
 
7
+ def image_mod(prompt: str, model: str, image_0: gr.Image, image_1: gr.Image) -> list[gr.Image]:
8
+ images = [image_0, image_1]
9
+ for i, diffusion_model in enumerate(model):
10
+ pipeline = DiffusionPipeline.from_pretrained(pretrained_model_name_or_path=diffusion_model, device_map="auto")
11
+ images[i] = pipeline(prompt).images[0]
12
+ return images
13
 
14
+ def get_liked_models(username: str) -> list:
15
+ likes = list_liked_repos(username)
16
+ # filter if the models are diffusion models
17
+ return likes.models
18
 
19
+
20
+ def get_dropdown_for_huggingface(profile: gr.OAuthProfile):
21
+ return gr.Dropdown(
22
+ choices=get_liked_models(profile.username),
23
+ multiselect=True,
24
+ max_choices=2,
25
+ label="Select a Model from Hugginface",
26
+ info="Select up to two models to compare. If you want to see more models populated in the dropdown, just like a model on huggingface! The dropdown is refreshed every minute.",
27
+ every=60,
28
+ visible=True
29
+ )
30
 
31
 
32
+ def change_model(choice):
33
+ images = [gr.Image(visible=False), gr.Image(visible=False)]
34
+ for i, model in enumerate(choice):
35
+ images[i] = gr.Image(label=model, visible=True, interactive=False)
36
+
37
+ return images
38
+
39
  with gr.Blocks() as demo:
40
  gr.Markdown(
41
+ """
42
+ **Comparing Stable Diffusion Models**
43
+
44
+ See https://huggingface.co/docs/diffusers/main/en/api/diffusion_pipeline
45
+
46
+
47
+ """)
48
  gr.LoginButton()
49
+ dropdown = gr.Dropdown(visible=False)
50
+ demo.load(get_dropdown_for_huggingface, inputs=None, outputs=dropdown)
51
+
52
+ textbox = gr.Textbox(label="Prompt for Image Generation")
53
+
54
+ with gr.Row() as row:
55
+ image_0 = gr.Image(visible=False)
56
+ image_1 = gr.Image(visible=False)
57
+ dropdown.change(
58
+ fn=change_model,
59
+ inputs=dropdown,
60
+ outputs=[image_0, image_1]
61
+ )
62
+
63
+ textbox.submit(
64
+ image_mod, [textbox, dropdown, image_0, image_1], [image_0, image_1]
65
+ )
66
 
67
  demo.launch()