Spaces:
Sleeping
Sleeping
artificialguybr
commited on
Commit
•
dddb041
1
Parent(s):
27063b6
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import io
|
4 |
+
from PIL import Image
|
5 |
+
import json
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Load LoRAs from JSON
|
9 |
+
with open('loras.json', 'r') as f:
|
10 |
+
loras = json.load(f)
|
11 |
+
|
12 |
+
# API call function
|
13 |
+
def query(payload, api_url, token):
|
14 |
+
headers = {"Authorization": f"Bearer {token}"}
|
15 |
+
response = requests.post(api_url, headers=headers, json=payload)
|
16 |
+
return io.BytesIO(response.content)
|
17 |
+
|
18 |
+
# Gradio UI
|
19 |
+
with gr.Blocks(css="custom.css") as demo:
|
20 |
+
title = gr.HTML(
|
21 |
+
"""<h1><img src="https://i.imgur.com/vT48NAO.png" alt="LoRA"> LoRA the Explorer</h1>""",
|
22 |
+
elem_id="title",
|
23 |
+
)
|
24 |
+
selected_state = gr.State()
|
25 |
+
gallery = gr.Gallery(
|
26 |
+
value=[(item["image"], item["title"]) for item in loras],
|
27 |
+
label="LoRA Gallery",
|
28 |
+
allow_preview=False,
|
29 |
+
columns=3,
|
30 |
+
elem_id="gallery",
|
31 |
+
show_share_button=False
|
32 |
+
)
|
33 |
+
prompt = gr.Textbox(label="Prompt", show_label=False, lines=1, max_lines=1, placeholder="Type a prompt after selecting a LoRA", elem_id="prompt")
|
34 |
+
advanced_options = gr.Accordion("Advanced options", open=False)
|
35 |
+
weight = gr.Slider(0, 10, value=1, step=0.1, label="LoRA weight")
|
36 |
+
result = gr.Image(interactive=False, label="Generated Image", elem_id="result-image")
|
37 |
+
|
38 |
+
# Define the function to run when the button is clicked
|
39 |
+
def run_lora(prompt, weight, selected_state):
|
40 |
+
selected_lora = loras[selected_state]
|
41 |
+
api_url = f"https://api-inference.huggingface.co/models/{selected_lora['repo']}"
|
42 |
+
trigger_word = selected_lora["trigger_word"]
|
43 |
+
token = os.getenv("API_TOKEN")
|
44 |
+
payload = {"inputs": f"{prompt} {trigger_word}"}
|
45 |
+
|
46 |
+
image_bytes = query(payload, api_url, token)
|
47 |
+
return Image.open(image_bytes)
|
48 |
+
|
49 |
+
prompt.submit(
|
50 |
+
fn=run_lora,
|
51 |
+
inputs=[prompt, weight, selected_state],
|
52 |
+
outputs=[result],
|
53 |
+
)
|