Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import asyncio | |
| from prompts import TRADELINK_CONTEXT | |
| from openai import OpenAI | |
| from fastapi import FastAPI | |
| from analyzer import analyze_portfolio_streaming | |
| from comparer import compare_portfolio | |
| from table_view import get_metrics_dataframe | |
| from alpha_chart import plot_alpha_btc_chart | |
| from rest_api import app as rest_api_app | |
| api_key = os.getenv("featherless") # | |
| client = OpenAI( | |
| base_url="https://api.featherless.ai/v1", | |
| api_key=api_key | |
| ) | |
| def show_metrics_table(pid: str): | |
| try: | |
| df = asyncio.run(get_metrics_dataframe(pid)) | |
| return df | |
| except Exception as e: | |
| return f"❌ Ошибка: {e}" | |
| def handle_chat_streaming(user_input): | |
| response = client.chat.completions.create( | |
| model="meta-llama/Meta-Llama-3.1-8B-Instruct", | |
| messages=[ | |
| {"role": "system", "content": TRADELINK_CONTEXT}, | |
| {"role": "user", "content": user_input} | |
| ], | |
| stream=True | |
| ) | |
| partial = "" | |
| for chunk in response: | |
| delta = chunk.choices[0].delta.content | |
| if delta: | |
| partial += delta | |
| yield partial | |
| def analyze_one_portfolio(text): | |
| yield from analyze_portfolio_streaming(text, client) | |
| def compare_analyze(text1, text2): | |
| yield from compare_portfolio(text1, text2, client) | |
| # Gradio интерфейс | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## 🧠 Анализ и сравнение инвестиционных портфелей Tradelink") | |
| with gr.Tab("📊 Анализ"): | |
| portfolio_input = gr.Textbox(label="Введите ссылку или portfolioId", placeholder="ea856c1b-...") | |
| analyze_button = gr.Button("🔍 Проанализировать") | |
| output_box = gr.Textbox(label="📈 Результат анализа", lines=15) | |
| analyze_button.click( | |
| fn=analyze_one_portfolio, | |
| inputs=portfolio_input, | |
| outputs=output_box # Textbox | |
| ) | |
| with gr.Tab("⚖️ Сравнение"): | |
| compare_input_1 = gr.Textbox(label="Портфель 1", placeholder="ea856c1b-...") | |
| compare_input_2 = gr.Textbox(label="Портфель 2", placeholder="d52f55cc-...") | |
| compare_button = gr.Button("📊 Сравнить") | |
| compare_output = gr.Textbox(label="📉 Результат сравнения", lines=20) | |
| compare_button.click(fn=compare_analyze, inputs=[compare_input_1, compare_input_2], outputs=compare_output) | |
| with gr.Tab("💬 Диалог"): | |
| chat_input = gr.Textbox(label="Ваш вопрос", placeholder="Что такое TradeLink Passport?") | |
| chat_button = gr.Button("Отправить") | |
| chat_output = gr.Textbox(label="Ответ", lines=8) | |
| chat_button.click(fn=handle_chat_streaming, inputs=chat_input, outputs=chat_output) | |
| with gr.Tab("📋 Таблица метрик"): | |
| metrics_input = gr.Textbox(label="Введите portfolioId", placeholder="ea856c1b-...") | |
| metrics_button = gr.Button("📥 Показать метрики") | |
| metrics_output = gr.Dataframe(label="📊 Метрики портфеля", wrap=True) | |
| metrics_button.click(fn=show_metrics_table, inputs=metrics_input, outputs=metrics_output) | |
| with gr.Tab("📈 AlphaBTC график"): | |
| chart_input = gr.Textbox(label="Введите portfolioId", value="3852a354-e66e-4bc5-97e9-55124e31e687") | |
| chart_button = gr.Button("Построить график") | |
| chart_output = gr.Plot(label="График Alpha") | |
| def build_chart(portfolio_id): | |
| print(f"[DEBUG] Нажата кнопка, portfolio_id = {portfolio_id}") | |
| fig = plot_alpha_btc_chart(portfolio_id) | |
| if fig is None: | |
| raise gr.Error("График не построен: проверьте ID.") | |
| return fig | |
| chart_button.click(fn=build_chart, inputs=chart_input, outputs=chart_output) | |
| demo.mount_api(rest_api_app) | |
| if __name__ == "__main__": | |
| demo.launch() | |