Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
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][
|
|
|
|
|
|
|
| 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
|
| 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 -
|
| 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 |
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="
|
| 134 |
container=False,
|
| 135 |
)
|
| 136 |
-
submit_button = gr.Button("
|
| 137 |
|
| 138 |
gr.Examples(
|
| 139 |
examples=[
|
| 140 |
-
"
|
| 141 |
-
"
|
| 142 |
-
"
|
| 143 |
-
"
|
| 144 |
],
|
| 145 |
inputs=text_input,
|
|
|
|
| 146 |
)
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 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. 启动应用
|