Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
import io
|
| 4 |
-
import shutil
|
| 5 |
from fastapi import FastAPI, File, UploadFile
|
| 6 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 7 |
from ultralytics import YOLO
|
|
@@ -9,31 +8,33 @@ from PIL import Image
|
|
| 9 |
import uvicorn
|
| 10 |
|
| 11 |
# --- 1. إعداد التطبيق والموديلات ---
|
| 12 |
-
app = FastAPI(title="YOLO + GIT Captioning API")
|
| 13 |
|
| 14 |
-
# تحديد الجهاز
|
| 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"⚠️ فشل تحميل
|
| 28 |
detection_model = YOLO("yolov8n.pt")
|
| 29 |
|
| 30 |
-
# ت
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
@app.get("/")
|
| 35 |
def home():
|
| 36 |
-
return {"status": "Online", "instruction": "Add /docs to the URL to test
|
| 37 |
|
| 38 |
# --- 2. وظيفة المعالجة ---
|
| 39 |
|
|
@@ -43,7 +44,7 @@ async def analyze_image(file: UploadFile = File(...)):
|
|
| 43 |
original_image = Image.open(io.BytesIO(data)).convert("RGB")
|
| 44 |
|
| 45 |
# 1. الكشف باستخدام YOLO
|
| 46 |
-
results = detection_model(original_image, conf=0.
|
| 47 |
integrated_results = []
|
| 48 |
|
| 49 |
for r in results:
|
|
@@ -53,11 +54,18 @@ async def analyze_image(file: UploadFile = File(...)):
|
|
| 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
|
| 59 |
inputs = processor(images=cropped_img, return_tensors="pt").to(device)
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
detailed_desc = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 62 |
|
| 63 |
integrated_results.append({
|
|
@@ -67,9 +75,10 @@ async def analyze_image(file: UploadFile = File(...)):
|
|
| 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=
|
| 73 |
general_desc = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 74 |
return {
|
| 75 |
"message": "No specific objects detected. General description provided.",
|
|
@@ -81,6 +90,6 @@ async def analyze_image(file: UploadFile = File(...)):
|
|
| 81 |
"results": integrated_results
|
| 82 |
}
|
| 83 |
|
| 84 |
-
# --- 3. تشغيل السيرفر (ت
|
| 85 |
-
if
|
| 86 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
import io
|
|
|
|
| 4 |
from fastapi import FastAPI, File, UploadFile
|
| 5 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 6 |
from ultralytics import YOLO
|
|
|
|
| 8 |
import uvicorn
|
| 9 |
|
| 10 |
# --- 1. إعداد التطبيق والموديلات ---
|
| 11 |
+
app = FastAPI(title="YOLO + GIT Large Captioning API")
|
| 12 |
|
| 13 |
+
# تحديد الجهاز
|
| 14 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
|
| 16 |
+
# مسار الموديل الخاص بك
|
| 17 |
MY_MODEL_PATH = 'best.pt'
|
| 18 |
|
| 19 |
+
print(f"🔄 جاري تحميل الموديلات على جهاز: {device}... يرجى الانتظار")
|
| 20 |
|
| 21 |
+
# تحميل موديل YOLO
|
| 22 |
try:
|
| 23 |
detection_model = YOLO(MY_MODEL_PATH)
|
| 24 |
print("✅ تم تحميل موديل YOLO الخاص بك بنجاح")
|
| 25 |
except Exception as e:
|
| 26 |
+
print(f"⚠️ فشل تحميل {MY_MODEL_PATH}، سيتم استخدام الموديل الافتراضي: {e}")
|
| 27 |
detection_model = YOLO("yolov8n.pt")
|
| 28 |
|
| 29 |
+
# --- التغيير هنا: استخدام microsoft/git-large ---
|
| 30 |
+
model_name = "microsoft/git-large"
|
| 31 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
| 32 |
+
caption_model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
|
| 33 |
+
print(f"✅ تم تحميل موديل {model_name} بنجاح")
|
| 34 |
|
| 35 |
@app.get("/")
|
| 36 |
def home():
|
| 37 |
+
return {"status": "Online", "model": "GIT-Large", "instruction": "Add /docs to the URL to test"}
|
| 38 |
|
| 39 |
# --- 2. وظيفة المعالجة ---
|
| 40 |
|
|
|
|
| 44 |
original_image = Image.open(io.BytesIO(data)).convert("RGB")
|
| 45 |
|
| 46 |
# 1. الكشف باستخدام YOLO
|
| 47 |
+
results = detection_model(original_image, conf=0.25)
|
| 48 |
integrated_results = []
|
| 49 |
|
| 50 |
for r in results:
|
|
|
|
| 54 |
coords = box.xyxy[0].tolist()
|
| 55 |
|
| 56 |
# 2. عملية القص (Cropping)
|
| 57 |
+
# إضافة هامش بسيط (Padding) للقص يحسن أحياناً من وصف الموديل
|
| 58 |
cropped_img = original_image.crop((coords[0], coords[1], coords[2], coords[3]))
|
| 59 |
|
| 60 |
+
# 3. وصف الجزء المقصوص عبر موديل GIT Large
|
| 61 |
inputs = processor(images=cropped_img, return_tensors="pt").to(device)
|
| 62 |
+
|
| 63 |
+
# ضبط البارامترات للحصول على أفضل وصف من نسخة Large
|
| 64 |
+
generated_ids = caption_model.generate(
|
| 65 |
+
pixel_values=inputs.pixel_values,
|
| 66 |
+
max_length=50,
|
| 67 |
+
num_beams=4 # استخدام beam search يحسن الجودة في نسخة Large
|
| 68 |
+
)
|
| 69 |
detailed_desc = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 70 |
|
| 71 |
integrated_results.append({
|
|
|
|
| 75 |
"description": detailed_desc
|
| 76 |
})
|
| 77 |
|
| 78 |
+
# إذا لم يتم اكتشاف أجسام، وصف الصورة كاملة
|
| 79 |
if not integrated_results:
|
| 80 |
inputs = processor(images=original_image, return_tensors="pt").to(device)
|
| 81 |
+
generated_ids = caption_model.generate(pixel_values=inputs.pixel_values, max_length=50)
|
| 82 |
general_desc = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 83 |
return {
|
| 84 |
"message": "No specific objects detected. General description provided.",
|
|
|
|
| 90 |
"results": integrated_results
|
| 91 |
}
|
| 92 |
|
| 93 |
+
# --- 3. تشغيل السيرفر (تصحيح الشرطات السفلية) ---
|
| 94 |
+
if name == "__main__":
|
| 95 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|