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| 4 |
+
import requests
|
| 5 |
+
import threading
|
| 6 |
+
import time
|
| 7 |
+
from dash import Dash, dcc, html, Input, Output, State, ctx
|
| 8 |
+
import dash_bootstrap_components as dbc
|
| 9 |
+
|
| 10 |
+
# Initialize the Dash app
|
| 11 |
+
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
| 12 |
+
|
| 13 |
+
# Stability AI API key (to be set as a Hugging Face secret)
|
| 14 |
+
STABILITY_API_KEY = os.getenv('STABILITY_API_KEY')
|
| 15 |
+
|
| 16 |
+
# Global variable to store the generated file
|
| 17 |
+
generated_file = None
|
| 18 |
+
|
| 19 |
+
# Function to upscale image
|
| 20 |
+
def upscale_image(contents):
|
| 21 |
+
global generated_file
|
| 22 |
+
generated_file = None
|
| 23 |
+
|
| 24 |
+
# Decode the base64 image
|
| 25 |
+
content_type, content_string = contents.split(',')
|
| 26 |
+
decoded = base64.b64decode(content_string)
|
| 27 |
+
|
| 28 |
+
# Prepare the API request
|
| 29 |
+
url = "https://api.stability.ai/v2beta/stable-image/upscale/fast"
|
| 30 |
+
headers = {
|
| 31 |
+
"Authorization": f"Bearer {STABILITY_API_KEY}",
|
| 32 |
+
"Accept": "image/*"
|
| 33 |
+
}
|
| 34 |
+
files = {
|
| 35 |
+
"image": ("image.png", io.BytesIO(decoded), "image/png")
|
| 36 |
+
}
|
| 37 |
+
data = {
|
| 38 |
+
"output_format": "png"
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
# Make the API request
|
| 42 |
+
response = requests.post(url, headers=headers, files=files, data=data)
|
| 43 |
+
|
| 44 |
+
if response.status_code == 200:
|
| 45 |
+
generated_file = response.content
|
| 46 |
+
return True
|
| 47 |
+
else:
|
| 48 |
+
print(f"Error: {response.status_code}, {response.text}")
|
| 49 |
+
return False
|
| 50 |
+
|
| 51 |
+
# App layout
|
| 52 |
+
app.layout = dbc.Container([
|
| 53 |
+
html.H1("Image Upscaler", className="text-center my-4"),
|
| 54 |
+
dbc.Card([
|
| 55 |
+
dbc.CardBody([
|
| 56 |
+
dcc.Upload(
|
| 57 |
+
id='upload-image',
|
| 58 |
+
children=html.Div([
|
| 59 |
+
'Drag and Drop or ',
|
| 60 |
+
html.A('Select an Image')
|
| 61 |
+
]),
|
| 62 |
+
style={
|
| 63 |
+
'width': '100%',
|
| 64 |
+
'height': '60px',
|
| 65 |
+
'lineHeight': '60px',
|
| 66 |
+
'borderWidth': '1px',
|
| 67 |
+
'borderStyle': 'dashed',
|
| 68 |
+
'borderRadius': '5px',
|
| 69 |
+
'textAlign': 'center',
|
| 70 |
+
'margin': '10px'
|
| 71 |
+
},
|
| 72 |
+
multiple=False
|
| 73 |
+
),
|
| 74 |
+
html.Div(id='output-image-upload'),
|
| 75 |
+
dbc.Button("Upscale Image", id="upscale-button", color="primary", className="mt-3"),
|
| 76 |
+
html.Div(id='upscaling-status'),
|
| 77 |
+
html.Div(id='output-upscaled-image'),
|
| 78 |
+
dbc.Button("Download Upscaled Image", id="download-button", color="success", className="mt-3", disabled=True),
|
| 79 |
+
dcc.Download(id="download-image")
|
| 80 |
+
])
|
| 81 |
+
]),
|
| 82 |
+
], fluid=True)
|
| 83 |
+
|
| 84 |
+
@app.callback(
|
| 85 |
+
Output('output-image-upload', 'children'),
|
| 86 |
+
Input('upload-image', 'contents'),
|
| 87 |
+
State('upload-image', 'filename')
|
| 88 |
+
)
|
| 89 |
+
def update_output(contents, filename):
|
| 90 |
+
if contents is not None:
|
| 91 |
+
return dbc.Card([
|
| 92 |
+
dbc.CardBody([
|
| 93 |
+
html.H4("Uploaded Image"),
|
| 94 |
+
html.Img(src=contents, style={'width': '100%'}),
|
| 95 |
+
html.P(filename)
|
| 96 |
+
])
|
| 97 |
+
])
|
| 98 |
+
|
| 99 |
+
@app.callback(
|
| 100 |
+
[Output('upscaling-status', 'children'),
|
| 101 |
+
Output('output-upscaled-image', 'children'),
|
| 102 |
+
Output('download-button', 'disabled')],
|
| 103 |
+
Input('upscale-button', 'n_clicks'),
|
| 104 |
+
State('upload-image', 'contents'),
|
| 105 |
+
prevent_initial_call=True
|
| 106 |
+
)
|
| 107 |
+
def upscale_image_callback(n_clicks, contents):
|
| 108 |
+
if contents is None:
|
| 109 |
+
return "Please upload an image first.", None, True
|
| 110 |
+
|
| 111 |
+
status_div = html.Div([
|
| 112 |
+
html.P("Upscaling image... ", style={'display': 'inline'}),
|
| 113 |
+
html.Div(className="dot-flashing")
|
| 114 |
+
])
|
| 115 |
+
|
| 116 |
+
def upscale_thread():
|
| 117 |
+
nonlocal status_div
|
| 118 |
+
success = upscale_image(contents)
|
| 119 |
+
if success:
|
| 120 |
+
status_div = "Upscaling complete!"
|
| 121 |
+
else:
|
| 122 |
+
status_div = "Upscaling failed. Please try again."
|
| 123 |
+
|
| 124 |
+
threading.Thread(target=upscale_thread).start()
|
| 125 |
+
|
| 126 |
+
while generated_file is None:
|
| 127 |
+
time.sleep(0.1) # Wait for the upscaling to complete
|
| 128 |
+
|
| 129 |
+
if generated_file:
|
| 130 |
+
upscaled_image = html.Div([
|
| 131 |
+
dbc.Card([
|
| 132 |
+
dbc.CardBody([
|
| 133 |
+
html.H4("Upscaled Image"),
|
| 134 |
+
html.Img(src=f"data:image/png;base64,{base64.b64encode(generated_file).decode()}", style={'width': '100%'})
|
| 135 |
+
])
|
| 136 |
+
])
|
| 137 |
+
])
|
| 138 |
+
return status_div, upscaled_image, False
|
| 139 |
+
else:
|
| 140 |
+
return status_div, None, True
|
| 141 |
+
|
| 142 |
+
@app.callback(
|
| 143 |
+
Output("download-image", "data"),
|
| 144 |
+
Input("download-button", "n_clicks"),
|
| 145 |
+
prevent_initial_call=True
|
| 146 |
+
)
|
| 147 |
+
def download_image(n_clicks):
|
| 148 |
+
if generated_file:
|
| 149 |
+
return dcc.send_bytes(generated_file, "upscaled_image.png")
|
| 150 |
+
|
| 151 |
+
if __name__ == '__main__':
|
| 152 |
+
print("Starting the Dash application...")
|
| 153 |
+
app.run(debug=True, host='0.0.0.0', port=7860)
|
| 154 |
+
print("Dash application has finished running.")
|