Spaces:
Runtime error
Runtime error
File size: 5,036 Bytes
22cc439 |
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
import gradio as gr
import requests
# Your API key for the exchange rate API
api_key = '91c5a69825b5e2377b0992cf'
base_url = f'https://v6.exchangerate-api.com/v6/{api_key}/latest/USD'
# Mapping of currency names to their ISO codes
currency_map = {
'algerian dinar': 'DZD',
'angolan kwanza': 'AOA',
'cfa franc bceao': 'XOF',
'botswana pula': 'BWP',
'burundian franc': 'BIF',
'cfa franc beac': 'XAF',
'cape verdean escudo': 'CVE',
'comorian franc': 'KMF',
'congolese franc': 'CDF',
'djiboutian franc': 'DJF',
'egypt pound': 'EGP',
'nakfa': 'ERN',
'ethiopian birr': 'ETB',
'dalasi': 'GMD',
'ghanaian cedi': 'GHS',
'guinean franc': 'GNF',
'kenyan shilling': 'KES',
'lesotho loti': 'LSL',
'liberian dollar': 'LRD',
'libyan dinar': 'LYD',
'ariary': 'MGA',
'malawian kwacha': 'MWK',
'mauritian rupee': 'MUR',
'moroccan dirham': 'MAD',
'mozambique metical': 'MZN',
'namibia dollar': 'NAD',
'naira': 'NGN',
'rwandan franc': 'RWF',
'seychelles rupee': 'SCR',
'somali shilling': 'SOS',
'rand': 'ZAR',
'sudanese pound': 'SDG',
'swazi lilangeni': 'SZL',
'tanzanian shilling': 'TZS',
'tunisian dinar': 'TND',
'ugandan shilling': 'UGX',
'zambian kwacha': 'ZMK',
'zimbabwean dollar': 'ZWL',
'dollar': 'USD',
'euro': 'EUR',
'afghani': 'AFN',
'dram': 'AMD',
'bdt': 'BDT',
'taaka': 'BDT',
'dinar': ['IQD', 'JOD'],
'kwanza': 'AOA',
'ngultrum': 'BTN',
'ruble': 'BYR',
'indian rupee': 'INR',
'sri lankan rupee': 'LKR',
'pakistani rupee': 'PKR'
}
def convert_name_to_code(name):
"""Convert currency name to ISO code."""
return currency_map.get(name.lower(), name.upper())
def fetch_exchange_rate(input_text):
"""Fetch and calculate the exchange rate based on input."""
try:
# Normalize the input to use "into" for splitting
normalized_input = input_text.lower().replace(' to ', ' into ')
# Extract the amount if present
parts = normalized_input.split(' into ')
if len(parts) != 2:
return "Invalid input format. Please use 'amount currency_from into currency_to'."
amount_currency_from, currency_to = parts
# Split the amount and currency code
amount, currency_from = amount_currency_from.strip().split(maxsplit=1)
amount = float(amount) # Convert amount to float
currency_from_code = convert_name_to_code(currency_from.strip())
currency_to_code = convert_name_to_code(currency_to.strip())
response = requests.get(base_url)
if response.status_code == 200:
data = response.json()
rates = data.get('conversion_rates', {})
rate_from = rates.get(currency_from_code)
rate_to = rates.get(currency_to_code)
if rate_from and rate_to:
# Calculate the conversion rate for the given amount
conversion_rate = (rate_to / rate_from) * amount
return f"{amount} {currency_from_code.upper()} is approximately {conversion_rate:.4f} {currency_to_code.upper()}."
else:
return f"Currency code '{currency_from_code}' or '{currency_to_code}' not found."
else:
return f"Failed to fetch exchange rates: {response.status_code} {response.text}."
except ValueError:
return "Invalid input format. Please use 'amount currency_from into currency_to'."
except Exception as e:
return f"An error occurred: {str(e)}."
def banking_service_query(query):
"""Provide responses for general banking service queries."""
query = query.lower()
responses = {
"opening account": "To open an account in the UAE, you generally need a valid Emirates ID, a passport, and proof of residence.",
"loan services": "Banks in the UAE offer various types of loans including personal loans, home loans, and car loans.",
"home loan": "For a home loan, you need a steady source of income, credit history, and a down payment.",
# Add more responses as needed
}
for key in responses:
if key in query:
return responses[key]
return "Sorry, I could not find an answer to your banking query. Please try rephrasing your question."
def process_query(input_text):
"""Determine if the query is about currency conversion or banking services."""
if " into " in input_text.lower() or " to " in input_text.lower():
return fetch_exchange_rate(input_text)
else:
return banking_service_query(input_text)
# Create Gradio interface
demo = gr.Interface(
fn=process_query,
inputs=gr.Textbox(label="Enter your query"),
outputs="text",
title="Currency Converter",
description="Enter currency conversion requests (e.g., '2000 AED into INR') or banking service queries."
)
if __name__ == "__main__":
print("Launching Gradio Interface...")
demo.launch() |