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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from requests.exceptions import ConnectTimeout
|
| 3 |
+
import time
|
| 4 |
+
import requests
|
| 5 |
+
import base64
|
| 6 |
+
|
| 7 |
+
global headers
|
| 8 |
+
global cancel_url
|
| 9 |
+
global path
|
| 10 |
+
global output_image
|
| 11 |
+
global property_name_array
|
| 12 |
+
property_name_array =[]
|
| 13 |
+
output_image = ''
|
| 14 |
+
path = ''
|
| 15 |
+
cancel_url =''
|
| 16 |
+
headers = {
|
| 17 |
+
'Content-Type': 'application/json',
|
| 18 |
+
'Authorization': 'Token r8_ZGZlzThfRkPZVDMygVclY1XZ9AuxmIQ2qwwPP',
|
| 19 |
+
"Access-Control-Allow-Headers": "Content-Type",
|
| 20 |
+
"Access-Control-Allow-Origin": '**',
|
| 21 |
+
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,PATCH"}
|
| 22 |
+
|
| 23 |
+
with gr.Blocks() as demo:
|
| 24 |
+
owner = "adirik"
|
| 25 |
+
name = "multilingual-e5-large"
|
| 26 |
+
max_retries = 3
|
| 27 |
+
retry_delay = 2
|
| 28 |
+
for retry in range(max_retries):
|
| 29 |
+
try:
|
| 30 |
+
url = f'https://api.replicate.com/v1/models/{owner}/{name}'
|
| 31 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 32 |
+
# Process the response
|
| 33 |
+
break # Break out of the loop if the request is successful
|
| 34 |
+
except ConnectTimeout:
|
| 35 |
+
if retry < max_retries - 1:
|
| 36 |
+
print(f"Connection timed out. Retrying in {retry_delay} seconds...")
|
| 37 |
+
time.sleep(retry_delay)
|
| 38 |
+
else:
|
| 39 |
+
print("Max retries exceeded. Unable to establish connection.")
|
| 40 |
+
|
| 41 |
+
data = response.json()
|
| 42 |
+
description =data.get("description", '')
|
| 43 |
+
title = data.get("default_example",'').get("model",'')
|
| 44 |
+
version = data.get("default_example",'').get("version",'')
|
| 45 |
+
|
| 46 |
+
gr.Markdown(
|
| 47 |
+
f"""
|
| 48 |
+
# {title}
|
| 49 |
+
{description}
|
| 50 |
+
""")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
with gr.Column():
|
| 54 |
+
inputs =[]
|
| 55 |
+
schema = data.get("latest_version", {}).get("openapi_schema", {}).get("components", {}).get("schemas", {})
|
| 56 |
+
ordered_properties = sorted(schema.get("Input", {}).get("properties", {}).items(), key=lambda x: x[1].get("x-order", 0))
|
| 57 |
+
required = schema.get("Input", '').get('required', [])
|
| 58 |
+
print(required,"required")
|
| 59 |
+
for property_name, property_info in ordered_properties :
|
| 60 |
+
property_name_array.append(property_name)
|
| 61 |
+
if required:
|
| 62 |
+
for item in required:
|
| 63 |
+
if item == property_name:
|
| 64 |
+
label = "*"+ property_info.get('title', '')
|
| 65 |
+
description = property_info.get('description','')
|
| 66 |
+
break
|
| 67 |
+
else:
|
| 68 |
+
label = property_info.get('title', '')
|
| 69 |
+
description = property_info.get('description','')
|
| 70 |
+
else:
|
| 71 |
+
label = property_info.get('title', '')
|
| 72 |
+
description = property_info.get('description','')
|
| 73 |
+
|
| 74 |
+
if "x-order" in property_info:
|
| 75 |
+
order = int(property_info.get('x-order',''))
|
| 76 |
+
if property_info.get("type", {}) == "integer":
|
| 77 |
+
value= data.get('default_example', '').get('input','').get(property_name,0)
|
| 78 |
+
if "minimum" and "maximum" in property_info:
|
| 79 |
+
if value == 0:
|
| 80 |
+
inputs.insert(order, gr.Slider(label=label, info= description, value=property_info.get('default', value), minimum=property_info.get('minimum', ''), maximum=property_info.get('maximum', ''), step=1))
|
| 81 |
+
else:
|
| 82 |
+
inputs.insert(order, gr.Slider(label=label, info= description, value=value, minimum=property_info.get('minimum', ''), maximum=property_info.get('maximum', ''), step=1))
|
| 83 |
+
else:
|
| 84 |
+
if value == 0:
|
| 85 |
+
inputs.insert(order, gr.Number(label=label, info= description, value=property_info.get('default', value)))
|
| 86 |
+
else:
|
| 87 |
+
inputs.insert(order, gr.Number(label=label, info= description, value=value))
|
| 88 |
+
|
| 89 |
+
elif property_info.get("type", {}) == "string":
|
| 90 |
+
value= data.get('default_example', '').get('input','').get(property_name,'')
|
| 91 |
+
if property_info.get('format','') == 'uri':
|
| 92 |
+
|
| 93 |
+
if value :
|
| 94 |
+
inputs.insert(order, gr.File(label=label, value=value, type="filepath"))
|
| 95 |
+
else :
|
| 96 |
+
inputs.insert(order, gr.File(label=label, type="filepath"))
|
| 97 |
+
|
| 98 |
+
else:
|
| 99 |
+
if value == '':
|
| 100 |
+
inputs.insert(order, gr.Textbox(label=label,info= description, value=property_info.get('default', value)))
|
| 101 |
+
else:
|
| 102 |
+
inputs.insert(order, gr.Textbox(label=label,info= description, value=value))
|
| 103 |
+
|
| 104 |
+
elif property_info.get("type", {}) == "number":
|
| 105 |
+
value= data.get('default_example', '').get('input','').get(property_name, 0)
|
| 106 |
+
if "minimum" and "maximum" in property_info:
|
| 107 |
+
if value == 0:
|
| 108 |
+
inputs.insert(order, gr.Slider(label=label,info= description, value=property_info.get('default', value), minimum=property_info.get('minimum', ''), maximum=property_info.get('maximum', '')))
|
| 109 |
+
else:
|
| 110 |
+
inputs.insert(order, gr.Slider(label=label,info= description, value=value, minimum=property_info.get('minimum', ''), maximum=property_info.get('maximum', '')))
|
| 111 |
+
else:
|
| 112 |
+
if value == 0:
|
| 113 |
+
inputs.insert(order, gr.Number(label=label,info= description, value=property_info.get('default', value)))
|
| 114 |
+
else:
|
| 115 |
+
inputs.insert(order, gr.Number(label=label,info= description, value=value))
|
| 116 |
+
elif property_info.get("type", {}) == "boolean":
|
| 117 |
+
value= data.get('default_example', '').get('input','').get(property_name,'')
|
| 118 |
+
if value == '':
|
| 119 |
+
inputs.insert(order, gr.Checkbox(label=label,info= description, value=property_info.get('default', value)))
|
| 120 |
+
else:
|
| 121 |
+
inputs.insert(order, gr.Checkbox(label=label,info= description, value=value))
|
| 122 |
+
else:
|
| 123 |
+
value= data.get('default_example', '').get('input','').get(property_name,'')
|
| 124 |
+
options=schema.get(property_name,'').get('enum',[])
|
| 125 |
+
if value:
|
| 126 |
+
inputs.insert(order, gr.Dropdown(label=property_name,info= description,choices=options, value=property_info.get("default", value)))
|
| 127 |
+
else:
|
| 128 |
+
inputs.insert(order, gr.Dropdown(label=property_name,info= description,choices=options, value=value))
|
| 129 |
+
|
| 130 |
+
with gr.Row():
|
| 131 |
+
cancel_btn = gr.Button("Cancel")
|
| 132 |
+
run_btn = gr.Button("Run")
|
| 133 |
+
|
| 134 |
+
with gr.Column():
|
| 135 |
+
|
| 136 |
+
outputs = []
|
| 137 |
+
|
| 138 |
+
output_result = data.get("default_example", '').get("output")
|
| 139 |
+
output_type= schema.get("Output", '').get("type", '')
|
| 140 |
+
if output_type == 'array':
|
| 141 |
+
output_image = output_result
|
| 142 |
+
else:
|
| 143 |
+
output_image = output_result
|
| 144 |
+
print (output_image)
|
| 145 |
+
outputs.append(gr.TextArea(value=output_image))
|
| 146 |
+
outputs.append(gr.Image(visible=False))
|
| 147 |
+
outputs.append(gr.Image(visible=False))
|
| 148 |
+
outputs.append(gr.Image(visible=False))
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
def run_process(input1, input2, input3):
|
| 153 |
+
global cancel_url
|
| 154 |
+
global property_name_array
|
| 155 |
+
print(len(property_name_array))
|
| 156 |
+
cancel_url=''
|
| 157 |
+
url = 'https://replicate.com/api/predictions'
|
| 158 |
+
|
| 159 |
+
print(version, 'version')
|
| 160 |
+
body = {
|
| 161 |
+
"version": version,
|
| 162 |
+
"input": {
|
| 163 |
+
property_name_array[0]: input1,
|
| 164 |
+
property_name_array[1]: input2,
|
| 165 |
+
property_name_array[2]: input3,
|
| 166 |
+
}
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
response = requests.post(url, json=body)
|
| 171 |
+
print(response.status_code)
|
| 172 |
+
if response.status_code == 201:
|
| 173 |
+
response_data = response.json()
|
| 174 |
+
get_url = response_data.get('urls','').get('get','')
|
| 175 |
+
identifier = 'https://replicate.com/api/predictions/'+get_url.split("/")[-1]
|
| 176 |
+
|
| 177 |
+
print(identifier,'')
|
| 178 |
+
time.sleep(3)
|
| 179 |
+
output =verify_image(identifier)
|
| 180 |
+
print(output,'333')
|
| 181 |
+
if output:
|
| 182 |
+
return gr.TextArea(value=output), gr.Image(),gr.Image(),gr.Image()
|
| 183 |
+
|
| 184 |
+
return gr.Image(),gr.Image(visible=False),gr.Image(visible=False),gr.Image(visible=False)
|
| 185 |
+
|
| 186 |
+
def cancel_process(input1, input2, input3):
|
| 187 |
+
global cancel_url
|
| 188 |
+
cancel_url = '123'
|
| 189 |
+
global output_image
|
| 190 |
+
return gr.TextArea(value=output_image), gr.Image(visible=False),gr.Image(visible=False),gr.Image(visible=False)
|
| 191 |
+
|
| 192 |
+
def verify_image(get_url):
|
| 193 |
+
res = requests.get(get_url)
|
| 194 |
+
if res.status_code == 200:
|
| 195 |
+
res_data = res.json()
|
| 196 |
+
if res_data.get('error',''):
|
| 197 |
+
return
|
| 198 |
+
else:
|
| 199 |
+
if cancel_url:
|
| 200 |
+
return
|
| 201 |
+
else:
|
| 202 |
+
output = res_data.get('output', [])
|
| 203 |
+
print(output,'111')
|
| 204 |
+
if output:
|
| 205 |
+
print(output,'222')
|
| 206 |
+
return output
|
| 207 |
+
|
| 208 |
+
else:
|
| 209 |
+
time.sleep(1)
|
| 210 |
+
val = verify_image(get_url)
|
| 211 |
+
return val
|
| 212 |
+
else:
|
| 213 |
+
return []
|
| 214 |
+
|
| 215 |
+
run_btn.click(run_process, inputs=inputs, outputs=outputs, api_name="run")
|
| 216 |
+
cancel_btn.click(cancel_process, inputs=inputs, outputs=outputs, api_name="cancel")
|
| 217 |
+
|
| 218 |
+
demo.launch()
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
|