Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,20 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
import torch
|
4 |
from PIL import Image
|
5 |
import io
|
|
|
6 |
|
7 |
-
|
8 |
-
|
|
|
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 = """
|
20 |
You are a professional technical analyst with 10 years of experience in financial markets.
|
21 |
Analyze the chart provided and answer the following:
|
22 |
|
23 |
1. What is the current trend? (Uptrend / Downtrend / Sideways)
|
24 |
2. Are there any key support/resistance levels?
|
25 |
-
3. What technical indicators do you see? (RSI, MACD,
|
26 |
4. Suggest a trade idea: Entry point, Stop Loss, Take Profit
|
27 |
5. Timeframe and Risk/Reward ratio
|
28 |
6. Strategy name and explanation
|
@@ -30,38 +22,42 @@ Analyze the chart provided and answer the following:
|
|
30 |
Answer in Arabic in a professional tone.
|
31 |
"""
|
32 |
|
|
|
|
|
|
|
|
|
|
|
33 |
def analyze_chart(image):
|
34 |
if image is None:
|
35 |
return "❌ لم يتم تحميل أي صورة."
|
|
|
|
|
36 |
|
37 |
try:
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
except Exception as e:
|
54 |
-
return f"❌
|
55 |
-
|
56 |
-
# 🖥️ واجهة Gradio
|
57 |
-
interface = gr.Interface(
|
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 |
-
|
67 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
|
|
3 |
from PIL import Image
|
4 |
import io
|
5 |
+
import base64
|
6 |
|
7 |
+
OPENROUTER_API_URL = "https://openrouter.ai/api/v1/chat/completions"
|
8 |
+
OPENROUTER_API_KEY = "YOUR_API_KEY_HERE"
|
9 |
+
MODEL_NAME = "google/gemini-flash-1.5"
|
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:
|
14 |
|
15 |
1. What is the current trend? (Uptrend / Downtrend / Sideways)
|
16 |
2. Are there any key support/resistance levels?
|
17 |
+
3. What technical indicators do you see? (RSI, MACD, Bollinger Bands...)
|
18 |
4. Suggest a trade idea: Entry point, Stop Loss, Take Profit
|
19 |
5. Timeframe and Risk/Reward ratio
|
20 |
6. Strategy name and explanation
|
|
|
22 |
Answer in Arabic in a professional tone.
|
23 |
"""
|
24 |
|
25 |
+
def image_to_data_uri(img):
|
26 |
+
buffered = io.BytesIO()
|
27 |
+
img.save(buffered, format=img.format)
|
28 |
+
return "data:image/png;base64," + base64.b64encode(buffered.getvalue()).decode()
|
29 |
+
|
30 |
def analyze_chart(image):
|
31 |
if image is None:
|
32 |
return "❌ لم يتم تحميل أي صورة."
|
33 |
+
|
34 |
+
data_uri = image_to_data_uri(image)
|
35 |
|
36 |
try:
|
37 |
+
response = requests.post(
|
38 |
+
OPENROUTER_API_URL,
|
39 |
+
headers={
|
40 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
41 |
+
"Content-Type": "application/json"
|
42 |
+
},
|
43 |
+
json={
|
44 |
+
"model": MODEL_NAME,
|
45 |
+
"messages": [
|
46 |
+
{
|
47 |
+
"role": "user",
|
48 |
+
"content": [
|
49 |
+
{"type": "text", "text": SYSTEM_PROMPT},
|
50 |
+
{"type": "image_url", "image_url": {"url": data_uri}}
|
51 |
+
]
|
52 |
+
}
|
53 |
+
],
|
54 |
+
"max_tokens": 512
|
55 |
+
}
|
56 |
+
)
|
57 |
+
result = response.json()
|
58 |
+
return result["choices"][0]["message"]["content"]
|
59 |
except Exception as e:
|
60 |
+
return f"❌ خطأ: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
interface = gr.Interface(fn=analyze_chart, inputs=gr.Image(type="pil"), outputs="markdown")
|
63 |
+
interface.launch()
|