gradio_app.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. """
  2. Gradio前端界面 - 多智能体天气穿衣建议系统
  3. 在本地8899端口提供服务
  4. """
  5. import gradio as gr
  6. import sys
  7. import os
  8. # 添加当前目录到Python路径
  9. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  10. def get_weather_and_fashion_advice(city_name):
  11. """
  12. 获取指定城市的天气和穿衣建议
  13. :param city_name: 城市名称
  14. :return: 完整的响应文本
  15. """
  16. try:
  17. # 导入多智能体协调器
  18. from multi_agent_coordinator import MultiAgentCoordinator
  19. # 创建协调器实例
  20. coordinator = MultiAgentCoordinator()
  21. # 构建查询
  22. query = f"查询{city_name}的天气并给出穿衣建议"
  23. # 处理查询
  24. result = coordinator.process_query(query)
  25. # 返回完整的响应文本
  26. response_text = f"""
  27. 🏙️ **查询城市**: {city_name}
  28. 🔍 **查询内容**: {query}
  29. 📊 **多智能体协作结果**:
  30. {result}
  31. 💡 **系统说明**:
  32. - 本系统使用多智能体协作架构
  33. - 天气查询智能体负责获取天气数据
  34. - 穿衣建议智能体基于天气数据生成专业建议
  35. - 协调器负责智能体间的通信和任务分配
  36. """
  37. return response_text
  38. except Exception as e:
  39. # 如果多智能体系统不可用,使用简化版本
  40. try:
  41. from simple_multi_agent import get_real_weather, get_llm_fashion_advice
  42. # 获取天气信息
  43. weather_info, weather_details = get_real_weather(city_name)
  44. # 获取LLM穿衣建议
  45. fashion_advice = get_llm_fashion_advice(weather_details, city_name)
  46. response_text = f"""
  47. 🏙️ **查询城市**: {city_name}
  48. 📊 **天气信息**:
  49. {weather_info}
  50. 🤖 **LLM智能穿衣建议**:
  51. {fashion_advice}
  52. 💡 **系统说明**:
  53. - 使用简化版多智能体系统
  54. - 基于真实天气数据生成建议
  55. - LLM智能处理穿衣建议
  56. """
  57. return response_text
  58. except Exception as e2:
  59. # 如果所有方法都失败,返回错误信息
  60. error_text = f"""
  61. ❌ **系统错误**: 无法获取{city_name}的天气和穿衣建议
  62. **错误信息**:
  63. - 主要错误: {str(e)}
  64. - 备用错误: {str(e2)}
  65. 💡 **解决方案**:
  66. 1. 检查网络连接
  67. 2. 确保天气API服务可用
  68. 3. 检查系统依赖是否完整安装
  69. 🔧 **备用建议**:
  70. 您可以尝试以下城市:北京、上海、广州、深圳、杭州、成都等
  71. """
  72. return error_text
  73. def create_gradio_interface():
  74. """创建Gradio界面"""
  75. # 界面描述
  76. description = """
  77. # 🌤️ 多智能体天气穿衣建议系统
  78. 输入您想查询的城市名称,系统将为您提供:
  79. - 📊 实时天气信息
  80. - 🤖 LLM智能穿衣建议
  81. - 💡 专业穿搭指导
  82. **支持功能**:
  83. - 多智能体协作处理
  84. - 真实天气数据查询
  85. - AI智能穿衣建议
  86. - 多轮对话支持
  87. """
  88. # 创建界面
  89. with gr.Blocks(
  90. title="多智能体天气穿衣建议系统",
  91. theme=gr.themes.Soft(),
  92. css="""
  93. .gradio-container {
  94. max-width: 900px !important;
  95. }
  96. .output-text {
  97. font-size: 14px;
  98. line-height: 1.6;
  99. }
  100. """
  101. ) as demo:
  102. gr.Markdown(description)
  103. with gr.Row():
  104. with gr.Column(scale=1):
  105. city_input = gr.Textbox(
  106. label="🌍 请输入城市名称",
  107. placeholder="例如:北京、上海、广州、深圳...",
  108. info="支持中文和英文城市名"
  109. )
  110. submit_btn = gr.Button(
  111. "🚀 获取穿衣建议",
  112. variant="primary",
  113. size="lg"
  114. )
  115. clear_btn = gr.Button("🗑️ 清空", variant="secondary")
  116. with gr.Column(scale=2):
  117. output_text = gr.Textbox(
  118. label="📋 完整响应结果",
  119. lines=20,
  120. max_lines=30,
  121. show_copy_button=True,
  122. elem_classes="output-text"
  123. )
  124. # 示例城市
  125. examples = gr.Examples(
  126. examples=[["beijing"], ["tokoy"], ["london"], ["new york"], ["paris"], ["seoul"], ["bangkok"], ["harbin"]],
  127. inputs=city_input,
  128. label="💡 点击示例快速体验"
  129. )
  130. # 按钮事件
  131. submit_btn.click(
  132. fn=get_weather_and_fashion_advice,
  133. inputs=city_input,
  134. outputs=output_text
  135. )
  136. clear_btn.click(
  137. fn=lambda: "",
  138. inputs=[],
  139. outputs=output_text
  140. )
  141. # 回车键提交
  142. city_input.submit(
  143. fn=get_weather_and_fashion_advice,
  144. inputs=city_input,
  145. outputs=output_text
  146. )
  147. # 页脚信息
  148. gr.Markdown("""
  149. ---
  150. ### 🔧 系统信息
  151. - **版本**: v1.0.0
  152. - **架构**: 多智能体协作系统
  153. - **技术栈**: Python + Gradio + 多智能体框架
  154. - **端口**: 8899
  155. ### 📖 使用说明
  156. 1. 在左侧输入框输入城市名称
  157. 2. 点击"获取穿衣建议"按钮或按回车键
  158. 3. 查看右侧的完整响应结果
  159. 4. 可以点击示例快速体验不同城市
  160. ### 🎯 功能特点
  161. - 🌤️ 实时天气数据查询
  162. - 🤖 AI智能穿衣建议
  163. - 🔄 多智能体协作处理
  164. - 📱 响应式Web界面
  165. - 🎨 美观的用户体验
  166. """)
  167. return demo
  168. def main():
  169. """启动Gradio应用"""
  170. print("🚀 启动多智能体天气穿衣建议系统 - Gradio前端")
  171. print("🌐 服务地址: http://localhost:8899")
  172. print("⏳ 正在启动服务...")
  173. # 创建界面
  174. demo = create_gradio_interface()
  175. # 启动服务
  176. demo.launch(
  177. server_name="0.0.0.0",
  178. server_port=8899,
  179. share=False,
  180. show_error=True,
  181. debug=True,
  182. quiet=False
  183. )
  184. if __name__ == "__main__":
  185. main()