Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +53 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import subprocess
|
2 |
+
|
3 |
+
# 安裝缺少的依賴包
|
4 |
+
subprocess.run("pip install einops", shell=True)
|
5 |
+
|
6 |
+
import gradio as gr
|
7 |
+
import torch
|
8 |
+
from PIL import Image
|
9 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
10 |
+
|
11 |
+
# Initialize Florence model
|
12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
florence_model = AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True).to(device).eval()
|
14 |
+
florence_processor = AutoProcessor.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True)
|
15 |
+
|
16 |
+
def generate_caption(image):
|
17 |
+
if not isinstance(image, Image.Image):
|
18 |
+
image = Image.fromarray(image)
|
19 |
+
|
20 |
+
inputs = florence_processor(text="<MORE_DETAILED_CAPTION>", images=image, return_tensors="pt").to(device)
|
21 |
+
generated_ids = florence_model.generate(
|
22 |
+
input_ids=inputs["input_ids"],
|
23 |
+
pixel_values=inputs["pixel_values"],
|
24 |
+
max_new_tokens=1024,
|
25 |
+
early_stopping=False,
|
26 |
+
do_sample=False,
|
27 |
+
num_beams=3,
|
28 |
+
)
|
29 |
+
generated_text = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
30 |
+
parsed_answer = florence_processor.post_process_generation(
|
31 |
+
generated_text,
|
32 |
+
task="<MORE_DETAILED_CAPTION>",
|
33 |
+
image_size=(image.width, image.height)
|
34 |
+
)
|
35 |
+
prompt = parsed_answer["<MORE_DETAILED_CAPTION>"]
|
36 |
+
return prompt
|
37 |
+
|
38 |
+
# Custom CSS for pink background and title styling
|
39 |
+
css = """
|
40 |
+
body {background-color: #FFC0CB;}
|
41 |
+
h1 {color: #800080; text-align: center; font-size: 32px; font-family: 'Comic Sans MS', cursive, sans-serif;}
|
42 |
+
"""
|
43 |
+
|
44 |
+
# Interface with pink background and a welcome message
|
45 |
+
io = gr.Interface(
|
46 |
+
fn=generate_caption,
|
47 |
+
inputs=[gr.Image(label="Input Image")],
|
48 |
+
outputs=[gr.Textbox(label="Output Prompt", lines=2, show_copy_button=True)],
|
49 |
+
title="歡迎來到我的魔法世界✨",
|
50 |
+
css=css
|
51 |
+
)
|
52 |
+
|
53 |
+
io.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
timm
|