loayshabet commited on
Commit
d0a587a
·
verified ·
1 Parent(s): 63936a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -44
app.py CHANGED
@@ -1,13 +1,14 @@
1
  import gradio as gr
 
2
  from PIL import Image
3
  import io
4
- import base64
5
- import requests
6
 
7
- # استيراد الإعدادات
8
- from config import OPENROUTER_API_KEY, OPENROUTER_API_URL, MODEL_NAME
 
 
9
 
10
- # تعليمات الذكاء الاصطناعي
11
  SYSTEM_PROMPT = """
12
  You are a professional technical analyst with 10 years of experience in financial markets.
13
  Analyze the chart provided and answer the following:
@@ -22,61 +23,37 @@ Analyze the chart provided and answer the following:
22
  Answer in Arabic in a professional tone.
23
  """
24
 
25
- # دالة تحويل الصورة إلى Data URI
26
- def image_to_data_uri(img):
27
- buffered = io.BytesIO()
28
- img_format = img.format if img.format else "PNG"
29
- img.save(buffered, format=img_format)
30
- return "data:image/png;base64," + base64.b64encode(buffered.getvalue()).decode("utf-8")
31
-
32
- # دالة التحليل
33
  def analyze_chart(image):
34
  if image is None:
35
  return "❌ لم يتم تحميل أي صورة."
36
 
37
  try:
38
- data_uri = image_to_data_uri(image)
 
39
 
40
- response = requests.post(
41
- OPENROUTER_API_URL,
42
- headers={
43
- "Authorization": f"Bearer {OPENROUTER_API_KEY}",
44
- "Content-Type": "application/json"
45
- },
46
- json={
47
- "model": MODEL_NAME,
48
- "messages": [
49
- {
50
- "role": "user",
51
- "content": [
52
- {"type": "text", "text": SYSTEM_PROMPT},
53
- {"type": "image_url", "image_url": {"url": data_uri}}
54
- ]
55
- }
56
- ],
57
- "max_tokens": 384 # ← عدد توكينات مناسب لهذا النموذج
58
- }
59
- )
60
 
61
- result = response.json()
 
62
 
63
- # التعامل مع الخطأ إذا لم يُعَدّ النموذج إجابة
64
- if "choices" not in result or len(result["choices"]) == 0:
65
- return f"❌ لم يتم العثور على إجابة من الذكاء الاصطناعي.\nالاستجابة: {result}"
66
 
67
- return result["choices"][0]["message"]["content"]
68
 
69
  except Exception as e:
70
- return f"❌ حدث خطأ أثناء التحليل:\n{str(e)}"
71
 
72
- # واجهة Gradio
73
  interface = gr.Interface(
74
  fn=analyze_chart,
75
  inputs=gr.Image(type="pil", label="تحميل مخطط التداول"),
76
  outputs=gr.Markdown(label="تحليل الذكاء الاصطناعي"),
77
- title="🤖 منصة تحليل التداول الذكية (Pixtral-12B)",
78
- description="ارسل مخططًا وسيقوم الذكاء الاصطناعي بتحليله وتقديم صفقات احترافية.",
79
- theme="default"
80
  )
81
 
82
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ from transformers import AutoProcessor, AutoModelForVision2Seq
3
  from PIL import Image
4
  import io
 
 
5
 
6
+ # 🔍 تحميل النموذج والمُعالِج
7
+ model_id = "HuggingFaceM4/sla-v1"
8
+ processor = AutoProcessor.from_pretrained(model_id)
9
+ model = AutoModelForVision2Seq.from_pretrained(model_id).to("cuda" if torch.cuda.is_available() else "cpu")
10
 
11
+ # 💬 تعليمات الذكاء الاصطناعي لتكون خبير تداول
12
  SYSTEM_PROMPT = """
13
  You are a professional technical analyst with 10 years of experience in financial markets.
14
  Analyze the chart provided and answer the following:
 
23
  Answer in Arabic in a professional tone.
24
  """
25
 
 
 
 
 
 
 
 
 
26
  def analyze_chart(image):
27
  if image is None:
28
  return "❌ لم يتم تحميل أي صورة."
29
 
30
  try:
31
+ # تحويل الصورة إلى RGB
32
+ img = image.convert("RGB")
33
 
34
+ # إنشاء prompt شامل
35
+ text = f"Question: {SYSTEM_PROMPT}\nAnswer:"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ # المعالجة
38
+ inputs = processor(images=img, text=text, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
39
 
40
+ # التوليد
41
+ generated_ids = model.generate(**inputs, max_new_tokens=256)
42
+ response = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
43
 
44
+ return response.strip()
45
 
46
  except Exception as e:
47
+ return f"❌ حدث خطأ أثناء التحليل: {str(e)}"
48
 
49
+ # 🖥️ واجهة Gradio
50
  interface = gr.Interface(
51
  fn=analyze_chart,
52
  inputs=gr.Image(type="pil", label="تحميل مخطط التداول"),
53
  outputs=gr.Markdown(label="تحليل الذكاء الاصطناعي"),
54
+ title="🤖 منصة تحليل التداول الذكية (نسخة مجانية)",
55
+ description="استخدم الذكاء الاصطناعي المحلي لتحليل مخططات التداول وإعطاء تحليل احترافي.",
56
+ theme="default",
57
  )
58
 
59
  if __name__ == "__main__":