waniberry66 commited on
Commit
594c279
·
verified ·
1 Parent(s): 9d99cd9

uodate app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -35
app.py CHANGED
@@ -1,45 +1,27 @@
1
  import gradio as gr
2
- import subprocess
3
  import torch
4
- from PIL import Image
5
- from transformers import AutoProcessor, AutoModelForCausalLM
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
- def generate_caption(image):
16
- if not isinstance(image, Image.Image):
17
- image = Image.fromarray(image)
18
-
19
- inputs = florence_processor(text="<MORE_DETAILED_CAPTION>", 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"],
23
- max_new_tokens=1024,
24
- early_stopping=False,
25
- do_sample=False,
26
- num_beams=3,
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 are currently running on the CPU, which might affect performance. We appreciate your understanding."
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)