Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import spaces
|
3 |
from PIL import Image
|
4 |
import torch
|
5 |
from transformers import AutoModelForCausalLM, AutoProcessor
|
6 |
import requests
|
|
|
7 |
|
8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
|
@@ -11,45 +130,91 @@ model = AutoModelForCausalLM.from_pretrained("MiaoshouAI/Florence-2-base-PromptG
|
|
11 |
processor = AutoProcessor.from_pretrained("MiaoshouAI/Florence-2-base-PromptGen-v1.5", trust_remote_code=True)
|
12 |
|
13 |
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
|
|
|
|
|
|
19 |
if request:
|
20 |
print("请求头字典:", request.headers)
|
21 |
print("IP 地址:", request.client.host)
|
22 |
print("查询参数:", dict(request.query_params))
|
23 |
print("会话哈希:", request.session_hash)
|
24 |
-
max_size = 256
|
25 |
-
width, height = image.size
|
26 |
-
if width > height:
|
27 |
-
new_width = max_size
|
28 |
-
new_height = int((new_width / width) * height)
|
29 |
-
else:
|
30 |
-
new_height = max_size
|
31 |
-
new_width = int((new_height / height) * width)
|
32 |
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
|
36 |
-
|
37 |
-
generated_ids = model.generate(
|
38 |
-
input_ids=inputs["input_ids"],
|
39 |
-
pixel_values=inputs["pixel_values"],
|
40 |
-
max_new_tokens=1024,
|
41 |
-
do_sample=False,
|
42 |
-
num_beams=3
|
43 |
-
)
|
44 |
-
|
45 |
-
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
46 |
-
|
47 |
-
parsed_answer = processor.post_process_generation(generated_text, task=prompt, image_size=(image.width, image.height))
|
48 |
-
data = {
|
49 |
-
'info': str(parsed_answer)
|
50 |
-
}
|
51 |
-
response = requests.post(url, json=data)
|
52 |
-
return parsed_answer
|
53 |
|
54 |
css = """
|
55 |
#col-container {
|
@@ -64,29 +229,13 @@ with gr.Blocks(css=css) as app:
|
|
64 |
Get tag based on images using the Florence-2-base-PromptGen-v1.5 model.
|
65 |
""")
|
66 |
|
67 |
-
|
68 |
-
prompt = gr.Text(
|
69 |
-
label="Prompt",
|
70 |
-
show_label=False,
|
71 |
-
max_lines=1,
|
72 |
-
placeholder="Enter your prompt or blank here.",
|
73 |
-
container=False,
|
74 |
-
)
|
75 |
-
image_input = gr.Image(
|
76 |
-
label="Image",
|
77 |
-
type="pil",
|
78 |
-
show_label=False,
|
79 |
-
container=False,
|
80 |
-
)
|
81 |
-
run_button = gr.Button("Run", scale=0)
|
82 |
-
|
83 |
result = gr.Textbox(label="Generated Text", show_label=False)
|
84 |
|
85 |
-
|
86 |
gr.on(
|
87 |
-
triggers=[run_button.click
|
88 |
fn=infer,
|
89 |
-
inputs=[
|
90 |
outputs=[result]
|
91 |
)
|
92 |
|
|
|
1 |
+
# import gradio as gr
|
2 |
+
# import spaces
|
3 |
+
# from PIL import Image
|
4 |
+
# import torch
|
5 |
+
# from transformers import AutoModelForCausalLM, AutoProcessor
|
6 |
+
# import requests
|
7 |
+
# import json
|
8 |
+
|
9 |
+
# device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
+
|
11 |
+
# model = AutoModelForCausalLM.from_pretrained("MiaoshouAI/Florence-2-base-PromptGen-v1.5", trust_remote_code=True).to(device)
|
12 |
+
# processor = AutoProcessor.from_pretrained("MiaoshouAI/Florence-2-base-PromptGen-v1.5", trust_remote_code=True)
|
13 |
+
|
14 |
+
|
15 |
+
# SERVER_URL = 'http://43.156.72.113:8188'
|
16 |
+
# FETCH_TASKS_URL = SERVER_URL + '/fetch/'
|
17 |
+
# UPDATE_TASK_STATUS_URL = SERVER_URL + '/update/'
|
18 |
+
|
19 |
+
# def fetch_task(category, fetch_all=False):
|
20 |
+
# params = {'fetch_all': 'true' if fetch_all else 'false'}
|
21 |
+
# response = requests.post(FETCH_TASKS_URL + category, params=params)
|
22 |
+
# if response.status_code == 200:
|
23 |
+
# return response.json()
|
24 |
+
# else:
|
25 |
+
# print(f"Failed to fetch tasks: {response.status_code} - {response.text}")
|
26 |
+
# return None
|
27 |
+
|
28 |
+
# def update_task_status(category, task_id, status, result=None):
|
29 |
+
# data = {'status': status}
|
30 |
+
# if result:
|
31 |
+
# data['result'] = result
|
32 |
+
|
33 |
+
# response = requests.post(UPDATE_TASK_STATUS_URL + category + f'/{task_id}', json=data)
|
34 |
+
# if response.status_code == 200:
|
35 |
+
# print(f"Task {task_id} updated successfully: {json.dumps(response.json(), indent=4)}")
|
36 |
+
# else:
|
37 |
+
# print(f"Failed to update task {task_id}: {response.status_code} - {response.text}")
|
38 |
+
|
39 |
+
|
40 |
+
# @spaces.GPU(duration=200)
|
41 |
+
# def infer(prompt, image, request: gr.Request):
|
42 |
+
|
43 |
+
# if request:
|
44 |
+
# print("请求头字典:", request.headers)
|
45 |
+
# print("IP 地址:", request.client.host)
|
46 |
+
# print("查询参数:", dict(request.query_params))
|
47 |
+
# print("会话哈希:", request.session_hash)
|
48 |
+
|
49 |
+
# max_size = 256
|
50 |
+
# width, height = image.size
|
51 |
+
# if width > height:
|
52 |
+
# new_width = max_size
|
53 |
+
# new_height = int((new_width / width) * height)
|
54 |
+
# else:
|
55 |
+
# new_height = max_size
|
56 |
+
# new_width = int((new_height / height) * width)
|
57 |
+
|
58 |
+
# image = image.resize((new_width, new_height), Image.LANCZOS)
|
59 |
+
|
60 |
+
# inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
|
61 |
+
|
62 |
+
# generated_ids = model.generate(
|
63 |
+
# input_ids=inputs["input_ids"],
|
64 |
+
# pixel_values=inputs["pixel_values"],
|
65 |
+
# max_new_tokens=1024,
|
66 |
+
# do_sample=False,
|
67 |
+
# num_beams=3
|
68 |
+
# )
|
69 |
+
|
70 |
+
# generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
71 |
+
|
72 |
+
# parsed_answer = processor.post_process_generation(generated_text, task=prompt, image_size=(image.width, image.height))
|
73 |
+
|
74 |
+
# return parsed_answer
|
75 |
+
|
76 |
+
# css = """
|
77 |
+
# #col-container {
|
78 |
+
# margin: 0 auto;
|
79 |
+
# max-width: 800px;
|
80 |
+
# }
|
81 |
+
# """
|
82 |
+
|
83 |
+
# with gr.Blocks(css=css) as app:
|
84 |
+
# with gr.Column(elem_id="col-container"):
|
85 |
+
# gr.Markdown(f"""# Tag The Image
|
86 |
+
# Get tag based on images using the Florence-2-base-PromptGen-v1.5 model.
|
87 |
+
# """)
|
88 |
+
|
89 |
+
# with gr.Row():
|
90 |
+
# prompt = gr.Text(
|
91 |
+
# label="Prompt",
|
92 |
+
# show_label=False,
|
93 |
+
# max_lines=1,
|
94 |
+
# placeholder="Enter your prompt or blank here.",
|
95 |
+
# container=False,
|
96 |
+
# )
|
97 |
+
# image_input = gr.Image(
|
98 |
+
# label="Image",
|
99 |
+
# type="pil",
|
100 |
+
# show_label=False,
|
101 |
+
# container=False,
|
102 |
+
# )
|
103 |
+
# run_button = gr.Button("Run", scale=0)
|
104 |
+
|
105 |
+
# result = gr.Textbox(label="Generated Text", show_label=False)
|
106 |
+
|
107 |
+
|
108 |
+
# gr.on(
|
109 |
+
# triggers=[run_button.click, prompt.submit],
|
110 |
+
# fn=infer,
|
111 |
+
# inputs=[prompt, image_input],
|
112 |
+
# outputs=[result]
|
113 |
+
# )
|
114 |
+
|
115 |
+
# app.queue()
|
116 |
+
# app.launch(show_error=True)
|
117 |
+
|
118 |
+
|
119 |
import gradio as gr
|
120 |
import spaces
|
121 |
from PIL import Image
|
122 |
import torch
|
123 |
from transformers import AutoModelForCausalLM, AutoProcessor
|
124 |
import requests
|
125 |
+
import json
|
126 |
|
127 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
128 |
|
|
|
130 |
processor = AutoProcessor.from_pretrained("MiaoshouAI/Florence-2-base-PromptGen-v1.5", trust_remote_code=True)
|
131 |
|
132 |
|
133 |
+
SERVER_URL = 'http://43.156.72.113:8188'
|
134 |
+
FETCH_TASKS_URL = SERVER_URL + '/fetch/'
|
135 |
+
UPDATE_TASK_STATUS_URL = SERVER_URL + '/update/'
|
136 |
|
137 |
+
def fetch_task(category, fetch_all=False):
|
138 |
+
params = {'fetch_all': 'true' if fetch_all else 'false'}
|
139 |
+
response = requests.post(FETCH_TASKS_URL + category, params=params)
|
140 |
+
if response.status_code == 200:
|
141 |
+
return response.json()
|
142 |
+
else:
|
143 |
+
print(f"Failed to fetch tasks: {response.status_code} - {response.text}")
|
144 |
+
return None
|
145 |
+
|
146 |
+
def update_task_status(category, task_id, status, result=None):
|
147 |
+
data = {'status': status}
|
148 |
+
if result:
|
149 |
+
data['result'] = result
|
150 |
+
|
151 |
+
response = requests.post(UPDATE_TASK_STATUS_URL + category + f'/{task_id}', json=data)
|
152 |
+
if response.status_code == 200:
|
153 |
+
print(f"Task {task_id} updated successfully: {json.dumps(response.json(), indent=4)}")
|
154 |
+
else:
|
155 |
+
print(f"Failed to update task {task_id}: {response.status_code} - {response.text}")
|
156 |
|
157 |
+
|
158 |
+
@spaces.GPU(duration=200)
|
159 |
+
def infer(request: gr.Request):
|
160 |
if request:
|
161 |
print("请求头字典:", request.headers)
|
162 |
print("IP 地址:", request.client.host)
|
163 |
print("查询参数:", dict(request.query_params))
|
164 |
print("会话哈希:", request.session_hash)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
|
166 |
+
# Fetch tasks
|
167 |
+
img2text_tasks = fetch_task('img2text', fetch_all=True)
|
168 |
+
|
169 |
+
if not img2text_tasks:
|
170 |
+
return "No tasks found or failed to fetch tasks."
|
171 |
+
|
172 |
+
for task in img2text_tasks:
|
173 |
+
if task['status'] == 'Pending':
|
174 |
+
try:
|
175 |
+
image_url = task['content']['url']
|
176 |
+
prompt = task['content']['prompt']
|
177 |
+
|
178 |
+
# Fetch the image from the URL
|
179 |
+
image_response = requests.get(image_url)
|
180 |
+
image = Image.open(BytesIO(image_response.content))
|
181 |
+
|
182 |
+
# Resize image
|
183 |
+
max_size = 256
|
184 |
+
width, height = image.size
|
185 |
+
if width > height:
|
186 |
+
new_width = max_size
|
187 |
+
new_height = int((new_width / width) * height)
|
188 |
+
else:
|
189 |
+
new_height = max_size
|
190 |
+
new_width = int((new_height / height) * width)
|
191 |
+
image = image.resize((new_width, new_height), Image.LANCZOS)
|
192 |
+
|
193 |
+
# Process the image and prompt
|
194 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
|
195 |
+
generated_ids = model.generate(
|
196 |
+
input_ids=inputs["input_ids"],
|
197 |
+
pixel_values=inputs["pixel_values"],
|
198 |
+
max_new_tokens=1024,
|
199 |
+
do_sample=False,
|
200 |
+
num_beams=3
|
201 |
+
)
|
202 |
+
|
203 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
204 |
+
parsed_answer = processor.post_process_generation(generated_text, task=prompt, image_size=(image.width, image.height))
|
205 |
+
|
206 |
+
# Update the task status to Successed with result
|
207 |
+
update_task_status('img2text', task['id'], 'Successed', {"text": parsed_answer})
|
208 |
+
|
209 |
+
return parsed_answer
|
210 |
+
except Exception as e:
|
211 |
+
print(f"Error processing task {task['id']}: {e}")
|
212 |
+
# If error occurs, update the task status to Failed
|
213 |
+
update_task_status('img2text', task['id'], 'Failed')
|
214 |
+
return f"Error processing task {task['id']}: {e}"
|
215 |
+
|
216 |
+
return "No pending tasks found."
|
217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
|
219 |
css = """
|
220 |
#col-container {
|
|
|
229 |
Get tag based on images using the Florence-2-base-PromptGen-v1.5 model.
|
230 |
""")
|
231 |
|
232 |
+
run_button = gr.Button("Run", scale=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
result = gr.Textbox(label="Generated Text", show_label=False)
|
234 |
|
|
|
235 |
gr.on(
|
236 |
+
triggers=[run_button.click],
|
237 |
fn=infer,
|
238 |
+
inputs=[],
|
239 |
outputs=[result]
|
240 |
)
|
241 |
|