Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
from ultralytics import YOLO
|
4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
+
import torch
|
6 |
+
|
7 |
+
# Load YOLOv8 pattern detector
|
8 |
+
pattern_model = YOLO("foduucom/stockmarket-pattern-detection-yolov8")
|
9 |
+
|
10 |
+
# Load Trading-Hero-LLM
|
11 |
+
llm_tokenizer = AutoTokenizer.from_pretrained("fuchenru/Trading-Hero-LLM")
|
12 |
+
llm_model = AutoModelForCausalLM.from_pretrained("fuchenru/Trading-Hero-LLM")
|
13 |
+
|
14 |
+
# Function: Run both models
|
15 |
+
def analyze_chart_and_question(image, question):
|
16 |
+
results = []
|
17 |
+
|
18 |
+
# Pattern detection with YOLO
|
19 |
+
if image:
|
20 |
+
boxes = pattern_model(image)[0]
|
21 |
+
names = boxes.names
|
22 |
+
results.append("📊 Detected Chart Patterns:")
|
23 |
+
for box in boxes.boxes:
|
24 |
+
cls = int(box.cls[0])
|
25 |
+
label = names[cls]
|
26 |
+
results.append(f"• {label}")
|
27 |
+
else:
|
28 |
+
results.append("🖼️ No image uploaded.")
|
29 |
+
|
30 |
+
# Trading-Hero-LLM response
|
31 |
+
if question:
|
32 |
+
inputs = llm_tokenizer(question, return_tensors="pt")
|
33 |
+
with torch.no_grad():
|
34 |
+
output = llm_model.generate(**inputs, max_new_tokens=100)
|
35 |
+
response = llm_tokenizer.decode(output[0], skip_special_tokens=True)
|
36 |
+
results.append("\n🧠 Trading Hero LLM Response:\n" + response)
|
37 |
+
else:
|
38 |
+
results.append("💬 No question entered.")
|
39 |
+
|
40 |
+
return "\n".join(results)
|
41 |
+
|
42 |
+
# Gradio UI
|
43 |
+
iface = gr.Interface(
|
44 |
+
fn=analyze_chart_and_question,
|
45 |
+
inputs=[
|
46 |
+
gr.Image(label="Upload Nifty Chart", type="pil"),
|
47 |
+
gr.Textbox(label="Ask a Trading Question")
|
48 |
+
],
|
49 |
+
outputs=gr.Textbox(label="AI Output"),
|
50 |
+
title="📊 Nifty Trading Assistant",
|
51 |
+
description="Upload a chart to detect patterns (YOLOv8) and ask a trading question (Trading-Hero-LLM)."
|
52 |
+
)
|
53 |
+
|
54 |
+
iface.launch()
|