Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,9 @@ import torch
|
|
| 4 |
from PIL import Image
|
| 5 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 9 |
|
|
@@ -12,6 +15,8 @@ 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,10 +36,29 @@ def generate_caption(image):
|
|
| 31 |
task="<MORE_DETAILED_CAPTION>",
|
| 32 |
image_size=(image.width, image.height)
|
| 33 |
)
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
io = gr.Interface(generate_caption,
|
| 37 |
inputs=[gr.Image(label="Input Image")],
|
| 38 |
-
outputs = [gr.Textbox(label="Output Prompt", lines=2, show_copy_button = True)
|
|
|
|
| 39 |
)
|
| 40 |
io.launch(debug=True)
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 6 |
|
| 7 |
+
import os
|
| 8 |
+
from gradio_client import Client
|
| 9 |
+
|
| 10 |
|
| 11 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 12 |
|
|
|
|
| 15 |
florence_model = AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True).to(device).eval()
|
| 16 |
florence_processor = AutoProcessor.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True)
|
| 17 |
|
| 18 |
+
api_key = os.getenv("HF_READ_TOKEN")
|
| 19 |
+
|
| 20 |
def generate_caption(image):
|
| 21 |
if not isinstance(image, Image.Image):
|
| 22 |
image = Image.fromarray(image)
|
|
|
|
| 36 |
task="<MORE_DETAILED_CAPTION>",
|
| 37 |
image_size=(image.width, image.height)
|
| 38 |
)
|
| 39 |
+
prompt = parsed_answer["<MORE_DETAILED_CAPTION>"]
|
| 40 |
+
yield prompt, None
|
| 41 |
+
image_path = generate_image(prompt,random.randint(0, 4294967296))
|
| 42 |
+
yield prompt, image_path
|
| 43 |
+
|
| 44 |
+
def generate_image(prompt, seed=42, width=1024, height=1024):
|
| 45 |
+
try:
|
| 46 |
+
result = Client("KingNish/Realtime-FLUX", hf_token=api_key).predict(
|
| 47 |
+
prompt=prompt,
|
| 48 |
+
seed=seed,
|
| 49 |
+
width=width,
|
| 50 |
+
height=height,
|
| 51 |
+
api_name="/generate_image"
|
| 52 |
+
)
|
| 53 |
+
# Extract the image path from the result tuple
|
| 54 |
+
image_path = result[0]
|
| 55 |
+
return image_path
|
| 56 |
+
except Exception as e:
|
| 57 |
+
raise Exception(f"Error generating image: {str(e)}")
|
| 58 |
|
| 59 |
io = gr.Interface(generate_caption,
|
| 60 |
inputs=[gr.Image(label="Input Image")],
|
| 61 |
+
outputs = [gr.Textbox(label="Output Prompt", lines=2, show_copy_button = True),
|
| 62 |
+
gr.Image(label="Output Image")]
|
| 63 |
)
|
| 64 |
io.launch(debug=True)
|