TradingBot / app.py
loayshabet's picture
Update app.py
6ee159d verified
raw
history blame
2.34 kB
import gradio as gr
from transformers import AutoProcessor, LlavaForConditionalGeneration
import torch
from PIL import Image
import requests
import io
# 📦 تحميل النموذج والمُعالِج
model_id = "llava-hf/llava-1.5-7b-hf"
model = LlavaForConditionalGeneration.from_pretrained(model_id, device_map="auto")
processor = AutoProcessor.from_pretrained(model_id)
# 💬 تعليمات الذكاء الاصطناعي لتكون خبير تداول
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
prompt = f"<image>\nUSER: {SYSTEM_PROMPT}\nASSISTANT:"
# المعالجة
inputs = processor(prompt, images=img, return_tensors='pt').to(0, torch.float16)
# التوليد
output = model.generate(**inputs, max_new_tokens=512)
response = processor.decode(output[0], skip_special_tokens=True)
return response.split("ASSISTANT:")[-1].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="🤖 منصة تحليل التداول الذكية (بدون API)",
description="استخدم الذكاء الاصطناعي المحلي لتحليل مخططات التداول وإعطاء تحليل احترافي.",
theme="default",
examples=[["example_chart.png"]]
)
if __name__ == "__main__":
interface.launch()