SHAMIL SHAHBAZ AWAN commited on
Commit
85d8ab6
Β·
verified Β·
1 Parent(s): bbe5534

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import os
3
+ import gradio as gr
4
+
5
+ def fetch_exchange_rates():
6
+ """
7
+ Fetches the latest exchange rates from the ExchangeRate API.
8
+ """
9
+ API_KEY = os.getenv('EXCHANGE_RATE_API_KEY') # Get API key from environment variable
10
+ if not API_KEY:
11
+ return None, "❌ API key is missing. Please set it in the environment variable 'EXCHANGE_RATE_API_KEY'."
12
+ url = f"https://v6.exchangerate-api.com/v6/{API_KEY}/latest/USD"
13
+ try:
14
+ response = requests.get(url)
15
+ response.raise_for_status() # Raise an exception for HTTP errors
16
+ data = response.json()
17
+ except requests.RequestException as e:
18
+ return None, f"❌ Error fetching data from API: {e}"
19
+
20
+ if data.get('result') != 'success':
21
+ return None, "❌ Failed to fetch exchange rates. Check your API key or account limits."
22
+
23
+ return data.get('conversion_rates'), None
24
+
25
+
26
+ def convert_currency(amount, input_currency, output_currency):
27
+ """
28
+ Converts the given amount from one currency to another using exchange rates.
29
+ """
30
+ exchange_rates, error = fetch_exchange_rates()
31
+
32
+ if error:
33
+ return error
34
+
35
+ if input_currency not in exchange_rates or output_currency not in exchange_rates:
36
+ return "❌ Invalid currency. Please ensure the currency codes are valid."
37
+
38
+ # Perform the conversion
39
+ input_rate = exchange_rates[input_currency]
40
+ output_rate = exchange_rates[output_currency]
41
+ converted_amount = amount * (output_rate / input_rate)
42
+ rounded_amount = round(converted_amount, 2)
43
+
44
+ return f"{amount} {input_currency} is equal to {rounded_amount} {output_currency}."
45
+
46
+
47
+ # Gradio Interface
48
+ def interface(amount, input_currency, output_currency):
49
+ try:
50
+ amount = float(amount)
51
+ if amount <= 0:
52
+ return "❌ Amount must be greater than 0."
53
+ except ValueError:
54
+ return "❌ Invalid input. Please enter a valid number."
55
+
56
+ return convert_currency(amount, input_currency.upper(), output_currency.upper())
57
+
58
+
59
+ # Create Gradio app
60
+ with gr.Blocks() as app:
61
+ gr.Markdown("## 🌍 Currency Converter using Live API")
62
+ gr.Markdown(
63
+ "Convert currencies using live exchange rates. Enter the amount, "
64
+ "source currency (e.g., USD), and target currency (e.g., PKR)."
65
+ )
66
+
67
+ with gr.Row():
68
+ amount_input = gr.Textbox(label="Amount", placeholder="Enter amount (e.g., 100)")
69
+ input_currency_input = gr.Textbox(label="From Currency", placeholder="Enter source currency code (e.g., USD)")
70
+ output_currency_input = gr.Textbox(label="To Currency", placeholder="Enter target currency code (e.g., PKR)")
71
+
72
+ convert_button = gr.Button("Convert")
73
+ result_output = gr.Textbox(label="Result", placeholder="Result will appear here", interactive=False)
74
+
75
+ convert_button.click(
76
+ interface,
77
+ inputs=[amount_input, input_currency_input, output_currency_input],
78
+ outputs=result_output,
79
+ )
80
+
81
+ # Launch the Gradio app
82
+ if __name__ == "__main__":
83
+ app.launch()