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 make it uniform normalized_input = input_text.lower().replace(' to ', ' into ').replace(' ', '') # If it contains an amount, it needs to be treated accordingly if 'into' in normalized_input: amount_currency_from, currency_to = normalized_input.split('into') if amount_currency_from.isnumeric(): amount = float(amount_currency_from) currency_from = currency_to.strip() currency_to = "" else: amount_parts = amount_currency_from.split(maxsplit=1) if len(amount_parts) != 2: return "Invalid input format. Please use 'amount currency_from into currency_to'." amount = float(amount_parts[0]) currency_from = amount_parts[1] currency_to = currency_to.strip() elif 'from' in normalized_input: return "Please use 'amount currency_from into currency_to'." else: return "Invalid input format. Please use 'amount currency_from into currency_to'." currency_from_code = convert_name_to_code(currency_from) currency_to_code = convert_name_to_code(currency_to) f fetch_exchange_rate(input_text): """Fetch and calculate the exchange rate based on input.""" try: # Normalize the input to make it uniform normalized_input = input_text.lower().replace(' to ', ' into ').replace(' ', '') # If it contains an amount, it needs to be treated accordingly if 'into' in normalized_input: amount_currency_from, currency_to = normalized_input.split('into') if amount_currency_from.isnumeric(): amount = float(amount_currency_from) currency_from = currency_to.strip() currency_to = "" else: amount_parts = amount_currency_from.split(maxsplit=1) if len(amount_parts) != 2: return "Invalid input format. Please use 'amount currency_from into currency_to'." amount = float(amount_parts[0]) currency_from = amount_parts[1] currency_to = currency_to.strip() elif 'from' in normalized_input: return "Please use 'amount currency_from into currency_to'." else: return "Invalid input format. Please use 'amount currency_from into currency_to'." currency_from_code = convert_name_to_code(currency_from) currency_to_code = convert_name_to_code(currency_to) # Fetch exchange rates 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.""" # Normalize input and check for conversion 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', 'AED into INR') or banking service queries." ) if __name__ == "__main__": print("Launching Gradio Interface...") demo.launch()