Spaces:
Running
Running
File size: 2,812 Bytes
bc29e11 5ec6635 bc29e11 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import gradio as gr
import requests
import time
import json
import os
import urllib.parse
import re
def convert_url_filename(url):
def encode_filename(match):
full_filename = match.group(1)
return urllib.parse.quote(full_filename)
new_url = re.sub(r'/([^/]+)$', lambda m: '/' + encode_filename(m), url)
return new_url
def process_image(image_path):
url_image = "https://lalashechka-image2prompt.hf.space/gradio_api/file=" + image_path
encoded_url_image = convert_url_filename(url_image)
print(encoded_url_image)
headers = {"accept": "*/*","accept-language": "en-US,en;q=0.9","cache-control": "no-cache","content-type": "application/json","origin": "https://replicate.com","pragma": "no-cache","priority": "u=1, i","referer": "https://replicate.com/","sec-ch-ua": '"Not;A=Brand";v="24", "Chromium";v="128"',"sec-ch-ua-mobile": "?0","sec-ch-ua-platform": '"Linux"',"sec-fetch-dest": "empty","sec-fetch-mode": "cors","sec-fetch-site": "same-site","user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"}
data = {"model": "tencentarc/gfpgan","version": "0fbacf7afc6c144e5be9767cff80f25aff23e52b0708f17e20f9879b2f21516c","input": {"img": encoded_url_image}}
result = requests.post("https://homepage.replicate.com/api/prediction", json=data, headers=headers)
prediction_id = result.json()['id']
poll_url = f"https://homepage.replicate.com/api/poll?id={prediction_id}"
c = 0
while c < 20:
time.sleep(1)
r = requests.get(poll_url, headers=headers)
status = r.json()['status']
if r.json()['status'] == 'succeeded':
image_url = r.json()['output']
return image_url
else:
c += 1
continue
css = """
.gradio-container {
min-width: 100% !important;
}
#image_output {
height: 500px;
}
#generate {
width: 100%;
background: #e253dd !important;
border: none;
border-radius: 50px;
outline: none !important;
color: white;
}
#generate:hover {
background: #de6bda !important;
outline: none !important;
color: #fff;
}
"""
with gr.Blocks(css=css) as demo:
with gr.Row():
with gr.Column():
image_input = gr.Image(show_download_button=False, interactive=True, label='Изображение:', elem_id='image_output', type='filepath')
text_button = gr.Button("Запустить нейросеть", variant='primary', elem_id="generate")
with gr.Column():
image_output= gr.Image(show_download_button=False, interactive=False, label='Результат:', type='filepath')
text_button.click(process_image, inputs=image_input, outputs=image_output)
demo.queue(default_concurrency_limit=12)
demo.launch() |