gentrich commited on
Commit
15e8f83
·
verified ·
1 Parent(s): a89a8cf

Upload folder using huggingface_hub

Browse files
Files changed (6) hide show
  1. .gradio/certificate.pem +31 -0
  2. README.md +3 -9
  3. __init__.py +50 -0
  4. data_load.py +63 -0
  5. ir.xml +114 -0
  6. ir_curve.py +58 -0
.gradio/certificate.pem ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
3
+ TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
4
+ cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
5
+ WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
6
+ ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
7
+ MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
8
+ h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
9
+ 0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
10
+ A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW
11
+ T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH
12
+ B5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UC
13
+ B5iPNgiV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7y0Ff/9yi0GE44Za4rF2LN9d11TPA
29
+ mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
30
+ emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
31
+ -----END CERTIFICATE-----
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Ir Curve
3
- emoji: 🦀
4
- colorFrom: pink
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 5.13.1
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: ir_curve
3
+ app_file: ir_curve.py
 
 
4
  sdk: gradio
5
+ sdk_version: 5.12.0
 
 
6
  ---
 
 
__init__.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # links:
2
+ # https://rfr.ihsmarkit.com/InterestRates_USD_20250124.zip?email=yguobin@hotmail.com
3
+
4
+
5
+ # questions in Chapter 4
6
+ # question = "List all of the teams in alphabetical order."
7
+ # question = "Show me all the bowling score information for each of our members. Please provide complete info. Don't limit the result."
8
+ # question = "Show me a list of bowlers and their addresses, and sort it in alphabetical order. Please provide complete list."
9
+
10
+ # questions in Chapter 5
11
+ # question = "Show next year’s tournament date for each tournament location. Please provide complete list. No explanation please."
12
+ # question = "List the name and phone number for each member of the league. Please provide complete list."
13
+ # question = "Give me a listing of each team’s lineup."
14
+
15
+ # questions in Chapter 6
16
+ # question = "Give me a list of the tournaments held during September 2017"
17
+ # question = "What are the tournament schedules for Bolero, Red Rooster, and Thunderbird Lanes?"
18
+ # question = "List the bowlers who live on the Eastside (you know—Bellevue, Bothell, Duvall, Redmond, and Woodinville) and who are on teams 5, 6, 7, or 8"
19
+
20
+ # questions in Chapter 8
21
+ # question = "List the bowling teams and all the team members"
22
+ # question = "Display the bowlers, the matches they played in, and the bowler game scores."
23
+ # question = "Find the bowlers who live in the same ZIP Code"
24
+
25
+ # questions in Chapter 9
26
+ # question = "Display matches with no game data"
27
+ # question = "Display all tournaments and any matches that have been played"
28
+
29
+ # questions in Chapter 10
30
+ # question = "Find the bowlers who had a raw score of 165 or better at Thunderbird Lanes combined with bowlers who had a raw score of 150 or better at Bolero Lanes"
31
+
32
+ # questions in Chapter 11
33
+ # question = "Show me all the bowlers and a count of games each bowled"
34
+ # question = "List all the bowlers who have a raw score that’s less than all of the other bowlers on the same team"
35
+
36
+ # questions in Chapter 12
37
+ # question = "What is the largest handicap held by any bowler at the current time?"
38
+ # question = "Which locations hosted tournaments on the earliest tournament date?"
39
+ # question = "What is the last tournament date we have on our schedule?"
40
+
41
+ # questions in Chapter 13
42
+ # question = "Display for each bowler the bowler name and the average of the bowler’s raw game scores."
43
+ # question = "Calculate the current average and handicap for each bowler"
44
+ # question = "Display the highest raw score for each bowler"
45
+
46
+ # questions in Chapter 14
47
+ # question = "Do any team captains have a raw score that is higher than any other member of the team?"
48
+ # question = "Display for each bowler the bowler name and the average of the bowler’s raw game scores for bowlers whose average is greater than 155."
49
+ # question = "List the last name and first name of every bowler whose average raw score is greater than or equal to the overall average score."
50
+
data_load.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import traceback
2
+ from datetime import datetime, date
3
+ from io import BytesIO
4
+ from typing import TypedDict, Literal, List, Optional, Annotated
5
+ from zipfile import ZipFile
6
+ from xml.etree import ElementTree
7
+ import requests
8
+
9
+ from langchain_core.tools import tool
10
+
11
+ supported_currencies = ['USD', 'JPY', 'EUR', 'GBP', 'CHF', 'AUD']
12
+
13
+ class CurvePoint(TypedDict):
14
+ """
15
+ Interest rate for a tenor
16
+ """
17
+ tenor: Annotated[str, 'tenor of the interest rate. (examples: tenor 1M is One month interest rate, tenor 1Y is One year interest rate)']
18
+ maturity_date: Annotated[date, 'date that the tenor expires (matured)']
19
+ rate: Annotated[float, 'interest rate of the tenor']
20
+
21
+ class IRCurve(TypedDict):
22
+ """
23
+ Interest rate curve holds interest rates on the effective date for the currency
24
+ """
25
+ effective_date: Annotated[date, 'the date that the interest rates were observed']
26
+ currency: Annotated[Literal['USD', 'JPY', 'EUR', 'GBP', 'CHF', 'AUD'], 'the currency of the interest rate']
27
+ curve_points: Annotated[List[CurvePoint], 'interest rates for the tenors (examples: tenor 1M is One month interest rate, tenor 1Y is One year interest rate)']
28
+
29
+ def _build_ir(xml: str, currency: Literal['USD', 'JPY', 'EUR', 'GBP', 'CHF', 'AUD']) -> IRCurve:
30
+ root = ElementTree.fromstring(xml)
31
+ effective_date_str = root.find('interestRateCurve').find('ois').find('snaptime').text
32
+ effective_date = datetime.strptime(effective_date_str, '%Y-%m-%dT%H:%M:%S').date()
33
+ curve_points = []
34
+ for curve_point in root.find('interestRateCurve').find('ois').findall('curvepoint'):
35
+ tenor = curve_point.find('tenor').text
36
+ maturity_date_str = curve_point.find('maturitydate').text
37
+ maturity_date = datetime.strptime(maturity_date_str, '%Y-%m-%d').date()
38
+ rate = float(curve_point.find('parrate').text)
39
+ curve_points.append(CurvePoint(tenor=tenor, maturity_date=maturity_date, rate=rate))
40
+ return IRCurve(effective_date=effective_date, currency=currency, curve_points=curve_points)
41
+
42
+ @tool
43
+ def load_interest_rate_curve(date_obj: date, currency: Literal['USD', 'JPY', 'EUR', 'GBP', 'CHF', 'AUD']) \
44
+ -> Annotated[Optional[IRCurve], 'Interest rate object in a Python dictionary']:
45
+ """
46
+ Loads interest rate curve for the given date and given currency
47
+ Returns Interest rate object (IRCurve, which is a Python dictionary) if the interest rates can be found
48
+ Returns None if the interest rates cannot be found
49
+ """
50
+ try:
51
+ url = f'https://rfr.ihsmarkit.com/InterestRates_{currency}_{date_obj.strftime('%Y%m%d')}.zip?email=yguobin@hotmail.com'
52
+ req = requests.get(url)
53
+ zip = ZipFile(BytesIO(req.content))
54
+ file = f'InterestRates_{currency}_{date_obj.strftime('%Y%m%d')}.xml'
55
+ lines = zip.open(file).readlines()
56
+ xml = "".join([line.decode('utf-8') for line in lines])
57
+ return _build_ir(xml, currency)
58
+ except Exception as e:
59
+ print(traceback.format_exc())
60
+ return None
61
+
62
+ # if __name__ == '__main__':
63
+ # print(load_interest_rate_curve.invoke(date(2025, 1, 24), 'USD')))
ir.xml ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version='1.0' encoding='UTF-8'?>
2
+ <IHSM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3
+ <interestRateCurve>
4
+ <effectiveasof>2025-01-27</effectiveasof>
5
+ <currency>USD</currency>
6
+ <baddayconvention>M</baddayconvention>
7
+ <ois>
8
+ <fixeddaycountconvention>ACT/360</fixeddaycountconvention>
9
+ <floatingdaycountconvention>ACT/360</floatingdaycountconvention>
10
+ <fixedpaymentfrequency>1Y</fixedpaymentfrequency>
11
+ <floatingpaymentfrequency>1Y</floatingpaymentfrequency>
12
+ <snaptime>2025-01-24T21:00:00</snaptime>
13
+ <spotdate>2025-01-29</spotdate>
14
+ <calendars>
15
+ <calendar>NYM</calendar>
16
+ </calendars>
17
+ <curvepoint>
18
+ <tenor>1M</tenor>
19
+ <maturitydate>2025-02-28</maturitydate>
20
+ <parrate>0.043141</parrate>
21
+ </curvepoint>
22
+ <curvepoint>
23
+ <tenor>2M</tenor>
24
+ <maturitydate>2025-03-29</maturitydate>
25
+ <parrate>0.043095</parrate>
26
+ </curvepoint>
27
+ <curvepoint>
28
+ <tenor>3M</tenor>
29
+ <maturitydate>2025-04-29</maturitydate>
30
+ <parrate>0.042952</parrate>
31
+ </curvepoint>
32
+ <curvepoint>
33
+ <tenor>6M</tenor>
34
+ <maturitydate>2025-07-29</maturitydate>
35
+ <parrate>0.042475</parrate>
36
+ </curvepoint>
37
+ <curvepoint>
38
+ <tenor>1Y</tenor>
39
+ <maturitydate>2026-01-29</maturitydate>
40
+ <parrate>0.041822</parrate>
41
+ </curvepoint>
42
+ <curvepoint>
43
+ <tenor>2Y</tenor>
44
+ <maturitydate>2027-01-29</maturitydate>
45
+ <parrate>0.04103</parrate>
46
+ </curvepoint>
47
+ <curvepoint>
48
+ <tenor>3Y</tenor>
49
+ <maturitydate>2028-01-29</maturitydate>
50
+ <parrate>0.040953</parrate>
51
+ </curvepoint>
52
+ <curvepoint>
53
+ <tenor>4Y</tenor>
54
+ <maturitydate>2029-01-29</maturitydate>
55
+ <parrate>0.041018</parrate>
56
+ </curvepoint>
57
+ <curvepoint>
58
+ <tenor>5Y</tenor>
59
+ <maturitydate>2030-01-29</maturitydate>
60
+ <parrate>0.0411</parrate>
61
+ </curvepoint>
62
+ <curvepoint>
63
+ <tenor>6Y</tenor>
64
+ <maturitydate>2031-01-29</maturitydate>
65
+ <parrate>0.041175</parrate>
66
+ </curvepoint>
67
+ <curvepoint>
68
+ <tenor>7Y</tenor>
69
+ <maturitydate>2032-01-29</maturitydate>
70
+ <parrate>0.041255</parrate>
71
+ </curvepoint>
72
+ <curvepoint>
73
+ <tenor>8Y</tenor>
74
+ <maturitydate>2033-01-29</maturitydate>
75
+ <parrate>0.041325</parrate>
76
+ </curvepoint>
77
+ <curvepoint>
78
+ <tenor>9Y</tenor>
79
+ <maturitydate>2034-01-29</maturitydate>
80
+ <parrate>0.041408</parrate>
81
+ </curvepoint>
82
+ <curvepoint>
83
+ <tenor>10Y</tenor>
84
+ <maturitydate>2035-01-29</maturitydate>
85
+ <parrate>0.041505</parrate>
86
+ </curvepoint>
87
+ <curvepoint>
88
+ <tenor>12Y</tenor>
89
+ <maturitydate>2037-01-29</maturitydate>
90
+ <parrate>0.041725</parrate>
91
+ </curvepoint>
92
+ <curvepoint>
93
+ <tenor>15Y</tenor>
94
+ <maturitydate>2040-01-29</maturitydate>
95
+ <parrate>0.041985</parrate>
96
+ </curvepoint>
97
+ <curvepoint>
98
+ <tenor>20Y</tenor>
99
+ <maturitydate>2045-01-29</maturitydate>
100
+ <parrate>0.04189</parrate>
101
+ </curvepoint>
102
+ <curvepoint>
103
+ <tenor>25Y</tenor>
104
+ <maturitydate>2050-01-29</maturitydate>
105
+ <parrate>0.041141</parrate>
106
+ </curvepoint>
107
+ <curvepoint>
108
+ <tenor>30Y</tenor>
109
+ <maturitydate>2055-01-29</maturitydate>
110
+ <parrate>0.040245</parrate>
111
+ </curvepoint>
112
+ </ois>
113
+ </interestRateCurve>
114
+ </IHSM>
ir_curve.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+
3
+ from data_load import load_interest_rate_curve, supported_currencies
4
+ from langchain_anthropic import ChatAnthropic
5
+ from langchain_core.messages import AIMessage
6
+ from langgraph.prebuilt import create_react_agent
7
+
8
+ import gradio as gr
9
+
10
+ if __name__ == '__main__':
11
+ model = ChatAnthropic(model="claude-3-5-haiku-20241022", temperature=0)
12
+
13
+ tools = [load_interest_rate_curve]
14
+
15
+ # parameter: supported_currencies
16
+ prompt_template = """
17
+ system
18
+ You are an agent named RichBot. You are designed to load and analyze interest rates
19
+ in {supported_currencies} for any trading day.
20
+ Given an input question, you can invoke the tool to load interest rates for the given date and currency,
21
+ then analyze the loaded interest rates to answer the questions.
22
+ If the input question is not one the supported currency, just answer "I don't know"
23
+ If the tool doesn't return meaningful interest rate curve object, you can also answer "I don't know".
24
+ You can invoke the tool as many times as you like to load relevant interest rates.
25
+ Please only answer the question based on the data returned from the tools.
26
+ Don't fake the data. Don't answer based on your past knowledge.
27
+ """
28
+ system_message = prompt_template.format(supported_currencies=supported_currencies)
29
+
30
+ agent_executor = create_react_agent(model, tools, state_modifier=system_message)
31
+
32
+ # for step in agent_executor.stream({"messages": [{"role": "user", "content": question}]}, stream_mode="values"):
33
+ # step["messages"][-1].pretty_print()
34
+
35
+ def ir_curve(question: str, history: list[dict]) -> str:
36
+ # answer = agent_executor.invoke({"messages": [{"role": "user", "content": question}]})
37
+ # return answer['messages'][-1].content
38
+ messages = history + [{"role": "user", "content": question}]
39
+ for step in agent_executor.stream({"messages": messages}, stream_mode="values"):
40
+ last_message = step["messages"][-1]
41
+ if isinstance(last_message, AIMessage) and last_message.content:
42
+ if isinstance(last_message.content, list):
43
+ for c in last_message.content:
44
+ if 'text' in c:
45
+ yield c['text']
46
+ elif isinstance(last_message.content, str):
47
+ yield last_message.content
48
+
49
+ gr.ChatInterface(
50
+ fn=ir_curve,
51
+ type="messages",
52
+ title="RichBot",
53
+ description=f"""
54
+ A simple bot that can load and analyze interest rates in these currencies: {supported_currencies}. <br/>
55
+ Data comes from https://rfr.ihsmarkit.com
56
+ """,
57
+ theme='John6666/YntecDark'
58
+ ).launch(share=True)