Spaces:
Runtime error
Runtime error
Commit
·
b36d8fb
1
Parent(s):
f5a25aa
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import io
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import json
|
| 6 |
+
import os
|
| 7 |
+
import shutil
|
| 8 |
+
import logging
|
| 9 |
+
import math
|
| 10 |
+
from tqdm import tqdm
|
| 11 |
+
import time
|
| 12 |
+
from diffusers import DiffusionPipeline
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def run_lora(lora, prompt, neg_prompt, progress=gr.Progress(track_tqdm=True)):
|
| 16 |
+
print(f"Inside run_lora, lora: {lora.name}, prompt: {prompt}, neg_prompt: {neg_prompt}")
|
| 17 |
+
|
| 18 |
+
api_url = f"https://api-inference.huggingface.co/models/{lora}"
|
| 19 |
+
payload = {
|
| 20 |
+
"inputs": f"{prompt}",
|
| 21 |
+
"parameters":{"negative_prompt": "bad art, ugly, watermark, deformed"},
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
# Add a print statement to display the API request
|
| 25 |
+
print(f"API Request: {api_url}")
|
| 26 |
+
print(f"API Payload: {payload}")
|
| 27 |
+
|
| 28 |
+
error_count = 0
|
| 29 |
+
pbar = tqdm(total=None, desc="Loading model")
|
| 30 |
+
while(True):
|
| 31 |
+
response = requests.post(api_url, json=payload)
|
| 32 |
+
if response.status_code == 200:
|
| 33 |
+
return Image.open(io.BytesIO(response.content))
|
| 34 |
+
elif response.status_code == 503:
|
| 35 |
+
#503 is triggered when the model is doing cold boot. It also gives you a time estimate from when the model is loaded but it is not super precise
|
| 36 |
+
time.sleep(1)
|
| 37 |
+
pbar.update(1)
|
| 38 |
+
elif response.status_code == 500 and error_count < 5:
|
| 39 |
+
print(response.content)
|
| 40 |
+
time.sleep(1)
|
| 41 |
+
error_count += 1
|
| 42 |
+
continue
|
| 43 |
+
else:
|
| 44 |
+
logging.error(f"API Error: {response.status_code}")
|
| 45 |
+
raise gr.Error("API Error: Unable to fetch the image.") # Raise a Gradio error here
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
app = gr.Interface(
|
| 49 |
+
run_lora,
|
| 50 |
+
[
|
| 51 |
+
gr.Textbox(label="LoRA model card", show_label=False, lines=1, max_lines=1, placeholder="Type the LoRA model card here."),
|
| 52 |
+
gr.Textbox(label="Prompt", show_label=False, placeholder="Type a prompt after selecting a LoRA."),
|
| 53 |
+
gr.Textbox(label="Negative Prompt", show_label=False, placeholder="Type negative prompt here."),
|
| 54 |
+
# gr.Button("Run")
|
| 55 |
+
],
|
| 56 |
+
"image",
|
| 57 |
+
# examples=[
|
| 58 |
+
# [2, "cat", ["Japan", "Pakistan"], "park", ["ate", "swam"], True],
|
| 59 |
+
# [4, "dog", ["Japan"], "zoo", ["ate", "swam"], False],
|
| 60 |
+
# [10, "bird", ["USA", "Pakistan"], "road", ["ran"], False],
|
| 61 |
+
# [8, "cat", ["Pakistan"], "zoo", ["ate"], True],
|
| 62 |
+
# ]
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
app.launch()
|