File size: 10,524 Bytes
900d7e8
f4ca844
 
 
d094ea0
22e33ed
4669a77
900d7e8
f4ca844
d094ea0
f4ca844
d094ea0
 
900d7e8
f6d1cff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d094ea0
 
 
 
 
 
 
4618b90
 
 
 
 
 
 
 
3a65210
f6d1cff
d094ea0
1b4db50
d094ea0
 
252c5e7
f6d1cff
4618b90
 
252c5e7
4618b90
 
 
 
22e33ed
4618b90
 
252c5e7
1b4db50
f6d1cff
 
4618b90
f6d1cff
4618b90
 
900d7e8
f4ca844
 
 
 
3a65210
f4ca844
 
4669a77
 
 
 
 
 
 
 
f4ca844
 
 
 
 
f6d1cff
f4ca844
 
 
 
 
3a65210
f4ca844
 
 
 
 
 
 
3a65210
 
 
 
 
 
 
 
 
 
 
 
 
d094ea0
 
 
 
4618b90
d094ea0
f4ca844
d094ea0
 
 
f4ca844
3a65210
f4ca844
 
f6d1cff
f4ca844
 
 
 
 
 
 
 
 
 
 
22e33ed
f4ca844
 
 
 
3a65210
 
 
 
 
f4ca844
3a65210
 
4618b90
 
 
 
 
 
 
 
 
 
3a65210
4618b90
 
3a65210
4618b90
3a65210
 
 
 
 
 
4618b90
3a65210
 
4618b90
 
 
 
 
3a65210
4618b90
 
 
3a65210
 
 
 
 
 
 
f4ca844
4618b90
 
 
 
 
22e33ed
 
4618b90
 
 
 
 
f4ca844
 
 
3a65210
 
 
001487b
618ba03
d094ea0
 
 
4618b90
252c5e7
3a65210
f4ca844
d094ea0
 
 
 
 
 
 
 
 
 
4669a77
f4ca844
4618b90
f4ca844
 
 
 
d094ea0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252c5e7
900d7e8
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import gradio as gr
import os
from dotenv import load_dotenv
from pathlib import Path
from modules.utils import load_processed_meetings, load_prompt_library
from src.modules.fed_tools import search_meetings_by_date, FED_TOOLS
from src.modules.llm_completions import stream_fed_agent_response

load_dotenv()

_FILE_PATH = Path(__file__).parents[1]
FOMC_MEETINGS = load_processed_meetings()
PROMPT_LIBRARY = load_prompt_library()

_CUSTOM_CSS = """
        .gradio-container {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        .chat-message {
            border-radius: 10px;
            padding: 10px;
            margin: 5px 0;
        }
        .function-call {
            background-color: #f0f8ff;
            border-left: 4px solid #1e88e5;
            padding: 10px;
            margin: 10px 0;
            border-radius: 5px;
        }
"""
_EXAMPLES = [
    "What was the rate decision in the last FOMC meeting?",
    "What is the most likely rate decision in the next FOMC meeting?",
    "What does the economic outlook look like today?",
    "Give me a table which summarized the last 5 FOMC meetings"
]

def convert_history_to_string(history: list) -> str:
    previous_messages = ""
    for msg in history:
        previous_messages += f"{msg['role'].capitalize()}: {msg['content']}\n\n"
    return previous_messages


def respond_for_chat_interface(message: str, history: list, api_key_input: str = ""):
    """Enhanced response function for gr.ChatInterface with Fed AI Savant capabilities"""

    api_key = api_key_input.strip() if api_key_input else os.getenv("FIREWORKS_API_KEY", "")

    if not api_key:
        yield "❌ Please enter your Fireworks AI API key in the configuration section above."
        return

    message_with_history = convert_history_to_string(history)

    try:
        for messages in stream_fed_agent_response(
                message=message,
                api_key=api_key,
                prompt_library=PROMPT_LIBRARY,
                fed_tools=FED_TOOLS,
                history=message_with_history
        ):
            if isinstance(messages, list) and len(messages) > 0:
                yield messages
            else:
                yield str(messages)

    except Exception as e:
        error_msg = f"❌ Error: {str(e)}"
        yield error_msg

# Function to create searchable FOMC meetings accordion
def create_fomc_meetings_accordion():
    """Create searchable accordion for FOMC meetings"""
    accordions = []
    for meeting in FOMC_MEETINGS:
        title = f"{meeting['date']} - Rate: {meeting['rate_decision']}"
        content = f"""
            **Meeting Title:** {meeting['title']}
            
            **Rate Decision:** {meeting['rate_decision']}
            
            **Summary:** {meeting['summary']}
            
            ---
            *Click to expand for full meeting details*
        """
        accordions.append((title, content))
    return accordions

# Create the enhanced interface
with gr.Blocks(css=_CUSTOM_CSS, title="Fed AI Savant", theme=gr.themes.Soft()) as demo:
    
    # Row 1: Title and Description
    with gr.Row():
        with gr.Column():
            gr.Markdown("""
            # πŸ›οΈ Fed AI Savant πŸ›οΈ 
            **Intelligent Analysis of Federal Reserve Policy and FOMC Meetings**
            
            Ask questions about interest rate decisions, monetary policy changes, and economic analysis based on Federal Reserve meeting minutes.
            """)
    
    # Row 2: API Key Configuration
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### Powered by")
            gr.Image(
                value=str(_FILE_PATH / "assets" / "fireworks_logo.png"),
                height=60,
                width=200,
                show_label=False,
                show_download_button=False,
                container=False,
                show_fullscreen_button=False,
                show_share_button=False,
            )
        with gr.Column(scale=1):
            gr.Markdown("## βš™οΈ Configuration")

            val = os.getenv("FIREWORKS_API_KEY", "")

            api_key_value = gr.Textbox(
                label="Fireworks AI API Key",
                type="password",
                placeholder="Enter your Fireworks AI API key",
                value=val,
                info="Required for AI processing",
            )
        with gr.Column(scale=2):
            gr.Markdown("### πŸ“‹ How to Use")
            gr.Markdown("""
            1. **Enter your AI API key**
            2. **Ask questions** about Fed policy, rate decisions, or FOMC meetings  
            3. **Review AI reasoning** with expandable explanations and sources
            """)
    
    # Row 3: FOMC Meetings Accordion (Searchable by Date)
    with gr.Row():
        with gr.Column():
            gr.Markdown("### πŸ“Š Recent FOMC Meeting Minutes")
            
            # Date search
            date_search = gr.Textbox(
                placeholder="Search by date (e.g., 2025-07, 'June 2024', etc)...",
                label="πŸ” Search Meetings by Date",
                lines=1
            )
            
            with gr.Accordion("FOMC Meetings", open=False):
                def generate_meetings_html(meetings_list):
                    """Generate HTML for meetings list"""
                    if not meetings_list:
                        return '<p style="color: #6b7280; text-align: center; padding: 20px;">No meetings available</p>'
                    
                    html_content = '<div style="space-y: 8px;">'
                    for meeting in meetings_list:
                        date = meeting.get('date', 'Unknown Date')
                        rate_decision = meeting.get('rate_decision', 'N/A')
                        title = meeting.get('title', 'FOMC Meeting')
                        action = meeting.get('action', 'N/A')
                        magnitude = meeting.get('magnitude', 'N/A')
                        summary = meeting.get('summary', 'No summary available')
                        economic_outlook = meeting.get('economic_outlook', 'N/A')
                        market_impact = meeting.get('market_impact', 'N/A')
                        url = meeting.get('url', '')
                        
                        factors_html = ""
                        key_factors = meeting.get('key_economic_factors', [])
                        if key_factors and len(key_factors) > 0:
                            factors_html = "<p><strong>Key Factors:</strong></p><ul>"
                            for factor in key_factors:
                                factors_html += f"<li>{factor}</li>"
                            factors_html += "</ul>"
                        
                        html_content += f"""
                        <details style="border: 1px solid #e5e7eb; border-radius: 6px; padding: 12px; margin-bottom: 8px;">
                            <summary style="font-weight: 600; cursor: pointer; color: #1f2937;">
                                πŸ“… {date} - Rate: {rate_decision}
                            </summary>
                            <div style="margin-top: 12px; padding-top: 12px; border-top: 1px solid #e5e7eb;">
                                <p><strong>Meeting:</strong> {title}</p>
                                <p><strong>Action:</strong> {action}</p>
                                <p><strong>Rate:</strong> {rate_decision}</p>
                                <p><strong>Magnitude:</strong> {magnitude}</p>
                                <p><strong>Forward Guidance:</strong> {summary}</p>
                                {factors_html}
                                <p><strong>Economic Outlook:</strong> {economic_outlook}</p>
                                <p><strong>Market Impact:</strong> {market_impact}</p>
                                {f'<p><strong>Source:</strong> <a href="{url}" target="_blank">Fed Minutes PDF</a></p>' if url else ''}
                            </div>
                        </details>
                        """
                    html_content += '</div>'
                    return html_content

                meetings_accordion = gr.HTML(generate_meetings_html(FOMC_MEETINGS))
    
    def search_and_format_meetings(query: str):
        """Search meetings and format them for HTML display"""
        if not query.strip():
            return generate_meetings_html(FOMC_MEETINGS)
        
        search_result = search_meetings_by_date(query)
        
        if search_result.get("success") and search_result.get("results"):
            return generate_meetings_html(search_result["results"])
        else:
            return '<p style="color: #ef4444; text-align: center; padding: 20px;">No meetings found matching your search.</p>'
    
    with gr.Row():
        with gr.Column():
            gr.Markdown("### πŸ’¬ Fed AI Assistant")

            chat_interface = gr.ChatInterface(
                fn=respond_for_chat_interface,
                type="messages",
                chatbot=gr.Chatbot(height=600, show_label=False, type="messages"),
                textbox=gr.Textbox(
                    placeholder="Ask about Fed policy, rate decisions, or FOMC meetings...", scale=10
                ),
                additional_inputs=[api_key_value],
                cache_examples=False,
                submit_btn="Send",
            )
            with gr.Row():
                with gr.Column(scale=1):
                    example_1 = gr.Button(_EXAMPLES[0], size="md")
                    example_2= gr.Button(
                        _EXAMPLES[1], size="md"
                    )
                with gr.Column(scale=1):
                    example_3 = gr.Button(_EXAMPLES[2], size="md")
                    example_4 = gr.Button(_EXAMPLES[3], size="md")


    date_search.change(
        search_and_format_meetings,
        inputs=date_search,
        outputs=meetings_accordion
    )
    
    def set_example_text(text):
        return text
    
    example_1.click(
        lambda: _EXAMPLES[0],
        outputs=chat_interface.textbox
    )
    example_2.click(
        lambda: _EXAMPLES[1],
        outputs=chat_interface.textbox
    )
    example_3.click(
        lambda: _EXAMPLES[2],
        outputs=chat_interface.textbox
    )
    example_4.click(
        lambda: _EXAMPLES[3],
        outputs=chat_interface.textbox
    )

if __name__ == "__main__":
    demo.launch()