Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import torch | |
| from PIL import Image | |
| from transformers import CLIPProcessor, CLIPModel | |
| # تحميل الموديل والمعالج | |
| model_id = "openai/clip-vit-base-patch32" | |
| model = CLIPModel.from_pretrained(model_id) | |
| processor = CLIPProcessor.from_pretrained(model_id) | |
| def classify_child_image(image, labels_text): | |
| if image is None: | |
| return "Please upload an image." | |
| # تحويل النص المدخل إلى قائمة من التصنيفات | |
| labels = [label.strip() for label in labels_text.split(",")] | |
| # معالجة المدخلات | |
| inputs = processor(text=labels, images=image, return_tensors="pt", padding=True) | |
| # التنبؤ | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| # حساب الاحتمالات | |
| logits_per_image = outputs.logits_per_image | |
| probs = logits_per_image.softmax(dim=1).numpy()[0] | |
| # تجهيز النتيجة بصيغة قاموس لواجهة Gradio | |
| return {labels[i]: float(probs[i]) for i in range(len(labels))} | |
| # بناء واجهة المستخدم | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 📸 Child Case Classification (CLIP)") | |
| gr.Markdown("ارفع صورة الطفل واكتب الحالات المحتملة مفصولة بفاصلة (مثلاً: healthy, skin rash, measuring)") | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_img = gr.Image(type="pil", label="الصورة") | |
| input_labels = gr.Textbox( | |
| label="التصنيفات (فصل بفاصلة)", | |
| placeholder="healthy, rash, crying..." | |
| ) | |
| btn = gr.Button("تحليل الحالة") | |
| with gr.Column(): | |
| output_label = gr.Label(label="النتائج") | |
| btn.click(fn=classify_child_image, inputs=[input_img, input_labels], outputs=output_label) | |
| # تشغيل الواجهة | |
| if __name__ == "__main__": | |
| demo.launch() | |