tsengiii commited on
Commit
9fb5a83
·
verified ·
1 Parent(s): be9fba2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -34
app.py CHANGED
@@ -4,20 +4,14 @@ 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
  # Initialize Florence model
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
- # api_key = os.getenv("HF_READ_TOKEN")
20
-
21
  def generate_caption(image):
22
  if not isinstance(image, Image.Image):
23
  image = Image.fromarray(image)
@@ -37,32 +31,22 @@ def generate_caption(image):
37
  task="<MORE_DETAILED_CAPTION>",
38
  image_size=(image.width, image.height)
39
  )
40
- prompt = parsed_answer["<MORE_DETAILED_CAPTION>"]
41
- print("\n\nGeneration completed!:"+ prompt)
42
  return prompt
43
- # yield prompt, None
44
- # image_path = generate_image(prompt,random.randint(0, 4294967296))
45
- # yield prompt, image_path
46
 
47
- # def generate_image(prompt, seed=42, width=1024, height=1024):
48
- # try:
49
- # result = Client("KingNish/Realtime-FLUX", hf_token=api_key).predict(
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 to install flash-attn if necessary (commented for now)
8
+ # subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
 
 
 
 
9
 
10
  # Initialize Florence model
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)
 
31
  task="<MORE_DETAILED_CAPTION>",
32
  image_size=(image.width, image.height)
33
  )
34
+ prompt = parsed_answer["<MORE_DETAILED_CAPTION>"]
 
35
  return prompt
 
 
 
36
 
37
+ # Custom CSS for pink background and title styling
38
+ css = """
39
+ body {background-color: #FFC0CB;}
40
+ h1 {color: #800080; text-align: center; font-size: 32px; font-family: 'Comic Sans MS', cursive, sans-serif;}
41
+ """
42
+
43
+ # Interface with pink background and a welcome message
44
+ io = gr.Interface(
45
+ fn=generate_caption,
46
+ inputs=[gr.Image(label="Input Image")],
47
+ outputs=[gr.Textbox(label="Output Prompt", lines=2, show_copy_button=True)],
48
+ title="歡迎來到我的魔法世界✨",
49
+ css=css
50
+ )
51
+
 
 
 
 
 
 
52
  io.launch(debug=True)