TradingBot / app.py
loayshabet's picture
Update app.py
3b85d8d verified
raw
history blame
2.44 kB
import gradio as gr
from transformers import AutoProcessor, Kosmos2Model
import torch
from PIL import Image
import io
# 📦 تحميل المعالج والنموذج
model_id = "microsoft/kosmos-2-patch14-224"
processor = AutoProcessor.from_pretrained(model_id)
model = Kosmos2Model.from_pretrained(model_id).to("cuda" if torch.cuda.is_available() else "cpu")
# 💬 تعليمات الذكاء الاصطناعي لتكون خبير تداول
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, etc.)
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.
"""
def analyze_chart(image):
if image is None:
return "❌ لم يتم تحميل أي صورة."
try:
# فتح الصورة
img = Image.open(io.BytesIO(image)).convert("RGB")
# إنشاء prompt شامل
text = f"Question: {SYSTEM_PROMPT}\nAnswer:"
# المعالجة
inputs = processor(images=img, text=text, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
# توليد التحليل
generated_ids = model.generate(
pixel_values=inputs["pixel_values"],
input_ids=inputs["input_ids"],
attention_mask=inputs["attention_mask"],
max_new_tokens=256
)
response = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
return response.strip()
except Exception as e:
return f"❌ حدث خطأ أثناء التحليل: {str(e)}"
# 🖥️ واجهة Gradio
interface = gr.Interface(
fn=analyze_chart,
inputs=gr.Image(type="bytes", label="تحميل مخطط التداول"),
outputs=gr.Markdown(label="تحليل الذكاء الاصطناعي"),
title="🤖 منصة تحليل التداول الذكية (نسخة خفيفة)",
description="استخدم الذكاء الاصطناعي المحلي لتحليل مخططات التداول وإعطاء تحليل احترافي.",
theme="default",
)
if __name__ == "__main__":
interface.launch()