loayshabet commited on
Commit
3b85d8d
·
verified ·
1 Parent(s): 832bbd8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -1,14 +1,13 @@
1
  import gradio as gr
2
- from transformers import AutoProcessor, LlavaForConditionalGeneration
3
  import torch
4
  from PIL import Image
5
- import requests
6
  import io
7
 
8
- # 📦 تحميل النموذج والمُعالِج
9
- model_id = "llava-hf/llava-1.5-7b-hf"
10
- model = LlavaForConditionalGeneration.from_pretrained(model_id, device_map="auto")
11
  processor = AutoProcessor.from_pretrained(model_id)
 
12
 
13
  # 💬 تعليمات الذكاء الاصطناعي لتكون خبير تداول
14
  SYSTEM_PROMPT = """
@@ -30,20 +29,26 @@ def analyze_chart(image):
30
  return "❌ لم يتم تحميل أي صورة."
31
 
32
  try:
33
- # تحويل الصورة إلى تنسيق مناسب للنموذج
34
  img = Image.open(io.BytesIO(image)).convert("RGB")
35
 
36
- # إعداد prompt
37
- prompt = f"<image>\nUSER: {SYSTEM_PROMPT}\nASSISTANT:"
38
 
39
  # المعالجة
40
- inputs = processor(prompt, images=img, return_tensors='pt').to(0, torch.float16)
41
 
42
- # التوليد
43
- output = model.generate(**inputs, max_new_tokens=512)
44
- response = processor.decode(output[0], skip_special_tokens=True)
 
 
 
 
45
 
46
- return response.split("ASSISTANT:")[-1].strip()
 
 
47
 
48
  except Exception as e:
49
  return f"❌ حدث خطأ أثناء التحليل: {str(e)}"
@@ -53,10 +58,9 @@ interface = gr.Interface(
53
  fn=analyze_chart,
54
  inputs=gr.Image(type="bytes", label="تحميل مخطط التداول"),
55
  outputs=gr.Markdown(label="تحليل الذكاء الاصطناعي"),
56
- title="🤖 منصة تحليل التداول الذكية (بدون API)",
57
  description="استخدم الذكاء الاصطناعي المحلي لتحليل مخططات التداول وإعطاء تحليل احترافي.",
58
  theme="default",
59
- examples=[["example_chart.png"]]
60
  )
61
 
62
  if __name__ == "__main__":
 
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 = """
 
29
  return "❌ لم يتم تحميل أي صورة."
30
 
31
  try:
32
+ # فتح الصورة
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)}"
 
58
  fn=analyze_chart,
59
  inputs=gr.Image(type="bytes", label="تحميل مخطط التداول"),
60
  outputs=gr.Markdown(label="تحليل الذكاء الاصطناعي"),
61
+ title="🤖 منصة تحليل التداول الذكية (نسخة خفيفة)",
62
  description="استخدم الذكاء الاصطناعي المحلي لتحليل مخططات التداول وإعطاء تحليل احترافي.",
63
  theme="default",
 
64
  )
65
 
66
  if __name__ == "__main__":