Spaces:
Sleeping
Sleeping
File size: 4,111 Bytes
5a2afd7 |
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 |
import gradio as gr
import freecurrencyapi
from datetime import datetime
# Initialize FreeCurrencyAPI Client
client = freecurrencyapi.Client('fca_live_TYFdDMZC9xmEEZPpTJCUdcJQIbdGwis1CHmmYF1d') # Replace with your API key
# Currency list
CURRENCIES = [
"EUR", "USD", "JPY", "BGN", "CZK", "DKK", "GBP", "HUF", "PLN", "RON", "SEK", "CHF", "ISK", "NOK",
"HRK", "RUB", "TRY", "AUD", "BRL", "CAD", "CNY", "HKD", "IDR", "ILS", "INR", "KRW", "MXN", "MYR",
"NZD", "PHP", "SGD", "THB", "ZAR"
]
# Function to get exchange rate and convert currency
def convert_currency(amount, from_currency, to_currency):
try:
result = client.currencies(currencies=[from_currency, to_currency])
conversion_rate = result['data'][to_currency] / result['data'][from_currency]
converted_amount = float(amount) * conversion_rate
return f"{amount} {from_currency} = {converted_amount:.2f} {to_currency}"
except Exception as e:
return f"Error: {str(e)}"
# Function to get historical exchange rates
def get_historical_rates(date, from_currency, to_currency):
try:
# Convert date to the format required by the API
date_obj = datetime.strptime(date, "%Y-%m-%d")
result = client.historical(date_obj.strftime("%Y-%m-%d"))
historical_rate = result['data'][date_obj.strftime("%Y-%m-%d")][to_currency] / result['data'][date_obj.strftime("%Y-%m-%d")][from_currency]
return f"On {date}, {from_currency} to {to_currency} rate was: {historical_rate:.2f}"
except Exception as e:
return f"Error: {str(e)}"
# Define the Gradio interface
def create_interface():
with gr.Blocks() as interface:
# Title
with gr.Row():
gr.Markdown("<h1 style='text-align: center;'>Currency Converter & Historical Rates 🏦💱</h1>")
# Input for amount
with gr.Row():
amount = gr.Textbox(label="Amount", placeholder="Enter amount to convert", elem_id="amount_input")
# From and To currency dropdowns
with gr.Row():
from_currency = gr.Dropdown(choices=CURRENCIES, label="From Currency", value="USD", elem_id="from_currency_input")
to_currency = gr.Dropdown(choices=CURRENCIES, label="To Currency", value="EUR", elem_id="to_currency_input")
# Convert Button
with gr.Row():
convert_button = gr.Button("Convert", elem_id="convert_button")
# Output for converted amount
with gr.Row():
converted_output = gr.Textbox(label="Converted Amount", interactive=False, elem_id="output_text")
# Section for historical exchange rates
with gr.Row():
historical_date = gr.Textbox(label="Historical Date (YYYY-MM-DD)", placeholder="Enter date for historical rates", elem_id="historical_date_input")
historical_button = gr.Button("Get Historical Rate", elem_id="historical_button")
# Output for historical data
with gr.Row():
historical_output = gr.Textbox(label="Historical Rate", interactive=False, elem_id="historical_output_text")
# Button actions
convert_button.click(fn=convert_currency, inputs=[amount, from_currency, to_currency], outputs=converted_output)
historical_button.click(fn=get_historical_rates, inputs=[historical_date, from_currency, to_currency], outputs=historical_output)
# Instructions
with gr.Row():
gr.Markdown("""
#### 💡 Usage Instructions
- **Amount**: Enter the amount you want to convert.
- **From Currency**: Select the currency you want to convert from.
- **To Currency**: Select the currency you want to convert to.
- **Historical Date**: Enter a date in the format YYYY-MM-DD to get historical rates.
#### ⚠️ Limitations
- Historical rates might not be available for all dates, and rates are updated periodically.
""")
return interface
# Launch the interface
interface = create_interface()
interface.launch(share=True)
|