loayshabet commited on
Commit
f114d79
·
verified ·
1 Parent(s): 3b60dce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -1,13 +1,19 @@
1
  import gradio as gr
2
- from transformers import AutoProcessor, Kosmos2Model
3
  import torch
4
  from PIL import Image
5
  import io
6
 
7
- # 📦 تحميل المعالج والنموذج
8
- model_id = "microsoft/kosmos-2-patch14-224"
 
 
 
 
 
 
 
9
  processor = AutoProcessor.from_pretrained(model_id)
10
- model = Kosmos2Model.from_pretrained(model_id).to("cuda" if torch.cuda.is_available() else "cpu")
11
 
12
  # 💬 تعليمات الذكاء الاصطناعي لتكون خبير تداول
13
  SYSTEM_PROMPT = """
@@ -33,22 +39,16 @@ def analyze_chart(image):
33
  img = Image.open(io.BytesIO(image)).convert("RGB")
34
 
35
  # إنشاء prompt شامل
36
- text = f"Question: {SYSTEM_PROMPT}\nAnswer:"
37
 
38
  # المعالجة
39
- inputs = processor(images=img, text=text, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
40
 
41
  # توليد التحليل
42
- generated_ids = model.generate(
43
- pixel_values=inputs["pixel_values"],
44
- input_ids=inputs["input_ids"],
45
- attention_mask=inputs["attention_mask"],
46
- max_new_tokens=256
47
- )
48
-
49
- response = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
50
 
51
- return response.strip()
52
 
53
  except Exception as e:
54
  return f"❌ حدث خطأ أثناء التحليل: {str(e)}"
 
1
  import gradio as gr
2
+ from transformers import AutoProcessor, LlavaForConditionalGeneration
3
  import torch
4
  from PIL import Image
5
  import io
6
 
7
+ # 📦 تحميل النموذج والمُعالِج
8
+ model_id = "dima80/llava-7b-sla-lvis4v_train_1k"
9
+
10
+ # 🔍 تقليل استهلاك الذاكرة باستخدام 8-bit quantization
11
+ model = LlavaForConditionalGeneration.from_pretrained(
12
+ model_id,
13
+ device_map="auto",
14
+ load_in_8bit=True
15
+ )
16
  processor = AutoProcessor.from_pretrained(model_id)
 
17
 
18
  # 💬 تعليمات الذكاء الاصطناعي لتكون خبير تداول
19
  SYSTEM_PROMPT = """
 
39
  img = Image.open(io.BytesIO(image)).convert("RGB")
40
 
41
  # إنشاء prompt شامل
42
+ text = f"<image>\nUSER: {SYSTEM_PROMPT}\nASSISTANT:"
43
 
44
  # المعالجة
45
+ inputs = processor(text, images=img, return_tensors='pt').to(0)
46
 
47
  # توليد التحليل
48
+ output = model.generate(**inputs, max_new_tokens=256)
49
+ response = processor.decode(output[0], skip_special_tokens=True)
 
 
 
 
 
 
50
 
51
+ return response.split("ASSISTANT:")[-1].strip()
52
 
53
  except Exception as e:
54
  return f"❌ حدث خطأ أثناء التحليل: {str(e)}"