Mazenbs commited on
Commit
afc819d
·
verified ·
1 Parent(s): 445477c

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +47 -0
main.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForImageClassification, AutoImageProcessor
3
+ from PIL import Image
4
+ import torch
5
+
6
+ # تحميل النموذج
7
+ model = AutoModelForImageClassification.from_pretrained(
8
+ "asyafalni/arabichar-v3",
9
+ trust_remote_code=True
10
+ )
11
+
12
+ # استخدام معالج صور جاهز
13
+ processor = AutoImageProcessor.from_pretrained(
14
+ "google/vit-base-patch16-224-in21k"
15
+ )
16
+
17
+ # دالة لمعالجة الصورة واستخراج النص
18
+ def classify_image(image):
19
+ try:
20
+ # تحويل الصورة إلى RGB
21
+ image = image.convert("RGB")
22
+
23
+ # تجهيز المدخلات
24
+ inputs = processor(images=image, return_tensors="pt")
25
+
26
+ # تمريرها للنموذج
27
+ with torch.no_grad():
28
+ outputs = model(**inputs)
29
+ logits = outputs.logits
30
+ predicted_class = logits.argmax(-1).item()
31
+ label = model.config.id2label.get(predicted_class, "Unknown")
32
+
33
+ return label
34
+ except Exception as e:
35
+ return f"حدث خطأ: {e}"
36
+
37
+ # إنشاء واجهة Gradio
38
+ iface = gr.Interface(
39
+ fn=classify_image,
40
+ inputs=gr.Image(type="pil", label="رفع صورة"),
41
+ outputs=gr.Textbox(label="النص المستخرج"),
42
+ title="تصنيف الصور - Arabic Char",
43
+ description="ارفع صورة وسيتم عرض النص المستخرج أسفلها."
44
+ )
45
+
46
+ # تشغيل الواجهة
47
+ iface.launch()