aifeifei798 commited on
Commit
51923c8
·
verified ·
1 Parent(s): ee66e7b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -63
app.py CHANGED
@@ -18,11 +18,7 @@ try:
18
  from core.agent import SmartAIAgent
19
 
20
  print("核心模块导入成功。")
21
- except ImportError as e:
22
- print(f"导入模块时出错: {e}")
23
- raise
24
 
25
- try:
26
  registered_tools, tool_recommender = initialize_system()
27
  print("系统数据库和工具推荐器初始化完成。")
28
  agent = SmartAIAgent(
@@ -50,7 +46,9 @@ def handle_user_message(user_input, history):
50
 
51
  def generate_bot_response(history):
52
  if agent is None:
53
- history[-1][1] = "抱歉,AI助理系统初始化失败,无法提供服务。"
 
 
54
  yield history
55
  return
56
 
@@ -65,7 +63,7 @@ def generate_bot_response(history):
65
  yield history
66
  time.sleep(0.01)
67
  except Exception as e:
68
- error_message = f"\n\n**抱歉,处理您的请求时发生了错误:**\n```\n{type(e).__name__}: {str(e)}\n```"
69
  history[-1][1] += error_message
70
  import traceback
71
 
@@ -76,8 +74,6 @@ def generate_bot_response(history):
76
  # ------------------------------------------------------------------
77
  # 4. 创建 Gradio 界面
78
  # ------------------------------------------------------------------
79
-
80
- # 自定义CSS来美化界面
81
  custom_css = """
82
  #chatbot .message-bubble-content { color: #000000 !important; }
83
  #chatbot code { background-color: #f0f0f0; border-radius: 4px; padding: 2px 4px; color: #c7254e; }
@@ -90,99 +86,87 @@ with gr.Blocks(
90
  css=custom_css,
91
  title="FeiMatrix Synapse",
92
  ) as demo:
 
 
93
  gr.Markdown(
94
  """
95
- # 🚀 FeiMatrix Synapse - 智能AI助理
96
-
97
  ---
98
-
99
- ### 核心功能与理念
100
- **FeiMatrix Synapse** 是一个高级AI助理的**技术概念验证 (Proof of Concept)**。
101
- 它的核心目标是演示一套能够**自主理解、规划并使用外部工具**来解决问题的AI工作流。
102
-
103
- ### 演示流程解析
104
- 当你提问时,你可以清晰地观察到AI的每一步“思考”过程:
105
-
106
- 1. **🤔 分析问题**: AI首先理解你的自然语言指令。
107
- 2. **🔍 推荐工具**: 系统从内部的“工具库”中,通过向量相似度搜索,推荐出最可能解决问题的几个工具。
108
- 3. **🧠 AI决策**: AI大脑(Gemini模型)在推荐的工具中做出最终选择,并从你的问题中提取执行所需的参数。
109
- 4. **⚙️ 执行工具**: 系统调用相应的Python函数(如API查询、网页抓取),获取外部的实时信息。
110
- 5. **✍️ 生成答案**: AI将工具返回的数据,结合你的原始问题,总结并生成最终的自然语言回答。
111
-
112
  ---
113
 
114
- ### ⚠️ **重要声明**
115
- - **这是一个技术流程演示项目,并非生产级应用。**
116
- - **所有工具(如股票、新闻)返回的数据均为【模拟数据】或【非实时数据】,结果一定不正确,请勿用作任何真实决策的依据。**
117
- - **本项目的重点在于展示AI的“思考链”和“工具调用”能力。**
118
  """
119
  )
120
 
121
  chatbot = gr.Chatbot(
122
  [],
123
  elem_id="chatbot",
124
- label="聊天窗口",
125
  height=650,
126
- avatar_images=(None, "assets/web-app-manifest-192x192.png")
127
  )
128
 
129
  with gr.Row():
130
  text_input = gr.Textbox(
131
  scale=4,
132
  show_label=False,
133
- placeholder="例如: '苹果公司(AAPL)今天的股价是多少?' '关于AI的最新进展有什么新闻?'",
134
  container=False,
135
  )
136
- submit_button = gr.Button("发送", variant="primary", scale=1, min_width=150)
137
 
138
  gr.Examples(
139
  examples=[
140
- "苹果公司(AAPL)的股价是多少?",
141
- "关于AI驱动的药物发现有什么最新新闻?",
142
- "你好,你能做什么?",
143
- "Python写一个快速排序算法",
144
  ],
145
  inputs=text_input,
 
146
  )
147
 
148
- # ------------------- 核心新增在这里! -------------------
149
- # 在界面底部添加一个页脚,写上我们的“诞生铭牌”
150
- with gr.Row():
151
- gr.Markdown(
152
- """
153
- ---
154
- <div class="footer">
155
- <p><strong>FeiMatrix Synapse v1.0</strong></p>
156
- <p>本项目由 <strong>FeiMatrix</strong> 提出核心需求并负责项目调试与部署。</p>
157
- <p>系统架构、核心程序及界面由 <strong>Gemini 2.5 Pro (AI)</strong> 协作完成。</p>
158
- </div>
159
- """,
160
- elem_classes="footer",
161
- )
162
- # ----------------------------------------------------
163
 
164
- # 定义事件的触发流程
165
  submit_event = text_input.submit(
166
  fn=handle_user_message,
167
  inputs=[text_input, chatbot],
168
  outputs=[text_input, chatbot],
169
  queue=False,
170
- ).then(
171
- fn=generate_bot_response,
172
- inputs=[chatbot],
173
- outputs=[chatbot],
174
- )
175
-
176
  submit_button.click(
177
  fn=handle_user_message,
178
  inputs=[text_input, chatbot],
179
  outputs=[text_input, chatbot],
180
  queue=False,
181
- ).then(
182
- fn=generate_bot_response,
183
- inputs=[chatbot],
184
- outputs=[chatbot],
185
- )
186
 
187
  # ------------------------------------------------------------------
188
  # 5. 启动应用
 
18
  from core.agent import SmartAIAgent
19
 
20
  print("核心模块导入成功。")
 
 
 
21
 
 
22
  registered_tools, tool_recommender = initialize_system()
23
  print("系统数据库和工具推荐器初始化完成。")
24
  agent = SmartAIAgent(
 
46
 
47
  def generate_bot_response(history):
48
  if agent is None:
49
+ history[-1][
50
+ 1
51
+ ] = "Sorry, the AI Assistant system failed to initialize and is currently unavailable."
52
  yield history
53
  return
54
 
 
63
  yield history
64
  time.sleep(0.01)
65
  except Exception as e:
66
+ error_message = f"\n\n**Sorry, an error occurred while processing your request:**\n```\n{type(e).__name__}: {str(e)}\n```"
67
  history[-1][1] += error_message
68
  import traceback
69
 
 
74
  # ------------------------------------------------------------------
75
  # 4. 创建 Gradio 界面
76
  # ------------------------------------------------------------------
 
 
77
  custom_css = """
78
  #chatbot .message-bubble-content { color: #000000 !important; }
79
  #chatbot code { background-color: #f0f0f0; border-radius: 4px; padding: 2px 4px; color: #c7254e; }
 
86
  css=custom_css,
87
  title="FeiMatrix Synapse",
88
  ) as demo:
89
+
90
+ # --- 界面文本已全部修改为英文 ---
91
  gr.Markdown(
92
  """
93
+ # 🚀 FeiMatrix Synapse - Intelligent AI Assistant
 
94
  ---
95
+ ### Core Concept & Philosophy
96
+ **FeiMatrix Synapse** is a **Proof of Concept** for an advanced AI assistant.
97
+ Its core objective is to demonstrate an AI workflow made to **autonomously understand, plan, and utilize external tools** to solve problems.
98
+
99
+ ### The Demo Flow Explained
100
+ When you ask a question, you can clearly observe each step of the AI's "thought process":
101
+
102
+ 1. **🤔 Analyzing the Problem**: The AI first understands your natural language command.
103
+ 2. **🔍 Recommending Tools**: The system searches its internal "Tool Library" using vector similarity to recommend the most relevant tools.
104
+ 3. **🧠 AI Decision-Making**: The AI Brain (Gemini Model) makes the final choice from the recommended tools and extracts the necessary parameters from your query.
105
+ 4. **⚙️ Executing the Tool**: The system invokes the corresponding Python function (e.g., an API call or web scraper) to fetch external, real-time information.
106
+ 5. **✍️ Generating the Answer**: The AI synthesizes the data returned by the tool with your original question to generate a final, natural-language response.
107
+
 
108
  ---
109
 
110
+ ### ⚠️ Important Disclaimer
111
+ - **This is a technical demonstration, not a production-ready application.**
112
+ - **All data returned by the tools (e.g., stock prices, news) is for **demonstration purposes only** using simulated or non-real-time data. Do not use it for any real-world decisions.**
113
+ - **The primary focus of this project is to showcase the AI's "chain of thought" and "tool-using" capabilities.**
114
  """
115
  )
116
 
117
  chatbot = gr.Chatbot(
118
  [],
119
  elem_id="chatbot",
120
+ label="Chat Window",
121
  height=650,
122
+ avatar_images=(None, "assets/web-app-manifest-192x192.png"),
123
  )
124
 
125
  with gr.Row():
126
  text_input = gr.Textbox(
127
  scale=4,
128
  show_label=False,
129
+ placeholder="Ask a question, e.g., 'What is the stock price of Apple (AAPL)?' or 'What's the latest news on AI-driven drug discovery?'",
130
  container=False,
131
  )
132
+ submit_button = gr.Button("Send", variant="primary", scale=1, min_width=150)
133
 
134
  gr.Examples(
135
  examples=[
136
+ "What is the stock price of Apple (AAPL)?",
137
+ "What is the latest news about AI-driven drug discovery?",
138
+ "Hello, what can you do?",
139
+ "Write a quicksort algorithm in Python",
140
  ],
141
  inputs=text_input,
142
+ label="Examples",
143
  )
144
 
145
+ gr.Markdown(
146
+ """
147
+ ---
148
+ <div class="footer">
149
+ <p><strong>FeiMatrix Synapse v1.0</strong></p>
150
+ <p>This project was conceived and directed by <strong>FeiMatrix</strong>, who also led the debugging and deployment.</p>
151
+ <p>The system architecture, core program, and interface were co-developed with <strong>Gemini 2.5 Pro (AI)</strong>.</p>
152
+ </div>
153
+ """,
154
+ elem_classes="footer",
155
+ )
 
 
 
 
156
 
157
+ # --- 对话事件的触发流程 (保持不变) ---
158
  submit_event = text_input.submit(
159
  fn=handle_user_message,
160
  inputs=[text_input, chatbot],
161
  outputs=[text_input, chatbot],
162
  queue=False,
163
+ ).then(fn=generate_bot_response, inputs=[chatbot], outputs=[chatbot])
 
 
 
 
 
164
  submit_button.click(
165
  fn=handle_user_message,
166
  inputs=[text_input, chatbot],
167
  outputs=[text_input, chatbot],
168
  queue=False,
169
+ ).then(fn=generate_bot_response, inputs=[chatbot], outputs=[chatbot])
 
 
 
 
170
 
171
  # ------------------------------------------------------------------
172
  # 5. 启动应用