Spaces:
Sleeping
Sleeping
uodate app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,27 @@
|
|
1 |
import gradio as gr
|
2 |
-
import subprocess
|
3 |
import torch
|
4 |
-
from
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
|
|
|
9 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
10 |
|
11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
-
florence_model = AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True).to(device).eval()
|
13 |
-
florence_processor = AutoProcessor.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True)
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
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="<MORE_DETAILED_CAPTION>",
|
32 |
-
image_size=(image.width, image.height)
|
33 |
-
)
|
34 |
-
prompt = parsed_answer["<MORE_DETAILED_CAPTION>"]
|
35 |
-
print("\n\nGeneration completed!:"+ prompt)
|
36 |
-
return prompt
|
37 |
-
|
38 |
-
io = gr.Interface(generate_caption,
|
39 |
-
inputs=[gr.Image(label="Input Image")],
|
40 |
-
outputs = [gr.Textbox(label="Output Prompt", lines=3, show_copy_button = True),
|
41 |
-
],
|
42 |
theme="Yntec/HaleyCH_Theme_Orange",
|
43 |
-
description="⚠ Sorry for the inconvenience. The space
|
44 |
)
|
45 |
io.launch(debug=True)
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import torch
|
3 |
+
from diffusers import StableDiffusionPipeline
|
4 |
+
import subprocess
|
|
|
|
|
5 |
|
6 |
+
# ติดตั้ง flash-attn เหมือนเดิม (แม้จะไม่ได้ใช้โดยตรง)
|
7 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
8 |
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
10 |
|
11 |
+
# โหลดโมเดล text-to-image
|
12 |
+
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16 if device=="cuda" else torch.float32)
|
13 |
+
pipe = pipe.to(device)
|
14 |
+
pipe.safety_checker = None # optional: remove safety checker for faster performance
|
15 |
+
|
16 |
+
def generate_image(prompt):
|
17 |
+
result = pipe(prompt)
|
18 |
+
image = result.images[0]
|
19 |
+
return image
|
20 |
+
|
21 |
+
io = gr.Interface(fn=generate_image,
|
22 |
+
inputs=[gr.Textbox(label="Enter your prompt")],
|
23 |
+
outputs=[gr.Image(label="Generated Image")],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
theme="Yntec/HaleyCH_Theme_Orange",
|
25 |
+
description="⚠ Sorry for the inconvenience. The space is currently running on the CPU, which might affect performance. We appreciate your understanding."
|
26 |
)
|
27 |
io.launch(debug=True)
|