Spaces:
Sleeping
Sleeping
File size: 4,404 Bytes
ab0a6c9 d0db547 7f2f713 08babaa e63adc7 8caee6c c54262a 00ddeb7 8b21f63 c30b7eb d0db547 8370475 f2b8fdc d97fa05 d02533f a4c20c7 d02533f a928dd2 d2b7cd0 d02533f 5e3de11 00ddeb7 d97fa05 a928dd2 d97fa05 a928dd2 984eb4a a928dd2 984eb4a a928dd2 d97fa05 a928dd2 d97fa05 a928dd2 d97fa05 a928dd2 d02533f a928dd2 06417df 8caee6c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
import gradio as gr
import os
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-...")
output_box = gr.Textbox(label="📈 Результат анализа", lines=15)
analyze_button = gr.Button("🔍 Проанализировать")
# stream=True включается через .stream()
analyze_button.click(
fn=analyze_one_portfolio,
inputs=portfolio_input,
outputs=output_box
).stream = True
# ⚖️ СРАВНЕНИЕ
with gr.Tab("⚖️ Сравнение"):
compare_input_1 = gr.Textbox(label="Портфель 1", placeholder="ea856c1b-...")
compare_input_2 = gr.Textbox(label="Портфель 2", placeholder="d52f55cc-...")
compare_output = gr.Textbox(label="📉 Результат сравнения", lines=20)
compare_button = gr.Button("📊 Сравнить")
compare_button.click(
fn=compare_analyze,
inputs=[compare_input_1, compare_input_2],
outputs=compare_output
).stream=True
# 💬 ДИАЛОГ
with gr.Tab("💬 Диалог"):
chat_input = gr.Textbox(label="Ваш вопрос", placeholder="Что такое TradeLink Passport?")
chat_output = gr.Textbox(label="Ответ", lines=8)
chat_button = gr.Button("Отправить")
chat_button.click(
fn=handle_chat_streaming,
inputs=chat_input,
outputs=chat_output
).stream = True
if __name__ == "__main__":
demo.launch()
|