Spaces:
Running
Running
QAway-to
commited on
Commit
·
985f897
1
Parent(s):
4afef48
New stcructure v1.2
Browse files- .idea/TradeLinkAI.iml +7 -0
- app.py +35 -66
- core/analyzer.py +5 -3
.idea/TradeLinkAI.iml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<module version="4">
|
| 3 |
+
<component name="PyDocumentationSettings">
|
| 4 |
+
<option name="format" value="PLAIN" />
|
| 5 |
+
<option name="myDocStringFormat" value="Plain" />
|
| 6 |
+
</component>
|
| 7 |
+
</module>
|
app.py
CHANGED
|
@@ -3,82 +3,51 @@ from services.llm_client import llm_service
|
|
| 3 |
from core.analyzer import PortfolioAnalyzer
|
| 4 |
from core.comparer import PortfolioComparer
|
| 5 |
from core.chat import ChatAssistant
|
| 6 |
-
from core.visualization import build_alpha_chart
|
| 7 |
from core.metrics import show_metrics_table
|
|
|
|
| 8 |
|
| 9 |
# === Model setup ===
|
| 10 |
-
MODEL_NAME = "
|
| 11 |
|
| 12 |
# === Use case classes ===
|
| 13 |
analyzer = PortfolioAnalyzer(llm_service, MODEL_NAME)
|
| 14 |
comparer = PortfolioComparer(llm_service, MODEL_NAME)
|
| 15 |
chatbot = ChatAssistant(llm_service, MODEL_NAME)
|
| 16 |
|
| 17 |
-
|
| 18 |
# === Gradio Interface ===
|
| 19 |
-
with gr.Blocks() as demo:
|
| 20 |
-
gr.Markdown("## 🧠
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
with gr.Tab("
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
chat_button.click(
|
| 54 |
-
fn=chatbot.run,
|
| 55 |
-
inputs=chat_input,
|
| 56 |
-
outputs=chat_output,
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
# --- Tab: Metrics Table ---
|
| 60 |
-
with gr.Tab("📋 Metrics Table"):
|
| 61 |
-
metrics_input = gr.Textbox(label="Enter portfolioId")
|
| 62 |
-
metrics_button = gr.Button("Show Metrics")
|
| 63 |
-
metrics_output = gr.Dataframe(label="Portfolio Metrics")
|
| 64 |
-
|
| 65 |
-
metrics_button.click(
|
| 66 |
-
fn=show_metrics_table,
|
| 67 |
-
inputs=metrics_input,
|
| 68 |
-
outputs=metrics_output,
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
-
# --- Tab: AlphaBTC Chart ---
|
| 72 |
-
with gr.Tab("📈 AlphaBTC Chart"):
|
| 73 |
-
chart_input = gr.Textbox(label="Portfolio ID", value="3852a354-e66e-4bc5-97e9-55124e31e687")
|
| 74 |
-
chart_button = gr.Button("Plot Chart")
|
| 75 |
-
chart_output = gr.Plot(label="Alpha vs BTC")
|
| 76 |
-
|
| 77 |
-
chart_button.click(
|
| 78 |
-
fn=build_alpha_chart,
|
| 79 |
-
inputs=chart_input,
|
| 80 |
-
outputs=chart_output,
|
| 81 |
-
)
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
| 84 |
demo.launch()
|
|
|
|
| 3 |
from core.analyzer import PortfolioAnalyzer
|
| 4 |
from core.comparer import PortfolioComparer
|
| 5 |
from core.chat import ChatAssistant
|
|
|
|
| 6 |
from core.metrics import show_metrics_table
|
| 7 |
+
from core.visualization import build_alpha_chart
|
| 8 |
|
| 9 |
# === Model setup ===
|
| 10 |
+
MODEL_NAME = "meta-llama/Meta-Llama-3.1-8B-Instruct"
|
| 11 |
|
| 12 |
# === Use case classes ===
|
| 13 |
analyzer = PortfolioAnalyzer(llm_service, MODEL_NAME)
|
| 14 |
comparer = PortfolioComparer(llm_service, MODEL_NAME)
|
| 15 |
chatbot = ChatAssistant(llm_service, MODEL_NAME)
|
| 16 |
|
|
|
|
| 17 |
# === Gradio Interface ===
|
| 18 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 19 |
+
gr.Markdown("## 🧠 Анализ и сравнение инвестиционных портфелей **TradeLink**")
|
| 20 |
+
|
| 21 |
+
with gr.Tab("📊 Анализ"):
|
| 22 |
+
portfolio_input = gr.Textbox(label="Введите ссылку или portfolioId", placeholder="ea856c1b-...")
|
| 23 |
+
analyze_button = gr.Button("🔍 Проанализировать")
|
| 24 |
+
analyze_output = gr.Textbox(label="📈 Результат анализа", lines=15)
|
| 25 |
+
analyze_button.click(fn=analyzer.run, inputs=portfolio_input, outputs=analyze_output)
|
| 26 |
+
|
| 27 |
+
with gr.Tab("⚖️ Сравнение"):
|
| 28 |
+
compare_input_1 = gr.Textbox(label="Портфель 1", placeholder="ea856c1b-...")
|
| 29 |
+
compare_input_2 = gr.Textbox(label="Портфель 2", placeholder="d52f55cc-...")
|
| 30 |
+
compare_button = gr.Button("📊 Сравнить")
|
| 31 |
+
compare_output = gr.Textbox(label="📉 Результат сравнения", lines=20)
|
| 32 |
+
compare_button.click(fn=comparer.run, inputs=[compare_input_1, compare_input_2], outputs=compare_output)
|
| 33 |
+
|
| 34 |
+
with gr.Tab("💬 Диалог"):
|
| 35 |
+
chat_input = gr.Textbox(label="Ваш вопрос", placeholder="Что такое TradeLink Passport?")
|
| 36 |
+
chat_button = gr.Button("Отправить")
|
| 37 |
+
chat_output = gr.Textbox(label="Ответ", lines=8)
|
| 38 |
+
chat_button.click(fn=chatbot.run, inputs=chat_input, outputs=chat_output)
|
| 39 |
+
|
| 40 |
+
with gr.Tab("📋 Таблица метрик"):
|
| 41 |
+
metrics_input = gr.Textbox(label="Введите portfolioId", placeholder="ea856c1b-...")
|
| 42 |
+
metrics_button = gr.Button("📥 Показать метрики")
|
| 43 |
+
metrics_output = gr.Dataframe(label="📊 Метрики портфеля", wrap=True)
|
| 44 |
+
metrics_button.click(fn=show_metrics_table, inputs=metrics_input, outputs=metrics_output)
|
| 45 |
+
|
| 46 |
+
with gr.Tab("📈 AlphaBTC график"):
|
| 47 |
+
chart_input = gr.Textbox(label="Введите portfolioId", value="3852a354-e66e-4bc5-97e9-55124e31e687")
|
| 48 |
+
chart_button = gr.Button("Построить график")
|
| 49 |
+
chart_output = gr.Plot(label="График Alpha к BTC")
|
| 50 |
+
chart_button.click(fn=build_alpha_chart, inputs=chart_input, outputs=chart_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
demo.launch()
|
core/analyzer.py
CHANGED
|
@@ -9,7 +9,7 @@ Purpose: Handles single-portfolio analysis using LLM. Fetches metrics, builds pr
|
|
| 9 |
import asyncio
|
| 10 |
from typing import Generator
|
| 11 |
from services.tradelink_api import extract_portfolio_id, fetch_metrics_async
|
| 12 |
-
from services.llm_client import
|
| 13 |
from prompts.system_prompts import ANALYSIS_SYSTEM_PROMPT
|
| 14 |
from prompts.reference_templates import REFERENCE_PROMPT
|
| 15 |
|
|
@@ -17,7 +17,8 @@ from prompts.reference_templates import REFERENCE_PROMPT
|
|
| 17 |
class PortfolioAnalyzer:
|
| 18 |
"""Main use-case class for analyzing a single portfolio."""
|
| 19 |
|
| 20 |
-
def __init__(self, llm
|
|
|
|
| 21 |
self.llm = llm
|
| 22 |
self.model_name = model_name
|
| 23 |
|
|
@@ -39,7 +40,8 @@ class PortfolioAnalyzer:
|
|
| 39 |
yield "❗ Unable to retrieve metrics from API."
|
| 40 |
return
|
| 41 |
|
| 42 |
-
|
|
|
|
| 43 |
prompt = f"{REFERENCE_PROMPT}\n\nUse this data for analysis:\n{metrics_text}"
|
| 44 |
|
| 45 |
try:
|
|
|
|
| 9 |
import asyncio
|
| 10 |
from typing import Generator
|
| 11 |
from services.tradelink_api import extract_portfolio_id, fetch_metrics_async
|
| 12 |
+
from services.llm_client import llm_service # Hugging Face client
|
| 13 |
from prompts.system_prompts import ANALYSIS_SYSTEM_PROMPT
|
| 14 |
from prompts.reference_templates import REFERENCE_PROMPT
|
| 15 |
|
|
|
|
| 17 |
class PortfolioAnalyzer:
|
| 18 |
"""Main use-case class for analyzing a single portfolio."""
|
| 19 |
|
| 20 |
+
def __init__(self, llm=llm_service, model_name: str = "mistralai/Mixtral-8x7B-Instruct"):
|
| 21 |
+
# llm must expose .stream_chat(messages=list[dict], model=str) -> Iterable[str]
|
| 22 |
self.llm = llm
|
| 23 |
self.model_name = model_name
|
| 24 |
|
|
|
|
| 40 |
yield "❗ Unable to retrieve metrics from API."
|
| 41 |
return
|
| 42 |
|
| 43 |
+
# Build prompt text from metrics
|
| 44 |
+
metrics_text = ", ".join(f"{k}: {v}" for k, v in metrics.items())
|
| 45 |
prompt = f"{REFERENCE_PROMPT}\n\nUse this data for analysis:\n{metrics_text}"
|
| 46 |
|
| 47 |
try:
|