Spaces:
Sleeping
Sleeping
artificialguybr
commited on
Commit
•
c49f67b
1
Parent(s):
c133494
Update app.py
Browse files
app.py
CHANGED
@@ -12,24 +12,35 @@ with open('loras.json', 'r') as f:
|
|
12 |
# API call function
|
13 |
def query(payload, api_url, token):
|
14 |
headers = {"Authorization": f"Bearer {token}"}
|
15 |
-
print(f"Sending API request with payload: {payload}")
|
16 |
response = requests.post(api_url, headers=headers, json=payload)
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# Define the function to run when the button is clicked
|
20 |
-
def run_lora(prompt):
|
21 |
-
selected_lora = loras[
|
22 |
api_url = f"https://api-inference.huggingface.co/models/{selected_lora['repo']}"
|
23 |
trigger_word = selected_lora["trigger_word"]
|
24 |
token = os.getenv("API_TOKEN")
|
25 |
payload = {"inputs": f"{prompt} {trigger_word}"}
|
26 |
image_bytes = query(payload, api_url, token)
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
# Gradio UI
|
30 |
with gr.Blocks(css="custom.css") as app:
|
31 |
title = gr.HTML("<h1>LoRA the Explorer</h1>")
|
32 |
-
selected_state = gr.State()
|
33 |
with gr.Row():
|
34 |
gallery = gr.Gallery(
|
35 |
[(item["image"], item["title"]) for item in loras],
|
@@ -39,26 +50,16 @@ with gr.Blocks(css="custom.css") as app:
|
|
39 |
)
|
40 |
with gr.Column():
|
41 |
prompt_title = gr.Markdown("### Click on a LoRA in the gallery to select it")
|
42 |
-
|
43 |
-
prompt = gr.Textbox(label="Prompt", show_label=False, lines=1, max_lines=1, placeholder="Type a prompt after selecting a LoRA")
|
44 |
-
button = gr.Button("Run")
|
45 |
result = gr.Image(interactive=False, label="Generated Image")
|
46 |
|
47 |
-
# Placeholder for gallery.select function
|
48 |
-
def update_selection():
|
49 |
-
pass
|
50 |
-
|
51 |
gallery.select(
|
52 |
-
update_selection
|
|
|
53 |
)
|
54 |
prompt.submit(
|
55 |
fn=run_lora,
|
56 |
-
inputs=[prompt],
|
57 |
-
outputs=[result]
|
58 |
-
)
|
59 |
-
button.click(
|
60 |
-
fn=run_lora,
|
61 |
-
inputs=[prompt],
|
62 |
outputs=[result]
|
63 |
)
|
64 |
|
|
|
12 |
# API call function
|
13 |
def query(payload, api_url, token):
|
14 |
headers = {"Authorization": f"Bearer {token}"}
|
15 |
+
print(f"Sending API request with payload: {payload}")
|
16 |
response = requests.post(api_url, headers=headers, json=payload)
|
17 |
+
if response.status_code == 200:
|
18 |
+
return io.BytesIO(response.content)
|
19 |
+
else:
|
20 |
+
print(f"API Error: {response.text}")
|
21 |
+
return None
|
22 |
|
23 |
# Define the function to run when the button is clicked
|
24 |
+
def run_lora(prompt, selected_lora_index):
|
25 |
+
selected_lora = loras[selected_lora_index]
|
26 |
api_url = f"https://api-inference.huggingface.co/models/{selected_lora['repo']}"
|
27 |
trigger_word = selected_lora["trigger_word"]
|
28 |
token = os.getenv("API_TOKEN")
|
29 |
payload = {"inputs": f"{prompt} {trigger_word}"}
|
30 |
image_bytes = query(payload, api_url, token)
|
31 |
+
if image_bytes:
|
32 |
+
return Image.open(image_bytes)
|
33 |
+
else:
|
34 |
+
return "API Error"
|
35 |
+
|
36 |
+
# Placeholder for gallery.select function
|
37 |
+
def update_selection(selected):
|
38 |
+
return selected
|
39 |
|
40 |
# Gradio UI
|
41 |
with gr.Blocks(css="custom.css") as app:
|
42 |
title = gr.HTML("<h1>LoRA the Explorer</h1>")
|
43 |
+
selected_state = gr.State(0) # Initialize with the index of the first LoRA
|
44 |
with gr.Row():
|
45 |
gallery = gr.Gallery(
|
46 |
[(item["image"], item["title"]) for item in loras],
|
|
|
50 |
)
|
51 |
with gr.Column():
|
52 |
prompt_title = gr.Markdown("### Click on a LoRA in the gallery to select it")
|
53 |
+
prompt = gr.Textbox(label="Prompt", show_label=False, lines=1, max_lines=1, placeholder="Type a prompt after selecting a LoRA")
|
|
|
|
|
54 |
result = gr.Image(interactive=False, label="Generated Image")
|
55 |
|
|
|
|
|
|
|
|
|
56 |
gallery.select(
|
57 |
+
update_selection,
|
58 |
+
outputs=[selected_state]
|
59 |
)
|
60 |
prompt.submit(
|
61 |
fn=run_lora,
|
62 |
+
inputs=[prompt, selected_state],
|
|
|
|
|
|
|
|
|
|
|
63 |
outputs=[result]
|
64 |
)
|
65 |
|