Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# Function to get exchange rate and convert
|
| 5 |
+
def convert_currency(amount, from_currency, to_currency):
|
| 6 |
+
try:
|
| 7 |
+
url = f"https://api.exchangerate.host/latest?base={from_currency}"
|
| 8 |
+
response = requests.get(url)
|
| 9 |
+
data = response.json()
|
| 10 |
+
rate = data["rates"][to_currency]
|
| 11 |
+
converted_amount = amount * rate
|
| 12 |
+
return f"{amount} {from_currency} = {converted_amount:.2f} {to_currency}"
|
| 13 |
+
except:
|
| 14 |
+
return "❌ Error: Please check your internet connection or currency codes."
|
| 15 |
+
|
| 16 |
+
# Currency list
|
| 17 |
+
currency_list = [
|
| 18 |
+
"USD", "EUR", "GBP", "INR", "PKR", "CAD", "AUD", "JPY", "CNY", "SAR", "AED"
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
# Gradio UI
|
| 22 |
+
with gr.Blocks() as app:
|
| 23 |
+
gr.Markdown("## 💱 Modern Currency Converter")
|
| 24 |
+
gr.Markdown("Convert currencies in real-time using live exchange rates.")
|
| 25 |
+
|
| 26 |
+
with gr.Row():
|
| 27 |
+
amount = gr.Number(label="Enter Amount", value=1)
|
| 28 |
+
from_currency = gr.Dropdown(currency_list, value="USD", label="From Currency")
|
| 29 |
+
to_currency = gr.Dropdown(currency_list, value="PKR", label="To Currency")
|
| 30 |
+
|
| 31 |
+
convert_btn = gr.Button("Convert 💱")
|
| 32 |
+
output = gr.Textbox(label="Result")
|
| 33 |
+
|
| 34 |
+
convert_btn.click(
|
| 35 |
+
fn=convert_currency,
|
| 36 |
+
inputs=[amount, from_currency, to_currency],
|
| 37 |
+
outputs=output
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
app.launch()
|