Spaces:
Running

Dzung Doan commited on
Commit
a94b3eb
Β·
1 Parent(s): 1d00e1d

enable showing of mcp prompts

Browse files
Files changed (1) hide show
  1. app.py +116 -22
app.py CHANGED
@@ -32,31 +32,125 @@ ENVIRONMENT = os.getenv("ENVIRONMENT", "dev").lower()
32
  AGENT_URL = ENV_URLS.get(ENVIRONMENT, {}).get("AGENT_URL") or os.getenv("AGENT_URL", "http://localhost:8001")
33
  MCP_SERVER_URL = ENV_URLS.get(ENVIRONMENT, {}).get("MCP_SERVER_URL") or os.getenv("MCP_SERVER_URL", "http://localhost:8000")
34
 
35
- st.set_page_config(page_title="World Cup AI Assistant", page_icon="πŸ†")
 
 
 
 
 
36
  st.title("πŸ† World Cup AI Assistant")
37
  st.write(f"**Environment:** `{ENVIRONMENT}` \nAsk any question about World Cup history and get an AI-powered answer!")
38
 
39
  if "chat_history" not in st.session_state:
40
  st.session_state.chat_history = []
41
 
42
- user_input = st.text_area("Your question:", key="user_input", height=100, placeholder="Type your question here...")
43
- if st.button("Ask") and user_input.strip():
44
- with st.spinner("Thinking..."):
45
- try:
46
- response = requests.post(
47
- f"{AGENT_URL}/ask",
48
- json={"question": user_input},
49
- timeout=15
50
- )
51
- response.raise_for_status()
52
- answer = response.json().get("answer", "No answer received.")
53
- except Exception as e:
54
- answer = f"Error: {e}"
55
- st.session_state.chat_history.append((user_input, answer))
56
-
57
- if st.session_state.chat_history:
58
- st.subheader("Chat History")
59
- for q, a in reversed(st.session_state.chat_history):
60
- st.markdown(f"**You:** {q}")
61
- st.markdown(f"**AI:** {a}")
62
- st.markdown("---")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  AGENT_URL = ENV_URLS.get(ENVIRONMENT, {}).get("AGENT_URL") or os.getenv("AGENT_URL", "http://localhost:8001")
33
  MCP_SERVER_URL = ENV_URLS.get(ENVIRONMENT, {}).get("MCP_SERVER_URL") or os.getenv("MCP_SERVER_URL", "http://localhost:8000")
34
 
35
+ st.set_page_config(
36
+ page_title="World Cup AI Assistant",
37
+ page_icon="πŸ†",
38
+ layout="wide", # Use full width
39
+ initial_sidebar_state="collapsed" # Hide sidebar to save space
40
+ )
41
  st.title("πŸ† World Cup AI Assistant")
42
  st.write(f"**Environment:** `{ENVIRONMENT}` \nAsk any question about World Cup history and get an AI-powered answer!")
43
 
44
  if "chat_history" not in st.session_state:
45
  st.session_state.chat_history = []
46
 
47
+ # Custom CSS for prompt cards
48
+ st.markdown("""
49
+ <style>
50
+ .prompt-card {
51
+ background-color: #f8f9fa;
52
+ border: 1px solid #e9ecef;
53
+ border-radius: 8px;
54
+ padding: 12px;
55
+ margin: 6px 0;
56
+ transition: all 0.2s ease;
57
+ }
58
+ .prompt-card:hover {
59
+ background-color: #e9ecef;
60
+ border-color: #dee2e6;
61
+ }
62
+ .prompt-title {
63
+ font-weight: bold;
64
+ color: #495057;
65
+ margin-bottom: 3px;
66
+ font-size: 0.95em;
67
+ }
68
+ .prompt-description {
69
+ color: #6c757d;
70
+ font-size: 0.8em;
71
+ margin-bottom: 6px;
72
+ line-height: 1.2;
73
+ }
74
+ .prompt-template {
75
+ color: #007bff;
76
+ font-size: 0.8em;
77
+ background-color: #f1f3f4;
78
+ padding: 4px 6px;
79
+ border-radius: 4px;
80
+ margin: 2px 0;
81
+ border-left: 3px solid #007bff;
82
+ line-height: 1.3;
83
+ }
84
+ .prompt-template-placeholder {
85
+ color: #dc3545;
86
+ font-weight: bold;
87
+ }
88
+ </style>
89
+ """, unsafe_allow_html=True)
90
+
91
+ # Fetch available prompts from MCP server
92
+ @st.cache_data(ttl=300) # Cache for 5 minutes
93
+ def get_available_prompts():
94
+ """Fetch available prompts from MCP server"""
95
+ try:
96
+ response = requests.get(f"{MCP_SERVER_URL}/prompts", timeout=5)
97
+ if response.status_code == 200:
98
+ return response.json().get("prompts", [])
99
+ else:
100
+ return []
101
+ except Exception as e:
102
+ st.warning(f"Could not fetch prompts: {e}")
103
+ return []
104
+
105
+ # Get prompts
106
+ prompts = get_available_prompts()
107
+
108
+ # Main layout with input and prompts side by side
109
+ col1, col2 = st.columns([2, 1]) # 2:1 ratio for better space utilization in wide layout
110
+
111
+ with col1:
112
+ # Main input area
113
+ user_input = st.text_area("Your question:", key="user_input", height=100, placeholder="Type your question here...")
114
+ if st.button("Ask") and user_input.strip():
115
+ with st.spinner("Thinking..."):
116
+ try:
117
+ response = requests.post(
118
+ f"{AGENT_URL}/ask",
119
+ json={"question": user_input},
120
+ timeout=15
121
+ )
122
+ response.raise_for_status()
123
+ answer = response.json().get("answer", "No answer received.")
124
+ except Exception as e:
125
+ answer = f"Error: {e}"
126
+ st.session_state.chat_history.append((user_input, answer))
127
+
128
+ # Chat history - moved inside the left column
129
+ if st.session_state.chat_history:
130
+ st.subheader("Chat History")
131
+ for q, a in reversed(st.session_state.chat_history):
132
+ st.markdown(f"**You:** {q}")
133
+ st.markdown(f"**AI:** {a}")
134
+ st.markdown("---")
135
+
136
+ with col2:
137
+ # Display prompts if available
138
+ if prompts:
139
+ st.markdown("### πŸ’‘ **What can I help you with?**")
140
+
141
+ for prompt in prompts:
142
+ with st.container():
143
+ # Highlight placeholders in templates
144
+ templates_html = ""
145
+ for template in prompt['templates']:
146
+ # Replace [placeholder] with highlighted version
147
+ highlighted_template = template.replace('[', '<span class="prompt-template-placeholder">[').replace(']', ']</span>')
148
+ templates_html += f'<div class="prompt-template">πŸ’¬ {highlighted_template}</div>'
149
+
150
+ st.markdown(f"""
151
+ <div class="prompt-card">
152
+ <div class="prompt-title">{prompt['title']}</div>
153
+ <div class="prompt-description">{prompt['description']}</div>
154
+ {templates_html}
155
+ </div>
156
+ """, unsafe_allow_html=True)