Spaces:
Running
Running
lalashechka
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import time
|
4 |
+
import json
|
5 |
+
from deep_translator import GoogleTranslator
|
6 |
+
import os
|
7 |
+
import urllib.parse
|
8 |
+
import re
|
9 |
+
|
10 |
+
|
11 |
+
def convert_url_filename(url):
|
12 |
+
def encode_filename(match):
|
13 |
+
full_filename = match.group(1)
|
14 |
+
return urllib.parse.quote(full_filename)
|
15 |
+
new_url = re.sub(r'/([^/]+)$', lambda m: '/' + encode_filename(m), url)
|
16 |
+
return new_url
|
17 |
+
|
18 |
+
def process_image(image_path):
|
19 |
+
url_image = "https://lalashechka-image2prompt.hf.space/gradio_api/file=" + image_path
|
20 |
+
encoded_url_image = convert_url_filename(url_image)
|
21 |
+
print(encoded_url_image)
|
22 |
+
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"}
|
23 |
+
data = {"model": "tencentarc/gfpgan","version": "0fbacf7afc6c144e5be9767cff80f25aff23e52b0708f17e20f9879b2f21516c","input": {"img": encoded_url_image}}
|
24 |
+
result = requests.post("https://homepage.replicate.com/api/prediction", json=data, headers=headers)
|
25 |
+
prediction_id = result.json()['id']
|
26 |
+
poll_url = f"https://homepage.replicate.com/api/poll?id={prediction_id}"
|
27 |
+
c = 0
|
28 |
+
while c < 20:
|
29 |
+
time.sleep(1)
|
30 |
+
r = requests.get(poll_url, headers=headers)
|
31 |
+
status = r.json()['status']
|
32 |
+
if r.json()['status'] == 'succeeded':
|
33 |
+
image_url = r.json()['output']
|
34 |
+
return image_url
|
35 |
+
else:
|
36 |
+
c += 1
|
37 |
+
continue
|
38 |
+
|
39 |
+
|
40 |
+
css = """
|
41 |
+
.gradio-container {
|
42 |
+
min-width: 100% !important;
|
43 |
+
}
|
44 |
+
#image_output {
|
45 |
+
height: 500px;
|
46 |
+
}
|
47 |
+
#generate {
|
48 |
+
width: 100%;
|
49 |
+
background: #e253dd !important;
|
50 |
+
border: none;
|
51 |
+
border-radius: 50px;
|
52 |
+
outline: none !important;
|
53 |
+
color: white;
|
54 |
+
}
|
55 |
+
#generate:hover {
|
56 |
+
background: #de6bda !important;
|
57 |
+
outline: none !important;
|
58 |
+
color: #fff;
|
59 |
+
}
|
60 |
+
"""
|
61 |
+
|
62 |
+
|
63 |
+
with gr.Blocks(css=css) as demo:
|
64 |
+
with gr.Row():
|
65 |
+
with gr.Column():
|
66 |
+
image_input = gr.Image(show_download_button=False, interactive=True, label='Изображение:', elem_id='image_output', type='filepath')
|
67 |
+
text_button = gr.Button("Запустить нейросеть", variant='primary', elem_id="generate")
|
68 |
+
with gr.Column():
|
69 |
+
image_output= gr.Image(show_download_button=False, interactive=True, label='Результат:', type='filepath')
|
70 |
+
text_button.click(process_image, inputs=image_input, outputs=image_output)
|
71 |
+
|
72 |
+
demo.queue(default_concurrency_limit=12)
|
73 |
+
demo.launch()
|