test chatbot
Browse files
app.py
CHANGED
|
@@ -3,6 +3,8 @@ import os
|
|
| 3 |
import datetime
|
| 4 |
import re
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
| 6 |
import globals as g
|
| 7 |
from service.mysql_service import get_companys, insert_company, get_company_by_name
|
| 8 |
from service.chat_service import get_analysis_report, get_stock_price_from_bailian, search_company, search_news, get_invest_suggest, chat_bot
|
|
@@ -14,6 +16,28 @@ from service.three_year_table_tool import build_table_format
|
|
| 14 |
from service.three_year_tool import process_financial_data_with_metadata
|
| 15 |
from service.tool_processor import get_stock_price
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
custom_css = """
|
| 18 |
/* 匹配所有以 gradio-container- 开头的类 */
|
| 19 |
div[class^="gradio-container-"],
|
|
@@ -109,14 +133,20 @@ def get_stock_code_by_company_name(company_name):
|
|
| 109 |
|
| 110 |
# 创建一个简单的函数来获取公司列表
|
| 111 |
def get_company_list_choices():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
try:
|
| 113 |
companies_data = get_companys()
|
|
|
|
| 114 |
if isinstance(companies_data, pd.DataFrame) and not companies_data.empty:
|
| 115 |
choices = [str(row.get('company_name', 'Unknown')) for _, row in companies_data.iterrows()]
|
| 116 |
else:
|
| 117 |
choices = []
|
| 118 |
except:
|
| 119 |
choices = []
|
|
|
|
| 120 |
return gr.update(choices=choices)
|
| 121 |
|
| 122 |
# Sidebar service functions
|
|
@@ -257,6 +287,9 @@ def initialize_companies_map():
|
|
| 257 |
|
| 258 |
# 从数据库获取公司数据
|
| 259 |
companies_data = get_companys()
|
|
|
|
|
|
|
|
|
|
| 260 |
print(f"Companies data from DB: {companies_data}")
|
| 261 |
|
| 262 |
# 如果数据库中有公司数据,则添加到映射中(去重)
|
|
@@ -557,33 +590,45 @@ def create_header():
|
|
| 557 |
with gr.Column(scale=2):
|
| 558 |
gr.Markdown(current_time, elem_classes=["text-sm-top-time"])
|
| 559 |
|
| 560 |
-
def create_company_list():
|
| 561 |
-
"""创建公司列表组件"""
|
| 562 |
-
# 获取公司列表数据
|
| 563 |
-
try:
|
| 564 |
-
companies_data = get_companys()
|
| 565 |
-
if isinstance(companies_data, pd.DataFrame) and not companies_data.empty:
|
| 566 |
-
choices = [str(row.get('company_name', 'Unknown')) for _, row in companies_data.iterrows()]
|
| 567 |
-
else:
|
| 568 |
-
choices = []
|
| 569 |
-
except:
|
| 570 |
-
choices = []
|
| 571 |
-
|
| 572 |
-
# 添加默认公司选项
|
| 573 |
-
if not choices:
|
| 574 |
-
choices = []
|
| 575 |
-
|
| 576 |
-
# 使用Radio组件显示公司列表,不显示标签
|
| 577 |
company_list = gr.Radio(
|
| 578 |
-
choices=
|
| 579 |
label="",
|
| 580 |
interactive=True,
|
| 581 |
elem_classes=["company-list-container"],
|
| 582 |
container=False, # 不显示外部容器边框
|
| 583 |
visible=True
|
| 584 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 585 |
|
| 586 |
-
|
| 587 |
|
| 588 |
def create_company_selector():
|
| 589 |
"""创建公司选择器组件"""
|
|
@@ -722,6 +767,7 @@ def format_financial_metrics(data: dict, prev_data: dict = None) -> list: # pyr
|
|
| 722 |
|
| 723 |
return result
|
| 724 |
|
|
|
|
| 725 |
def create_sidebar():
|
| 726 |
"""创建侧边栏组件"""
|
| 727 |
# 初始化 companies_map
|
|
@@ -732,8 +778,16 @@ def create_sidebar():
|
|
| 732 |
with gr.Group(elem_classes=["card"]):
|
| 733 |
gr.Markdown("### Select Company", elem_classes=["card-title", "left-card-title"])
|
| 734 |
with gr.Column():
|
|
|
|
|
|
|
| 735 |
# 创建公司列表
|
| 736 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 737 |
|
| 738 |
# 创建公司选择器
|
| 739 |
company_input, status_message, company_modal = create_company_selector()
|
|
@@ -1617,19 +1671,18 @@ def create_tab_content(tab_name, company_name):
|
|
| 1617 |
gr.Markdown("Report Preview", elem_classes=["font-medium", "mb-3"])
|
| 1618 |
# 这里将显示报告预览
|
| 1619 |
|
| 1620 |
-
from chatbot.chat_main import create_financial_chatbot
|
| 1621 |
-
|
| 1622 |
def create_chat_panel():
|
| 1623 |
"""创建聊天面板组件"""
|
| 1624 |
with gr.Column(elem_classes=["chat-panel"]):
|
| 1625 |
-
chat_component = create_financial_chatbot()
|
| 1626 |
-
chat_component.render()
|
| 1627 |
# 聊天头部
|
| 1628 |
# with gr.Row(elem_classes=["p-4", "border-b", "border-gray-200", "items-center", "gap-2"]):
|
| 1629 |
# gr.Markdown("🤖", elem_classes=["text-xl", "text-blue-600"])
|
| 1630 |
# gr.Markdown("Financial Assistant", elem_classes=["font-medium"])
|
| 1631 |
|
| 1632 |
# 聊天区域
|
|
|
|
|
|
|
|
|
|
| 1633 |
# chatbot = gr.Chatbot(
|
| 1634 |
# value=[
|
| 1635 |
# {"role": "assistant", "content": "I'm your financial assistant, how can I help you today?"},
|
|
@@ -1650,7 +1703,7 @@ def create_chat_panel():
|
|
| 1650 |
# container=False,
|
| 1651 |
# )
|
| 1652 |
|
| 1653 |
-
# 输入区域
|
| 1654 |
# with gr.Row(elem_classes=["border-t", "border-gray-200", "gap-2"]):
|
| 1655 |
# msg = gr.Textbox(
|
| 1656 |
# placeholder="Ask a financial question...",
|
|
@@ -1737,7 +1790,7 @@ def main():
|
|
| 1737 |
create_metrics_dashboard()
|
| 1738 |
|
| 1739 |
with gr.Row(elem_classes=["main-content-box"]):
|
| 1740 |
-
with gr.Column(scale=
|
| 1741 |
# Tab内容
|
| 1742 |
with gr.Tabs():
|
| 1743 |
with gr.TabItem("Invest Suggest", elem_classes=["tab-item"]):
|
|
@@ -1850,7 +1903,7 @@ def main():
|
|
| 1850 |
)
|
| 1851 |
# with gr.TabItem("Comparison", elem_classes=["tab-item"]):
|
| 1852 |
# create_tab_content("comparison")
|
| 1853 |
-
with gr.Column(scale=
|
| 1854 |
# 聊天面板
|
| 1855 |
create_chat_panel()
|
| 1856 |
|
|
@@ -1859,7 +1912,7 @@ def main():
|
|
| 1859 |
fn=get_company_list_choices,
|
| 1860 |
inputs=[],
|
| 1861 |
outputs=[company_list_component],
|
| 1862 |
-
concurrency_limit=None
|
| 1863 |
)
|
| 1864 |
|
| 1865 |
# 绑定公司选择事件到状态更新
|
|
|
|
| 3 |
import datetime
|
| 4 |
import re
|
| 5 |
import pandas as pd
|
| 6 |
+
from sqlalchemy import true
|
| 7 |
+
from chatbot.chat_main import create_financial_chatbot
|
| 8 |
import globals as g
|
| 9 |
from service.mysql_service import get_companys, insert_company, get_company_by_name
|
| 10 |
from service.chat_service import get_analysis_report, get_stock_price_from_bailian, search_company, search_news, get_invest_suggest, chat_bot
|
|
|
|
| 16 |
from service.three_year_tool import process_financial_data_with_metadata
|
| 17 |
from service.tool_processor import get_stock_price
|
| 18 |
|
| 19 |
+
get_companys_state = True
|
| 20 |
+
# JavaScript代码用于读取和存储数据
|
| 21 |
+
js_code = """
|
| 22 |
+
function handleStorage(operation, key, value) {
|
| 23 |
+
if (operation === 'set') {
|
| 24 |
+
localStorage.setItem(key, value);
|
| 25 |
+
return `已存储: ${key} = ${value}`;
|
| 26 |
+
} else if (operation === 'get') {
|
| 27 |
+
let storedValue = localStorage.getItem(key);
|
| 28 |
+
if (storedValue === null) {
|
| 29 |
+
return `未找到键: ${key}`;
|
| 30 |
+
}
|
| 31 |
+
return `读取到: ${key} = ${storedValue}`;
|
| 32 |
+
} else if (operation === 'clear') {
|
| 33 |
+
localStorage.removeItem(key);
|
| 34 |
+
return `已清除: ${key}`;
|
| 35 |
+
} else if (operation === 'clearAll') {
|
| 36 |
+
localStorage.clear();
|
| 37 |
+
return '已清除所有数据';
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
"""
|
| 41 |
custom_css = """
|
| 42 |
/* 匹配所有以 gradio-container- 开头的类 */
|
| 43 |
div[class^="gradio-container-"],
|
|
|
|
| 133 |
|
| 134 |
# 创建一个简单的函数来获取公司列表
|
| 135 |
def get_company_list_choices():
|
| 136 |
+
choices = []
|
| 137 |
+
print(f"Getting init add company list choices...{get_companys_state}")
|
| 138 |
+
if not get_companys_state:
|
| 139 |
+
return gr.update(choices=choices)
|
| 140 |
try:
|
| 141 |
companies_data = get_companys()
|
| 142 |
+
print(f"Getting init add company list choices...companies_data: {companies_data}")
|
| 143 |
if isinstance(companies_data, pd.DataFrame) and not companies_data.empty:
|
| 144 |
choices = [str(row.get('company_name', 'Unknown')) for _, row in companies_data.iterrows()]
|
| 145 |
else:
|
| 146 |
choices = []
|
| 147 |
except:
|
| 148 |
choices = []
|
| 149 |
+
|
| 150 |
return gr.update(choices=choices)
|
| 151 |
|
| 152 |
# Sidebar service functions
|
|
|
|
| 287 |
|
| 288 |
# 从数据库获取公司数据
|
| 289 |
companies_data = get_companys()
|
| 290 |
+
# companies_data = window.cachedCompanies or []
|
| 291 |
+
|
| 292 |
+
|
| 293 |
print(f"Companies data from DB: {companies_data}")
|
| 294 |
|
| 295 |
# 如果数据库中有公司数据,则添加到映射中(去重)
|
|
|
|
| 590 |
with gr.Column(scale=2):
|
| 591 |
gr.Markdown(current_time, elem_classes=["text-sm-top-time"])
|
| 592 |
|
| 593 |
+
def create_company_list(get_companys_state):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 594 |
company_list = gr.Radio(
|
| 595 |
+
choices=[],
|
| 596 |
label="",
|
| 597 |
interactive=True,
|
| 598 |
elem_classes=["company-list-container"],
|
| 599 |
container=False, # 不显示外部容器边框
|
| 600 |
visible=True
|
| 601 |
+
)
|
| 602 |
+
if (get_companys_state == False):
|
| 603 |
+
return company_list
|
| 604 |
+
else:
|
| 605 |
+
"""创建公司列表组件"""
|
| 606 |
+
# 获取公司列表数据
|
| 607 |
+
try:
|
| 608 |
+
companies_data = get_companys()
|
| 609 |
+
print(f"创建公司列表组件 - Companies data: {companies_data}")
|
| 610 |
+
if isinstance(companies_data, pd.DataFrame) and not companies_data.empty:
|
| 611 |
+
choices = [str(row.get('company_name', 'Unknown')) for _, row in companies_data.iterrows()]
|
| 612 |
+
else:
|
| 613 |
+
choices = []
|
| 614 |
+
except:
|
| 615 |
+
choices = []
|
| 616 |
+
|
| 617 |
+
# 添加默认公司选项
|
| 618 |
+
if not choices:
|
| 619 |
+
choices = []
|
| 620 |
+
|
| 621 |
+
# 使用Radio组件显示公司列表,不显示标签
|
| 622 |
+
company_list = gr.Radio(
|
| 623 |
+
choices=choices,
|
| 624 |
+
label="",
|
| 625 |
+
interactive=True,
|
| 626 |
+
elem_classes=["company-list-container"],
|
| 627 |
+
container=False, # 不显示外部容器边框
|
| 628 |
+
visible=True
|
| 629 |
+
)
|
| 630 |
|
| 631 |
+
return company_list
|
| 632 |
|
| 633 |
def create_company_selector():
|
| 634 |
"""创建公司选择器组件"""
|
|
|
|
| 767 |
|
| 768 |
return result
|
| 769 |
|
| 770 |
+
|
| 771 |
def create_sidebar():
|
| 772 |
"""创建侧边栏组件"""
|
| 773 |
# 初始化 companies_map
|
|
|
|
| 778 |
with gr.Group(elem_classes=["card"]):
|
| 779 |
gr.Markdown("### Select Company", elem_classes=["card-title", "left-card-title"])
|
| 780 |
with gr.Column():
|
| 781 |
+
company_list = create_company_list(get_companys_state)
|
| 782 |
+
|
| 783 |
# 创建公司列表
|
| 784 |
+
# if not get_companys_state:
|
| 785 |
+
# getCompanyFromStorage = gr.Button("读取")
|
| 786 |
+
# getCompanyFromStorage.click(
|
| 787 |
+
# fn=create_company_list(True),
|
| 788 |
+
# inputs=[],
|
| 789 |
+
# outputs=[company_list, status_message]
|
| 790 |
+
# )
|
| 791 |
|
| 792 |
# 创建公司选择器
|
| 793 |
company_input, status_message, company_modal = create_company_selector()
|
|
|
|
| 1671 |
gr.Markdown("Report Preview", elem_classes=["font-medium", "mb-3"])
|
| 1672 |
# 这里将显示报告预览
|
| 1673 |
|
|
|
|
|
|
|
| 1674 |
def create_chat_panel():
|
| 1675 |
"""创建聊天面板组件"""
|
| 1676 |
with gr.Column(elem_classes=["chat-panel"]):
|
|
|
|
|
|
|
| 1677 |
# 聊天头部
|
| 1678 |
# with gr.Row(elem_classes=["p-4", "border-b", "border-gray-200", "items-center", "gap-2"]):
|
| 1679 |
# gr.Markdown("🤖", elem_classes=["text-xl", "text-blue-600"])
|
| 1680 |
# gr.Markdown("Financial Assistant", elem_classes=["font-medium"])
|
| 1681 |
|
| 1682 |
# 聊天区域
|
| 1683 |
+
# 一行代码嵌入!
|
| 1684 |
+
chat_component = create_financial_chatbot()
|
| 1685 |
+
chat_component.render()
|
| 1686 |
# chatbot = gr.Chatbot(
|
| 1687 |
# value=[
|
| 1688 |
# {"role": "assistant", "content": "I'm your financial assistant, how can I help you today?"},
|
|
|
|
| 1703 |
# container=False,
|
| 1704 |
# )
|
| 1705 |
|
| 1706 |
+
# # 输入区域
|
| 1707 |
# with gr.Row(elem_classes=["border-t", "border-gray-200", "gap-2"]):
|
| 1708 |
# msg = gr.Textbox(
|
| 1709 |
# placeholder="Ask a financial question...",
|
|
|
|
| 1790 |
create_metrics_dashboard()
|
| 1791 |
|
| 1792 |
with gr.Row(elem_classes=["main-content-box"]):
|
| 1793 |
+
with gr.Column(scale=8):
|
| 1794 |
# Tab内容
|
| 1795 |
with gr.Tabs():
|
| 1796 |
with gr.TabItem("Invest Suggest", elem_classes=["tab-item"]):
|
|
|
|
| 1903 |
)
|
| 1904 |
# with gr.TabItem("Comparison", elem_classes=["tab-item"]):
|
| 1905 |
# create_tab_content("comparison")
|
| 1906 |
+
with gr.Column(scale=2):
|
| 1907 |
# 聊天面板
|
| 1908 |
create_chat_panel()
|
| 1909 |
|
|
|
|
| 1912 |
fn=get_company_list_choices,
|
| 1913 |
inputs=[],
|
| 1914 |
outputs=[company_list_component],
|
| 1915 |
+
concurrency_limit=None,
|
| 1916 |
)
|
| 1917 |
|
| 1918 |
# 绑定公司选择事件到状态更新
|