Update app.py
Browse files
app.py
CHANGED
|
@@ -35,7 +35,7 @@ print("[INFO] Loading Florence-2-base model on CPU...")
|
|
| 35 |
try:
|
| 36 |
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base", trust_remote_code=True)
|
| 37 |
model = AutoModelForCausalLM.from_pretrained(
|
| 38 |
-
"microsoft/Florence-2-
|
| 39 |
trust_remote_code=True,
|
| 40 |
attn_implementation="eager"
|
| 41 |
).to(DEVICE).eval()
|
|
@@ -95,13 +95,14 @@ def analyze_image(image: Image.Image, task: str = "<MORE_DETAILED_CAPTION>") ->
|
|
| 95 |
generated_ids = model.generate(
|
| 96 |
input_ids=inputs["input_ids"],
|
| 97 |
pixel_values=inputs["pixel_values"],
|
| 98 |
-
max_new_tokens=1024,
|
| 99 |
num_beams=3,
|
| 100 |
-
do_sample=False
|
|
|
|
| 101 |
)
|
| 102 |
|
| 103 |
# Decode and post-process
|
| 104 |
-
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=
|
| 105 |
result = processor.post_process_generation(
|
| 106 |
generated_text,
|
| 107 |
task=task,
|
|
@@ -112,7 +113,7 @@ def analyze_image(image: Image.Image, task: str = "<MORE_DETAILED_CAPTION>") ->
|
|
| 112 |
|
| 113 |
except Exception as e:
|
| 114 |
raise ValueError(f"Failed to analyze image: {e}")
|
| 115 |
-
|
| 116 |
# ===== API Endpoints =====
|
| 117 |
@app.get("/")
|
| 118 |
async def root():
|
|
|
|
| 35 |
try:
|
| 36 |
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base", trust_remote_code=True)
|
| 37 |
model = AutoModelForCausalLM.from_pretrained(
|
| 38 |
+
"microsoft/Florence-2-Large",
|
| 39 |
trust_remote_code=True,
|
| 40 |
attn_implementation="eager"
|
| 41 |
).to(DEVICE).eval()
|
|
|
|
| 95 |
generated_ids = model.generate(
|
| 96 |
input_ids=inputs["input_ids"],
|
| 97 |
pixel_values=inputs["pixel_values"],
|
| 98 |
+
max_new_tokens=1024, # Maximum token length
|
| 99 |
num_beams=3,
|
| 100 |
+
do_sample=False,
|
| 101 |
+
repetition_penalty=1.2 # Helps avoid repetitive outputs
|
| 102 |
)
|
| 103 |
|
| 104 |
# Decode and post-process
|
| 105 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 106 |
result = processor.post_process_generation(
|
| 107 |
generated_text,
|
| 108 |
task=task,
|
|
|
|
| 113 |
|
| 114 |
except Exception as e:
|
| 115 |
raise ValueError(f"Failed to analyze image: {e}")
|
| 116 |
+
|
| 117 |
# ===== API Endpoints =====
|
| 118 |
@app.get("/")
|
| 119 |
async def root():
|