Update app.py
Browse files
app.py
CHANGED
|
@@ -35,11 +35,10 @@ caption_model = AutoModelForCausalLM.from_pretrained("microsoft/git-base").to(de
|
|
| 35 |
def home():
|
| 36 |
return {"status": "Online", "instruction": "Add /docs to the URL to test the model"}
|
| 37 |
|
| 38 |
-
# --- 2. وظيفة المعالجة
|
| 39 |
|
| 40 |
@app.post("/analyze")
|
| 41 |
async def analyze_image(file: UploadFile = File(...)):
|
| 42 |
-
# قراءة الصورة المرفوعة
|
| 43 |
data = await file.read()
|
| 44 |
original_image = Image.open(io.BytesIO(data)).convert("RGB")
|
| 45 |
|
|
@@ -51,9 +50,9 @@ async def analyze_image(file: UploadFile = File(...)):
|
|
| 51 |
for i, box in enumerate(r.boxes):
|
| 52 |
label = r.names[int(box.cls)]
|
| 53 |
conf_score = float(box.conf[0])
|
| 54 |
-
coords = box.xyxy[0].tolist()
|
| 55 |
|
| 56 |
-
# 2. عملية القص (Cropping)
|
| 57 |
cropped_img = original_image.crop((coords[0], coords[1], coords[2], coords[3]))
|
| 58 |
|
| 59 |
# 3. وصف الجزء المقصوص عبر موديل GIT
|
|
@@ -68,13 +67,12 @@ async def analyze_image(file: UploadFile = File(...)):
|
|
| 68 |
"description": detailed_desc
|
| 69 |
})
|
| 70 |
|
| 71 |
-
# إذا لم يجد YOLO شيئاً، نصف الصورة كاملة
|
| 72 |
if not integrated_results:
|
| 73 |
inputs = processor(images=original_image, return_tensors="pt").to(device)
|
| 74 |
generated_ids = caption_model.generate(pixel_values=inputs.pixel_values, max_length=40)
|
| 75 |
general_desc = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 76 |
return {
|
| 77 |
-
"message": "No specific objects detected
|
| 78 |
"general_description": general_desc
|
| 79 |
}
|
| 80 |
|
|
@@ -83,8 +81,6 @@ async def analyze_image(file: UploadFile = File(...)):
|
|
| 83 |
"results": integrated_results
|
| 84 |
}
|
| 85 |
|
| 86 |
-
# --- 3. تشغيل السيرفر ---
|
| 87 |
if name == "__main__":
|
| 88 |
-
# المنفذ 7860 هو المطلوب في Hugging Face
|
| 89 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 90 |
-
|
|
|
|
| 35 |
def home():
|
| 36 |
return {"status": "Online", "instruction": "Add /docs to the URL to test the model"}
|
| 37 |
|
| 38 |
+
# --- 2. وظيفة المعالجة ---
|
| 39 |
|
| 40 |
@app.post("/analyze")
|
| 41 |
async def analyze_image(file: UploadFile = File(...)):
|
|
|
|
| 42 |
data = await file.read()
|
| 43 |
original_image = Image.open(io.BytesIO(data)).convert("RGB")
|
| 44 |
|
|
|
|
| 50 |
for i, box in enumerate(r.boxes):
|
| 51 |
label = r.names[int(box.cls)]
|
| 52 |
conf_score = float(box.conf[0])
|
| 53 |
+
coords = box.xyxy[0].tolist()
|
| 54 |
|
| 55 |
+
# 2. عملية القص (Cropping)
|
| 56 |
cropped_img = original_image.crop((coords[0], coords[1], coords[2], coords[3]))
|
| 57 |
|
| 58 |
# 3. وصف الجزء المقصوص عبر موديل GIT
|
|
|
|
| 67 |
"description": detailed_desc
|
| 68 |
})
|
| 69 |
|
|
|
|
| 70 |
if not integrated_results:
|
| 71 |
inputs = processor(images=original_image, return_tensors="pt").to(device)
|
| 72 |
generated_ids = caption_model.generate(pixel_values=inputs.pixel_values, max_length=40)
|
| 73 |
general_desc = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 74 |
return {
|
| 75 |
+
"message": "No specific objects detected. General description provided.",
|
| 76 |
"general_description": general_desc
|
| 77 |
}
|
| 78 |
|
|
|
|
| 81 |
"results": integrated_results
|
| 82 |
}
|
| 83 |
|
| 84 |
+
# --- 3. تشغيل السيرفر (تم تصحيح الشرطات السفلية هنا) ---
|
| 85 |
if name == "__main__":
|
|
|
|
| 86 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|