Spaces:
Sleeping
Sleeping
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) | |