import streamlit as st
import time
from config import config
from agents import code_explainer, bug_fixer
from utils import detect_language
# 设置页面配置
st.set_page_config(
page_title="代码解释与修复助手",
page_icon="🤖",
layout="wide",
initial_sidebar_state="expanded"
)
# 自定义CSS样式
st.markdown("""
""", unsafe_allow_html=True)
# 页面标题和描述
st.title("🤖 代码解释与修复助手")
st.markdown("""
输入您的代码,选择功能,AI将为您提供专业的代码解释或修复建议
""", unsafe_allow_html=True)
# 创建两列布局
col1, col2 = st.columns([1, 1], gap="large")
# 左侧:输入区域
with col1:
with st.container():
st.subheader("📝 代码输入")
# 代码输入框
code_input = st.text_area(
"请输入您需要分析的代码:",
height=300,
placeholder="在此输入您的代码...\n例如:\ndef hello():\n print('Hello World')",
label_visibility="collapsed"
)
# 语言选择
language = st.selectbox(
"编程语言:",
options=[(lang.capitalize(), lang) for lang in config.SUPPORTED_LANGUAGES],
format_func=lambda x: x[0],
key="language"
)
# 任务类型选择
task_type = st.selectbox(
"任务类型:",
options=[("代码解释", "explain"), ("代码修复", "fix")],
format_func=lambda x: x[0],
key="task_type"
)
# 解释详细程度(仅在代码解释时显示)
depth = None
if task_type[1] == "explain":
# 使用简单的字符串选项,完全避免元组带来的状态管理问题
depth_options = ["基础", "详细", "全面"]
depth_values = {"基础": "basic", "详细": "detailed", "全面": "comprehensive"}
# 设置默认值为"详细"
depth_str = st.select_slider(
"解释详细程度:",
options=depth_options,
value="详细",
key="depth_str"
)
# 构建需要的返回值格式
depth = (depth_str, depth_values[depth_str])
# 开始分析按钮
start_button = st.button(
"🚀 开始分析",
type="primary",
use_container_width=True
)
# 右侧:结果区域
with col2:
with st.container():
st.subheader("📊 分析结果")
# 结果显示区域
result_placeholder = st.empty()
# 初始状态
with result_placeholder.container():
st.markdown("""
💡
输入代码并点击"开始分析"按钮,获取AI分析结果
""", unsafe_allow_html=True)
# 处理分析请求
if start_button:
# 验证输入
if not code_input.strip():
with result_placeholder.container():
st.error("请输入代码后再进行分析!")
else:
# 显示加载状态
with result_placeholder.container():
with st.spinner("🤖 AI正在分析代码...请稍候"):
# 添加视觉反馈
loading_bar = st.progress(0)
for i in range(100):
time.sleep(0.01) # 模拟进度
loading_bar.progress(i + 1)
try:
# 调用相应的处理函数
if task_type[1] == "explain":
# 代码解释
result = code_explainer.explain(
code=code_input,
language=language[1],
depth=depth[1]
)
# 显示解释结果
with result_placeholder.container():
st.success("✅ 代码解释完成!")
st.markdown("""
""".format(result), unsafe_allow_html=True)
else:
# 代码修复
result = bug_fixer.fix(
code=code_input,
language=language[1]
)
# 显示修复结果
with result_placeholder.container():
st.success("✅ 代码修复完成!")
# 显示问题分析
st.markdown("""
🔍 问题分析
""", unsafe_allow_html=True)
# 解析并显示问题
if hasattr(result, 'problems_found'):
for problem in result.problems_found:
st.markdown(f"- {problem}")
st.markdown("""
""", unsafe_allow_html=True)
# 显示修复方案
st.markdown("""
🔧 修复方案
""", unsafe_allow_html=True)
if hasattr(result, 'fixed_code'):
st.code(result.fixed_code, language=language[1])
# 显示修复说明
if hasattr(result, 'explanation'):
st.markdown("""
📝 修复说明
{}
""".format(result.explanation), unsafe_allow_html=True)
except Exception as e:
# 显示错误信息
with result_placeholder.container():
st.error("❌ 分析失败!")
st.markdown("""
""".format(str(e)), unsafe_allow_html=True)
# 页脚信息
st.markdown("""
""", unsafe_allow_html=True)