Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
|
|
|
|
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 |
-
|
|
|
39 |
|
40 |
-
|
41 |
-
|
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 |
-
|
|
|
62 |
|
63 |
-
#
|
64 |
-
|
65 |
-
|
66 |
|
67 |
-
return
|
68 |
|
69 |
except Exception as e:
|
70 |
-
return f"❌ حدث خطأ أثناء
|
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="🤖 منصة تحليل التداول الذكية (
|
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__":
|