Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ import torch
|
|
7 |
@st.cache_resource
|
8 |
def load_model():
|
9 |
model_id = 'microsoft/Florence-2-large'
|
10 |
-
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True
|
11 |
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
12 |
return model, processor
|
13 |
|
@@ -19,7 +19,16 @@ def run_example(task_prompt, image, text_input=None):
|
|
19 |
prompt = task_prompt
|
20 |
else:
|
21 |
prompt = task_prompt + text_input
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
generated_ids = model.generate(
|
24 |
input_ids=inputs["input_ids"],
|
25 |
pixel_values=inputs["pixel_values"],
|
@@ -51,19 +60,22 @@ if uploaded_file is not None:
|
|
51 |
st.subheader("Generated Captions")
|
52 |
|
53 |
with st.spinner("Generating caption..."):
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
7 |
@st.cache_resource
|
8 |
def load_model():
|
9 |
model_id = 'microsoft/Florence-2-large'
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True).eval()
|
11 |
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
12 |
return model, processor
|
13 |
|
|
|
19 |
prompt = task_prompt
|
20 |
else:
|
21 |
prompt = task_prompt + text_input
|
22 |
+
|
23 |
+
# Prepare inputs
|
24 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
25 |
+
inputs["input_ids"] = inputs["input_ids"].to(torch.float32)
|
26 |
+
inputs["pixel_values"] = inputs["pixel_values"].to(torch.float32)
|
27 |
+
|
28 |
+
# Ensure the model is in float32 mode
|
29 |
+
model = model.to(torch.float32)
|
30 |
+
|
31 |
+
# Generate predictions
|
32 |
generated_ids = model.generate(
|
33 |
input_ids=inputs["input_ids"],
|
34 |
pixel_values=inputs["pixel_values"],
|
|
|
60 |
st.subheader("Generated Captions")
|
61 |
|
62 |
with st.spinner("Generating caption..."):
|
63 |
+
try:
|
64 |
+
caption = run_example('<CAPTION>', image)
|
65 |
+
detailed_caption = run_example('<DETAILED_CAPTION>', image)
|
66 |
+
more_detailed_caption = run_example('<MORE_DETAILED_CAPTION>', image)
|
67 |
+
|
68 |
+
st.write("**Caption:**", caption)
|
69 |
+
st.write("**Detailed Caption:**", detailed_caption)
|
70 |
+
st.write("**More Detailed Caption:**", more_detailed_caption)
|
71 |
|
72 |
+
# Option to save the output
|
73 |
+
if st.button("Save Captions"):
|
74 |
+
output_path = "captions.txt"
|
75 |
+
with open(output_path, "w") as file:
|
76 |
+
file.write(f"Caption: {caption}\n")
|
77 |
+
file.write(f"Detailed Caption: {detailed_caption}\n")
|
78 |
+
file.write(f"More Detailed Caption: {more_detailed_caption}\n")
|
79 |
+
st.success(f"Captions saved to {output_path}!")
|
80 |
+
except Exception as e:
|
81 |
+
st.error(f"Error: {e}")
|