Update app.py
Browse files
app.py
CHANGED
|
@@ -9,84 +9,82 @@ from PIL import Image
|
|
| 9 |
import uvicorn
|
| 10 |
|
| 11 |
# --- 1. إعداد التطبيق والموديلات ---
|
| 12 |
-
app = FastAPI(title="YOLO + GIT Captioning")
|
| 13 |
|
| 14 |
-
# تحديد الجهاز (
|
| 15 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
# تأكدي أن ملف best.pt موجود في نفس المجلد
|
| 19 |
MY_MODEL_PATH = 'best.pt'
|
| 20 |
|
| 21 |
-
print("🔄 جاري تحميل الموديلات...
|
| 22 |
|
| 23 |
-
# تحميل موديل YOLO
|
| 24 |
-
|
| 25 |
detection_model = YOLO(MY_MODEL_PATH)
|
| 26 |
-
print("✅ تم تحميل موديل YOLO بنجاح")
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
detection_model = YOLO("
|
| 30 |
-
print("⚠️ تحذير: لم يتم العثور على best.pt، تم استخدام الموديل الافتراضي.")
|
| 31 |
|
| 32 |
-
# تحميل موديل GIT-base (أخف وأسرع للمساح
|
| 33 |
processor = AutoProcessor.from_pretrained("microsoft/git-base")
|
| 34 |
caption_model = AutoModelForCausalLM.from_pretrained("microsoft/git-base").to(device)
|
| 35 |
|
| 36 |
@app.get("/")
|
| 37 |
def home():
|
| 38 |
-
return {"status": "
|
| 39 |
|
| 40 |
-
# --- 2. وظيفة المعالجة (نفس منطق ك
|
| 41 |
|
| 42 |
@app.post("/analyze")
|
| 43 |
async def analyze_image(file: UploadFile = File(...)):
|
|
|
|
| 44 |
data = await file.read()
|
| 45 |
original_image = Image.open(io.BytesIO(data)).convert("RGB")
|
| 46 |
|
| 47 |
-
# استخدام
|
| 48 |
results = detection_model(original_image, conf=0.20)
|
| 49 |
-
|
| 50 |
integrated_results = []
|
| 51 |
|
| 52 |
for r in results:
|
| 53 |
for i, box in enumerate(r.boxes):
|
| 54 |
label = r.names[int(box.cls)]
|
| 55 |
conf_score = float(box.conf[0])
|
| 56 |
-
coords = box.xyxy[0].tolist()
|
| 57 |
|
| 58 |
-
# عملية القص (Cropping)
|
| 59 |
cropped_img = original_image.crop((coords[0], coords[1], coords[2], coords[3]))
|
| 60 |
|
| 61 |
-
# وصف الجزء المقصوص
|
| 62 |
inputs = processor(images=cropped_img, return_tensors="pt").to(device)
|
| 63 |
generated_ids = caption_model.generate(pixel_values=inputs.pixel_values, max_length=40)
|
| 64 |
detailed_desc = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 65 |
|
| 66 |
integrated_results.append({
|
| 67 |
"object_id": i + 1,
|
| 68 |
-
"
|
| 69 |
"confidence": f"{conf_score:.2f}",
|
| 70 |
-
"
|
| 71 |
})
|
| 72 |
|
| 73 |
-
#
|
| 74 |
if not integrated_results:
|
| 75 |
inputs = processor(images=original_image, return_tensors="pt").to(device)
|
| 76 |
generated_ids = caption_model.generate(pixel_values=inputs.pixel_values, max_length=40)
|
| 77 |
general_desc = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 78 |
return {
|
| 79 |
-
"message": "
|
| 80 |
"general_description": general_desc
|
| 81 |
}
|
| 82 |
|
| 83 |
return {
|
| 84 |
"detected_count": len(integrated_results),
|
| 85 |
-
"
|
| 86 |
}
|
| 87 |
|
| 88 |
-
# --- 3. تشغيل السيرفر
|
| 89 |
if name == "__main__":
|
| 90 |
-
# المنفذ 7860 هو ال
|
| 91 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 92 |
|
|
|
|
| 9 |
import uvicorn
|
| 10 |
|
| 11 |
# --- 1. إعداد التطبيق والموديلات ---
|
| 12 |
+
app = FastAPI(title="YOLO + GIT Captioning API")
|
| 13 |
|
| 14 |
+
# تحديد الجهاز (استخدام CPU للمساحات المجانية لضمان الاستقرار)
|
| 15 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
|
| 17 |
+
# مسار الموديل الذي رفعتِيه يدوياً في القائمة
|
|
|
|
| 18 |
MY_MODEL_PATH = 'best.pt'
|
| 19 |
|
| 20 |
+
print("🔄 جاري تحميل الموديلات... يرجى الانتظار")
|
| 21 |
|
| 22 |
+
# تحميل موديل YOLO الخاص بكِ
|
| 23 |
+
try:
|
| 24 |
detection_model = YOLO(MY_MODEL_PATH)
|
| 25 |
+
print("✅ تم تحميل موديل YOLO الخاص بك بنجاح")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"⚠️ فشل تحميل best.pt، سيتم استخدام الموديل الافتراضي: {e}")
|
| 28 |
+
detection_model = YOLO("yolov8n.pt")
|
|
|
|
| 29 |
|
| 30 |
+
# تحميل موديل GIT-base (أخف وأسرع للمساحة المجانية)
|
| 31 |
processor = AutoProcessor.from_pretrained("microsoft/git-base")
|
| 32 |
caption_model = AutoModelForCausalLM.from_pretrained("microsoft/git-base").to(device)
|
| 33 |
|
| 34 |
@app.get("/")
|
| 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 |
|
| 46 |
+
# 1. الكشف باستخدام YOLO
|
| 47 |
results = detection_model(original_image, conf=0.20)
|
|
|
|
| 48 |
integrated_results = []
|
| 49 |
|
| 50 |
for r in results:
|
| 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() # [xmin, ymin, xmax, ymax]
|
| 55 |
|
| 56 |
+
# 2. عملية القص (Cropping) للجزء المكتشف
|
| 57 |
cropped_img = original_image.crop((coords[0], coords[1], coords[2], coords[3]))
|
| 58 |
|
| 59 |
+
# 3. وصف الجزء المقصوص عبر موديل GIT
|
| 60 |
inputs = processor(images=cropped_img, return_tensors="pt").to(device)
|
| 61 |
generated_ids = caption_model.generate(pixel_values=inputs.pixel_values, max_length=40)
|
| 62 |
detailed_desc = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 63 |
|
| 64 |
integrated_results.append({
|
| 65 |
"object_id": i + 1,
|
| 66 |
+
"label": label,
|
| 67 |
"confidence": f"{conf_score:.2f}",
|
| 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 by YOLO. Here is a general description.",
|
| 78 |
"general_description": general_desc
|
| 79 |
}
|
| 80 |
|
| 81 |
return {
|
| 82 |
"detected_count": len(integrated_results),
|
| 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 |
|