Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from services.llm_client import llm_service | |
| from core.analyzer import PortfolioAnalyzer | |
| from core.comparer import PortfolioComparer | |
| from core.chat import ChatAssistant | |
| from core.metrics import show_metrics_table | |
| from core.visualization import build_alpha_chart | |
| MODEL_NAME = "meta-llama/Meta-Llama-3.1-8B-Instruct" | |
| analyzer = PortfolioAnalyzer(llm_service, MODEL_NAME) | |
| comparer = PortfolioComparer(llm_service, MODEL_NAME) | |
| chatbot = ChatAssistant(llm_service, MODEL_NAME) | |
| # === Text dictionaries for RU/ENG === | |
| LANG_TEXT = { | |
| "ENG": { | |
| "title": "🧠 Investment Portfolio Analysis", | |
| "tabs": ["📊 Analysis", "⚖️ Comparison", "💬 Chat2", "📋 Metrics Table", "📈 AlphaBTC Chart"], | |
| "labels": { | |
| "analyze_input": "Enter portfolioId or link", | |
| "analyze_button": "🔍 Analyze", | |
| "analyze_output": "Analysis Result", | |
| "compare_1": "Portfolio A", | |
| "compare_2": "Portfolio B", | |
| "compare_button": "📈 Compare", | |
| "compare_output": "Comparison Result", | |
| "chat_input": "Ask your question", | |
| "chat_button": "Send", | |
| "chat_output": "Response", | |
| "metrics_input": "Enter portfolioId", | |
| "metrics_button": "Show Metrics", | |
| "metrics_output": "Portfolio Metrics", | |
| "chart_input": "Enter portfolioId", | |
| "chart_button": "Plot Chart", | |
| "chart_output": "Alpha to BTC Chart", | |
| } | |
| }, | |
| "RU": { | |
| "title": "🧠 Анализ инвестиционных портфелей", | |
| "tabs": ["📊 Анализ", "⚖️ Сравнение", "💬 Диалог", "📋 Таблица метрик", "📈 График AlphaBTC"], | |
| "labels": { | |
| "analyze_input": "Введите ссылку или portfolioId", | |
| "analyze_button": "🔍 Проанализировать", | |
| "analyze_output": "📈 Результат анализа", | |
| "compare_1": "Портфель 1", | |
| "compare_2": "Портфель 2", | |
| "compare_button": "📊 Сравнить", | |
| "compare_output": "📉 Результат сравнения", | |
| "chat_input": "Ваш вопрос", | |
| "chat_button": "Отправить", | |
| "chat_output": "Ответ", | |
| "metrics_input": "Введите portfolioId", | |
| "metrics_button": "📥 Показать метрики", | |
| "metrics_output": "📊 Метрики портфеля", | |
| "chart_input": "Введите portfolioId", | |
| "chart_button": "Построить график", | |
| "chart_output": "График Alpha к BTC", | |
| } | |
| } | |
| } | |
| def build_interface(lang="ENG"): | |
| text = LANG_TEXT[lang] | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| lang_state = gr.State(lang) | |
| # Header | |
| gr.Markdown(f"## {text['title']}") | |
| # Language toggle | |
| lang_button = gr.Button("🌐 Switch Language (RU / ENG)") | |
| def switch_language(current_lang): | |
| return "RU" if current_lang == "ENG" else "ENG" | |
| lang_button.click(fn=switch_language, inputs=lang_state, outputs=lang_state).then( | |
| fn=lambda l: gr.update(visible=False), inputs=lang_state, outputs=None | |
| ) | |
| with gr.Tabs(): | |
| # --- Tab 1: Analysis --- | |
| with gr.TabItem(text["tabs"][0]): | |
| inp = gr.Textbox(label=text["labels"]["analyze_input"]) | |
| btn = gr.Button(text["labels"]["analyze_button"]) | |
| out = gr.Textbox(label=text["labels"]["analyze_output"], lines=15) | |
| btn.click(fn=analyzer.run, inputs=inp, outputs=out) | |
| # --- Tab 2: Comparison --- | |
| with gr.TabItem(text["tabs"][1]): | |
| a = gr.Textbox(label=text["labels"]["compare_1"]) | |
| b = gr.Textbox(label=text["labels"]["compare_2"]) | |
| cmp_btn = gr.Button(text["labels"]["compare_button"]) | |
| cmp_out = gr.Textbox(label=text["labels"]["compare_output"], lines=20) | |
| cmp_btn.click(fn=comparer.run, inputs=[a, b], outputs=cmp_out) | |
| # --- Tab 3: Chat --- | |
| with gr.TabItem(text["tabs"][2]): | |
| chat_in = gr.Textbox(label=text["labels"]["chat_input"]) | |
| chat_btn = gr.Button(text["labels"]["chat_button"]) | |
| chat_out = gr.Textbox(label=text["labels"]["chat_output"], lines=8) | |
| chat_btn.click(fn=chatbot.run, inputs=chat_in, outputs=chat_out) | |
| # --- Tab 4: Metrics Table --- | |
| with gr.TabItem(text["tabs"][3]): | |
| mid = gr.Textbox(label=text["labels"]["metrics_input"]) | |
| mbtn = gr.Button(text["labels"]["metrics_button"]) | |
| mout = gr.Dataframe(label=text["labels"]["metrics_output"], wrap=True) | |
| mbtn.click(fn=show_metrics_table, inputs=mid, outputs=mout) | |
| # --- Tab 5: AlphaBTC Chart --- | |
| with gr.TabItem(text["tabs"][4]): | |
| cid = gr.Textbox(label=text["labels"]["chart_input"], value="3852a354-e66e-4bc5-97e9-55124e31e687") | |
| cbtn = gr.Button(text["labels"]["chart_button"]) | |
| cout = gr.Plot(label=text["labels"]["chart_output"]) | |
| cbtn.click(fn=build_alpha_chart, inputs=cid, outputs=cout) | |
| return demo | |
| # === Default launch in English === | |
| if __name__ == "__main__": | |
| demo = build_interface(lang="ENG") | |
| demo.launch() | |