Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from freecurrencyapi import Client
|
| 3 |
+
|
| 4 |
+
# Initialize the FreeCurrencyAPI client
|
| 5 |
+
api_key = 'fca_live_TYFdDMZC9xmEEZPpTJCUdcJQIbdGwis1CHmmYF1d' # Replace with your actual API key
|
| 6 |
+
client = Client(api_key)
|
| 7 |
+
|
| 8 |
+
# Function to get current exchange rates
|
| 9 |
+
def convert_currency(amount, from_currency, to_currency, historical=False, date=None):
|
| 10 |
+
try:
|
| 11 |
+
if historical:
|
| 12 |
+
# Fetch historical data if needed
|
| 13 |
+
result = client.historical(date=date)
|
| 14 |
+
rates = result.get('data', {}).get(date, {})
|
| 15 |
+
else:
|
| 16 |
+
# Get the latest rates
|
| 17 |
+
result = client.currencies(currencies=[from_currency, to_currency])
|
| 18 |
+
rates = result.get('data', {}).get('rates', {})
|
| 19 |
+
|
| 20 |
+
# Get the conversion rate for the desired currencies
|
| 21 |
+
if from_currency in rates and to_currency in rates:
|
| 22 |
+
conversion_rate = rates[to_currency] / rates[from_currency]
|
| 23 |
+
converted_amount = float(amount) * conversion_rate
|
| 24 |
+
return f"{amount} {from_currency} = {converted_amount:.2f} {to_currency}"
|
| 25 |
+
else:
|
| 26 |
+
return "Error: Invalid currency code or data not available."
|
| 27 |
+
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"Error: {str(e)}"
|
| 30 |
+
|
| 31 |
+
# Define the Gradio interface
|
| 32 |
+
def create_interface():
|
| 33 |
+
with gr.Blocks() as interface:
|
| 34 |
+
with gr.Row():
|
| 35 |
+
gr.Markdown("<h1 style='text-align: center;'>Currency Converter</h1>")
|
| 36 |
+
|
| 37 |
+
with gr.Row():
|
| 38 |
+
amount = gr.Textbox(label="Amount", value="1", placeholder="Enter amount", elem_id="amount_input")
|
| 39 |
+
|
| 40 |
+
with gr.Row():
|
| 41 |
+
from_currency = gr.Dropdown(
|
| 42 |
+
choices=["USD", "EUR", "GBP", "INR", "JPY", "CAD", "AUD", "CNY", "BRL", "ZAR"],
|
| 43 |
+
label="From Currency", value="USD", elem_id="from_currency_input"
|
| 44 |
+
)
|
| 45 |
+
to_currency = gr.Dropdown(
|
| 46 |
+
choices=["USD", "EUR", "GBP", "INR", "JPY", "CAD", "AUD", "CNY", "BRL", "ZAR"],
|
| 47 |
+
label="To Currency", value="EUR", elem_id="to_currency_input"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
historical_checkbox = gr.Checkbox(label="Get Historical Data", value=False)
|
| 52 |
+
historical_date = gr.Date(label="Select Date", visible=False)
|
| 53 |
+
|
| 54 |
+
with gr.Row():
|
| 55 |
+
convert_button = gr.Button("Convert")
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
output = gr.Textbox(label="Converted Amount", interactive=False)
|
| 59 |
+
|
| 60 |
+
# Update the visibility of the date picker based on historical checkbox
|
| 61 |
+
historical_checkbox.change(
|
| 62 |
+
lambda checked: historical_date.update(visible=checked), inputs=[historical_checkbox], outputs=[historical_date]
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Function to trigger conversion with historical data if required
|
| 66 |
+
convert_button.click(
|
| 67 |
+
fn=convert_currency,
|
| 68 |
+
inputs=[amount, from_currency, to_currency, historical_checkbox, historical_date],
|
| 69 |
+
outputs=output
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
return interface
|
| 73 |
+
|
| 74 |
+
# Launch the interface
|
| 75 |
+
interface = create_interface()
|
| 76 |
+
interface.launch(share=True)
|