TradingBot / app.py
loayshabet's picture
Update app.py
e227d9a verified
raw
history blame
2.94 kB
import gradio as gr
from PIL import Image
import io
import base64
import requests
# استيراد الإعدادات
from config import OPENROUTER_API_KEY, OPENROUTER_API_URL, MODEL_NAME
# تعليمات الذكاء الاصطناعي
SYSTEM_PROMPT = """
You are a professional technical analyst with 10 years of experience in financial markets.
Analyze the chart provided and answer the following:
1. What is the current trend? (Uptrend / Downtrend / Sideways)
2. Are there any key support/resistance levels?
3. What technical indicators do you see? (RSI, MACD, Bollinger Bands...)
4. Suggest a trade idea: Entry point, Stop Loss, Take Profit
5. Timeframe and Risk/Reward ratio
6. Strategy name and explanation
Answer in Arabic in a professional tone.
"""
# دالة تحويل الصورة إلى Data URI
def image_to_data_uri(img):
buffered = io.BytesIO()
img_format = img.format if img.format else "PNG"
img.save(buffered, format=img_format)
return "data:image/png;base64," + base64.b64encode(buffered.getvalue()).decode("utf-8")
# دالة التحليل
def analyze_chart(image):
if image is None:
return "❌ لم يتم تحميل أي صورة."
try:
data_uri = image_to_data_uri(image)
response = requests.post(
OPENROUTER_API_URL,
headers={
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
"Content-Type": "application/json"
},
json={
"model": MODEL_NAME,
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": SYSTEM_PROMPT},
{"type": "image_url", "image_url": {"url": data_uri}}
]
}
],
"max_tokens": 512
}
)
result = response.json()
# التعامل مع الخطأ إذا لم يُعَدّ النموذج إجابة
if "choices" not in result or len(result["choices"]) == 0:
return f"❌ لم يتم العثور على إجابة من الذكاء الاصطناعي.\nالاستجابة: {result}"
return result["choices"][0]["message"]["content"]
except Exception as e:
return f"❌ حدث خطأ أثناء التحليل:\n{str(e)}"
# واجهة Gradio
interface = gr.Interface(
fn=analyze_chart,
inputs=gr.Image(type="pil", label="تحميل مخطط التداول"),
outputs=gr.Markdown(label="تحليل الذكاء الاصطناعي"),
title="🤖 منصة تحليل التداول الذكية (Google Gemini)",
description="ارسل مخططًا وسيقوم الذكاء الاصطناعي بتحليله وتقديم صفقات احترافية.",
theme="default"
)
if __name__ == "__main__":
interface.launch()