Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| from prompts import TRADELINK_CONTEXT | |
| 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 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() | |