gijl commited on
Commit
a4ad16e
·
verified ·
1 Parent(s): 21a5a8f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoProcessor, AutoModelForImageTextToText
4
+ from PIL import Image
5
+
6
+ # اسم النموذج
7
+ model_id = "gijl/gemma-4-E4B-it"
8
+
9
+ # تحميل المعالج والنموذج
10
+ # استخدمنا torch.bfloat16 و low_cpu_mem_usage لمحاولة تقليل استهلاك الذاكرة قدر الإمكان
11
+ processor = AutoProcessor.from_pretrained(model_id)
12
+ model = AutoModelForImageTextToText.from_pretrained(
13
+ model_id,
14
+ torch_dtype=torch.bfloat16,
15
+ low_cpu_mem_usage=True,
16
+ device_map="auto"
17
+ )
18
+
19
+ def process_image_and_text(image, text):
20
+ if image is None:
21
+ return "الرجاء رفع صورة."
22
+ if not text:
23
+ text = "Describe this image." # نص افتراضي إذا لم يقم المستخدم بإدخال نص
24
+
25
+ # تجهيز المدخلات
26
+ inputs = processor(text=text, images=image, return_tensors="pt")
27
+ inputs = {k: v.to(model.device) for k, v in inputs.items()}
28
+
29
+ # توليد النص
30
+ with torch.no_grad():
31
+ generated_ids = model.generate(**inputs, max_new_tokens=100)
32
+
33
+ # فك تشفير النص الناتج
34
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
35
+ return generated_text
36
+
37
+ # بناء واجهة المستخدم باستخدام Gradio
38
+ demo = gr.Interface(
39
+ fn=process_image_and_text,
40
+ inputs=[
41
+ gr.Image(type="pil", label="رفع الصورة"),
42
+ gr.Textbox(label="أدخل سؤالك أو طلبك هنا", placeholder="مثال: ماذا يوجد في هذه الصورة؟")
43
+ ],
44
+ outputs=gr.Textbox(label="النتيجة"),
45
+ title="Gemma Image-to-Text Model",
46
+ description="تطبيق لتشغيل نموذج gijl/gemma-4-E4B-it للرؤية والنصوص."
47
+ )
48
+
49
+ # تشغيل الواجهة
50
+ if __name__ == "__main__":
51
+ demo.launch()