Pontonkid commited on
Commit
5a2afd7
·
verified ·
1 Parent(s): 9529223

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import freecurrencyapi
3
+ from datetime import datetime
4
+
5
+ # Initialize FreeCurrencyAPI Client
6
+ client = freecurrencyapi.Client('fca_live_TYFdDMZC9xmEEZPpTJCUdcJQIbdGwis1CHmmYF1d') # Replace with your API key
7
+
8
+ # Currency list
9
+ CURRENCIES = [
10
+ "EUR", "USD", "JPY", "BGN", "CZK", "DKK", "GBP", "HUF", "PLN", "RON", "SEK", "CHF", "ISK", "NOK",
11
+ "HRK", "RUB", "TRY", "AUD", "BRL", "CAD", "CNY", "HKD", "IDR", "ILS", "INR", "KRW", "MXN", "MYR",
12
+ "NZD", "PHP", "SGD", "THB", "ZAR"
13
+ ]
14
+
15
+ # Function to get exchange rate and convert currency
16
+ def convert_currency(amount, from_currency, to_currency):
17
+ try:
18
+ result = client.currencies(currencies=[from_currency, to_currency])
19
+ conversion_rate = result['data'][to_currency] / result['data'][from_currency]
20
+ converted_amount = float(amount) * conversion_rate
21
+ return f"{amount} {from_currency} = {converted_amount:.2f} {to_currency}"
22
+ except Exception as e:
23
+ return f"Error: {str(e)}"
24
+
25
+ # Function to get historical exchange rates
26
+ def get_historical_rates(date, from_currency, to_currency):
27
+ try:
28
+ # Convert date to the format required by the API
29
+ date_obj = datetime.strptime(date, "%Y-%m-%d")
30
+ result = client.historical(date_obj.strftime("%Y-%m-%d"))
31
+ historical_rate = result['data'][date_obj.strftime("%Y-%m-%d")][to_currency] / result['data'][date_obj.strftime("%Y-%m-%d")][from_currency]
32
+ return f"On {date}, {from_currency} to {to_currency} rate was: {historical_rate:.2f}"
33
+ except Exception as e:
34
+ return f"Error: {str(e)}"
35
+
36
+ # Define the Gradio interface
37
+ def create_interface():
38
+ with gr.Blocks() as interface:
39
+ # Title
40
+ with gr.Row():
41
+ gr.Markdown("<h1 style='text-align: center;'>Currency Converter & Historical Rates 🏦💱</h1>")
42
+
43
+ # Input for amount
44
+ with gr.Row():
45
+ amount = gr.Textbox(label="Amount", placeholder="Enter amount to convert", elem_id="amount_input")
46
+
47
+ # From and To currency dropdowns
48
+ with gr.Row():
49
+ from_currency = gr.Dropdown(choices=CURRENCIES, label="From Currency", value="USD", elem_id="from_currency_input")
50
+ to_currency = gr.Dropdown(choices=CURRENCIES, label="To Currency", value="EUR", elem_id="to_currency_input")
51
+
52
+ # Convert Button
53
+ with gr.Row():
54
+ convert_button = gr.Button("Convert", elem_id="convert_button")
55
+
56
+ # Output for converted amount
57
+ with gr.Row():
58
+ converted_output = gr.Textbox(label="Converted Amount", interactive=False, elem_id="output_text")
59
+
60
+ # Section for historical exchange rates
61
+ with gr.Row():
62
+ historical_date = gr.Textbox(label="Historical Date (YYYY-MM-DD)", placeholder="Enter date for historical rates", elem_id="historical_date_input")
63
+ historical_button = gr.Button("Get Historical Rate", elem_id="historical_button")
64
+
65
+ # Output for historical data
66
+ with gr.Row():
67
+ historical_output = gr.Textbox(label="Historical Rate", interactive=False, elem_id="historical_output_text")
68
+
69
+ # Button actions
70
+ convert_button.click(fn=convert_currency, inputs=[amount, from_currency, to_currency], outputs=converted_output)
71
+ historical_button.click(fn=get_historical_rates, inputs=[historical_date, from_currency, to_currency], outputs=historical_output)
72
+
73
+ # Instructions
74
+ with gr.Row():
75
+ gr.Markdown("""
76
+ #### 💡 Usage Instructions
77
+ - **Amount**: Enter the amount you want to convert.
78
+ - **From Currency**: Select the currency you want to convert from.
79
+ - **To Currency**: Select the currency you want to convert to.
80
+ - **Historical Date**: Enter a date in the format YYYY-MM-DD to get historical rates.
81
+
82
+ #### ⚠️ Limitations
83
+ - Historical rates might not be available for all dates, and rates are updated periodically.
84
+ """)
85
+
86
+ return interface
87
+
88
+ # Launch the interface
89
+ interface = create_interface()
90
+ interface.launch(share=True)