Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,25 +4,19 @@ import torch
|
|
4 |
from PIL import Image
|
5 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
6 |
|
7 |
-
# import os
|
8 |
-
# import random
|
9 |
-
# from gradio_client import Client
|
10 |
-
|
11 |
-
|
12 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
13 |
|
14 |
-
#
|
15 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
16 |
florence_model = AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True).to(device).eval()
|
17 |
florence_processor = AutoProcessor.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True)
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
def generate_caption(image):
|
22 |
if not isinstance(image, Image.Image):
|
23 |
image = Image.fromarray(image)
|
24 |
|
25 |
-
|
|
|
26 |
generated_ids = florence_model.generate(
|
27 |
input_ids=inputs["input_ids"],
|
28 |
pixel_values=inputs["pixel_values"],
|
@@ -34,35 +28,15 @@ def generate_caption(image):
|
|
34 |
generated_text = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
35 |
parsed_answer = florence_processor.post_process_generation(
|
36 |
generated_text,
|
37 |
-
task="
|
38 |
image_size=(image.width, image.height)
|
39 |
)
|
40 |
-
prompt = parsed_answer["
|
41 |
-
print("\n\
|
42 |
return prompt
|
43 |
-
# yield prompt, None
|
44 |
-
# image_path = generate_image(prompt,random.randint(0, 4294967296))
|
45 |
-
# yield prompt, image_path
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
# prompt=prompt,
|
51 |
-
# seed=seed,
|
52 |
-
# width=width,
|
53 |
-
# height=height,
|
54 |
-
# api_name="/generate_image"
|
55 |
-
# )
|
56 |
-
# # Extract the image path from the result tuple
|
57 |
-
# image_path = result[0]
|
58 |
-
# return image_path
|
59 |
-
# except Exception as e:
|
60 |
-
# raise Exception(f"Error generating image: {str(e)}")
|
61 |
-
|
62 |
-
io = gr.Interface(generate_caption,
|
63 |
-
inputs=[gr.Image(label="Input Image")],
|
64 |
-
outputs = [gr.Textbox(label="Output Prompt", lines=2, show_copy_button = True),
|
65 |
-
# gr.Image(label="Output Image")
|
66 |
-
]
|
67 |
)
|
68 |
io.launch(debug=True)
|
|
|
4 |
from PIL import Image
|
5 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
6 |
|
|
|
|
|
|
|
|
|
|
|
7 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
8 |
|
9 |
+
# Инициализация модели Florence
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
florence_model = AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True).to(device).eval()
|
12 |
florence_processor = AutoProcessor.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True)
|
13 |
|
14 |
+
def generate_ad_post(image):
|
|
|
|
|
15 |
if not isinstance(image, Image.Image):
|
16 |
image = Image.fromarray(image)
|
17 |
|
18 |
+
# Измененный текст запроса для генерации рекламного поста
|
19 |
+
inputs = florence_processor(text="Создайте рекламный пост на русском языке", images=image, return_tensors="pt").to(device)
|
20 |
generated_ids = florence_model.generate(
|
21 |
input_ids=inputs["input_ids"],
|
22 |
pixel_values=inputs["pixel_values"],
|
|
|
28 |
generated_text = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
29 |
parsed_answer = florence_processor.post_process_generation(
|
30 |
generated_text,
|
31 |
+
task="Создание рекламного поста",
|
32 |
image_size=(image.width, image.height)
|
33 |
)
|
34 |
+
prompt = parsed_answer["Создание рекламного поста"]
|
35 |
+
print("\n\nГенерация завершена!:"+ prompt)
|
36 |
return prompt
|
|
|
|
|
|
|
37 |
|
38 |
+
io = gr.Interface(generate_ad_post,
|
39 |
+
inputs=[gr.Image(label="Входное изображение")],
|
40 |
+
outputs=[gr.Textbox(label="Рекламный пост", lines=2, show_copy_button=True)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
)
|
42 |
io.launch(debug=True)
|