loayshabet commited on
Commit
6ee159d
·
verified ·
1 Parent(s): 16f43e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -30
app.py CHANGED
@@ -1,42 +1,50 @@
1
- import google.generativeai as genai
 
 
2
  from PIL import Image
 
3
  import io
4
- import gradio as gr
5
- import os
6
 
7
- # ⚙️ إعدادات Gemini API
8
- GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") # سيتم وضعه في HuggingFace Secrets
9
- genai.configure(api_key=GEMINI_API_KEY)
10
- model = genai.GenerativeModel("gemini-pro-vision")
11
 
12
- # 🧠 تعليمات للذكاء الاصطناعي ليتصرف كخبير تداول
13
  SYSTEM_PROMPT = """
14
- أنت محلل تداول محترف ولديك خبرة 10 سنوات في أسواق الفوركس والعملات الرقمية.
15
- قم بتحليل الصورة التالية وأجبني بما يلي:
16
 
17
- 1. ما هو الاتجاه الحالي؟ (صعودي / هبوطي / جانبي)
18
- 2. مستويات الدعم والمقاومة الرئيسية.
19
- 3. المؤشرات الفنية: RSI, MACD, Bollinger Bands...
20
- 4. اقتراح صفقة: نقطة الدخول، وقف الخسارة، الربح المستهدف.
21
- 5. الإطار الزمني المناسب ونسبة المخاطرة/العائد.
22
- 6. اسم الاستراتيجية وتفسيرها.
23
 
24
- قدم إجابتك باللغة العربية بشكل احترافي وتعليمي.
25
  """
26
 
27
- # 📊 الدالة الرئيسية لتحليل الصورة
28
  def analyze_chart(image):
29
  if image is None:
30
  return "❌ لم يتم تحميل أي صورة."
31
-
32
  try:
33
- img = Image.open(io.BytesIO(image))
34
- response = model.generate_content([SYSTEM_PROMPT, img])
35
-
36
- if response.text:
37
- return response.text
38
- else:
39
- return "⚠️ لم يتم العثور على تحليل لهذه الصورة."
 
 
 
 
 
 
 
 
40
  except Exception as e:
41
  return f"❌ حدث خطأ أثناء التحليل: {str(e)}"
42
 
@@ -45,11 +53,11 @@ interface = gr.Interface(
45
  fn=analyze_chart,
46
  inputs=gr.Image(type="bytes", label="تحميل مخطط التداول"),
47
  outputs=gr.Markdown(label="تحليل الذكاء الاصطناعي"),
48
- title="🤖 منصة تحليل التداول الذكية",
49
- description="ارسل مخططًا وسيقوم الذكاء الاصطناعي بتحليله وتقديم صفقات احترافية.",
50
- theme="default"
 
51
  )
52
 
53
- # تشغيل التطبيق
54
  if __name__ == "__main__":
55
  interface.launch()
 
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 = """
15
+ You are a professional technical analyst with 10 years of experience in financial markets.
16
+ Analyze the chart provided and answer the following:
17
 
18
+ 1. What is the current trend? (Uptrend / Downtrend / Sideways)
19
+ 2. Are there any key support/resistance levels?
20
+ 3. What technical indicators do you see? (RSI, MACD, etc.)
21
+ 4. Suggest a trade idea: Entry point, Stop Loss, Take Profit
22
+ 5. Timeframe and Risk/Reward ratio
23
+ 6. Strategy name and explanation
24
 
25
+ Answer in Arabic in a professional tone.
26
  """
27
 
 
28
  def analyze_chart(image):
29
  if image is None:
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)}"
50
 
 
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__":
63
  interface.launch()