adil9858 commited on
Commit
0c3f7ae
·
verified ·
1 Parent(s): 76c5b28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -17
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, torch_dtype='auto').eval()
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
- inputs = processor(text=prompt, images=image, return_tensors="pt").to(torch.float32) # Ensure CPU compatibility
 
 
 
 
 
 
 
 
 
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
- caption = run_example('<CAPTION>', image)
55
- detailed_caption = run_example('<DETAILED_CAPTION>', image)
56
- more_detailed_caption = run_example('<MORE_DETAILED_CAPTION>', image)
57
-
58
- st.write("**Caption:**", caption)
59
- st.write("**Detailed Caption:**", detailed_caption)
60
- st.write("**More Detailed Caption:**", more_detailed_caption)
 
61
 
62
- # Option to save the output
63
- if st.button("Save Captions"):
64
- output_path = "captions.txt"
65
- with open(output_path, "w") as file:
66
- file.write(f"Caption: {caption}\n")
67
- file.write(f"Detailed Caption: {detailed_caption}\n")
68
- file.write(f"More Detailed Caption: {more_detailed_caption}\n")
69
- st.success(f"Captions saved to {output_path}!")
 
 
 
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}")