Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| from prompts import SYSTEM_PROMPT | |
| from prompts import TRADELINK_CONTEXT | |
| from fetch import extract_portfolio_id | |
| from fetch import fetch_metrics | |
| from openai import OpenAI | |
| from analyzer import analyze_portfolio_streaming | |
| from comparer import compare_portfolio | |
| api_key = os.getenv("featherless") # | |
| client = OpenAI( | |
| base_url="https://api.featherless.ai/v1", | |
| api_key=api_key | |
| ) | |
| # # Сравнение двух портфелей | |
| # def compare_portfolios_streaming(text1: str, text2: str): | |
| # id1 = extract_portfolio_id(text1) | |
| # id2 = extract_portfolio_id(text2) | |
| # if not id1 or not id2: | |
| # yield "❗ Один или оба portfolioId некорректны." | |
| # return | |
| # | |
| # m1 = fetch_metrics(id1) | |
| # m2 = fetch_metrics(id2) | |
| # if not m1 or not m2: | |
| # yield "❗ Не удалось получить метрики одного из портфелей." | |
| # return | |
| # | |
| # m1_text = ", ".join([f"{k}: {v}" for k, v in m1.items()]) | |
| # m2_text = ", ".join([f"{k}: {v}" for k, v in m2.items()]) | |
| # prompt = f"""Сравни два инвестиционных портфеля: | |
| # | |
| # Портфель 1: {m1_text} | |
| # Портфель 2: {m2_text} | |
| # | |
| # Сделай сравнительный анализ на русском языке как финансовый аналитик. Укажи, какой портфель сильнее, в чём риски, где преимущества.""" | |
| # | |
| # response = client.chat.completions.create( | |
| # model="meta-llama/Meta-Llama-3.1-8B-Instruct", | |
| # messages=[ | |
| # {"role": "system", "content": SYSTEM_PROMPT}, | |
| # {"role": "user", "content": prompt} | |
| # ], | |
| # stream=True | |
| # ) | |
| # | |
| # partial = "" | |
| # for chunk in response: | |
| # delta = chunk.choices[0].delta.content | |
| # if delta: | |
| # partial += delta | |
| # yield partial | |
| 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) | |
| if __name__ == "__main__": | |
| demo.launch() | |