Guhanselvam commited on
Commit
22cc439
·
verified ·
1 Parent(s): 828d02a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -0
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ # Your API key for the exchange rate API
5
+ api_key = '91c5a69825b5e2377b0992cf'
6
+ base_url = f'https://v6.exchangerate-api.com/v6/{api_key}/latest/USD'
7
+
8
+ # Mapping of currency names to their ISO codes
9
+ currency_map = {
10
+ 'algerian dinar': 'DZD',
11
+ 'angolan kwanza': 'AOA',
12
+ 'cfa franc bceao': 'XOF',
13
+ 'botswana pula': 'BWP',
14
+ 'burundian franc': 'BIF',
15
+ 'cfa franc beac': 'XAF',
16
+ 'cape verdean escudo': 'CVE',
17
+ 'comorian franc': 'KMF',
18
+ 'congolese franc': 'CDF',
19
+ 'djiboutian franc': 'DJF',
20
+ 'egypt pound': 'EGP',
21
+ 'nakfa': 'ERN',
22
+ 'ethiopian birr': 'ETB',
23
+ 'dalasi': 'GMD',
24
+ 'ghanaian cedi': 'GHS',
25
+ 'guinean franc': 'GNF',
26
+ 'kenyan shilling': 'KES',
27
+ 'lesotho loti': 'LSL',
28
+ 'liberian dollar': 'LRD',
29
+ 'libyan dinar': 'LYD',
30
+ 'ariary': 'MGA',
31
+ 'malawian kwacha': 'MWK',
32
+ 'mauritian rupee': 'MUR',
33
+ 'moroccan dirham': 'MAD',
34
+ 'mozambique metical': 'MZN',
35
+ 'namibia dollar': 'NAD',
36
+ 'naira': 'NGN',
37
+ 'rwandan franc': 'RWF',
38
+ 'seychelles rupee': 'SCR',
39
+ 'somali shilling': 'SOS',
40
+ 'rand': 'ZAR',
41
+ 'sudanese pound': 'SDG',
42
+ 'swazi lilangeni': 'SZL',
43
+ 'tanzanian shilling': 'TZS',
44
+ 'tunisian dinar': 'TND',
45
+ 'ugandan shilling': 'UGX',
46
+ 'zambian kwacha': 'ZMK',
47
+ 'zimbabwean dollar': 'ZWL',
48
+ 'dollar': 'USD',
49
+ 'euro': 'EUR',
50
+ 'afghani': 'AFN',
51
+ 'dram': 'AMD',
52
+ 'bdt': 'BDT',
53
+ 'taaka': 'BDT',
54
+ 'dinar': ['IQD', 'JOD'],
55
+ 'kwanza': 'AOA',
56
+ 'ngultrum': 'BTN',
57
+ 'ruble': 'BYR',
58
+ 'indian rupee': 'INR',
59
+ 'sri lankan rupee': 'LKR',
60
+ 'pakistani rupee': 'PKR'
61
+ }
62
+
63
+ def convert_name_to_code(name):
64
+ """Convert currency name to ISO code."""
65
+ return currency_map.get(name.lower(), name.upper())
66
+
67
+ def fetch_exchange_rate(input_text):
68
+ """Fetch and calculate the exchange rate based on input."""
69
+ try:
70
+ # Normalize the input to use "into" for splitting
71
+ normalized_input = input_text.lower().replace(' to ', ' into ')
72
+
73
+ # Extract the amount if present
74
+ parts = normalized_input.split(' into ')
75
+ if len(parts) != 2:
76
+ return "Invalid input format. Please use 'amount currency_from into currency_to'."
77
+
78
+ amount_currency_from, currency_to = parts
79
+ # Split the amount and currency code
80
+ amount, currency_from = amount_currency_from.strip().split(maxsplit=1)
81
+ amount = float(amount) # Convert amount to float
82
+
83
+ currency_from_code = convert_name_to_code(currency_from.strip())
84
+ currency_to_code = convert_name_to_code(currency_to.strip())
85
+
86
+ response = requests.get(base_url)
87
+ if response.status_code == 200:
88
+ data = response.json()
89
+ rates = data.get('conversion_rates', {})
90
+
91
+ rate_from = rates.get(currency_from_code)
92
+ rate_to = rates.get(currency_to_code)
93
+
94
+ if rate_from and rate_to:
95
+ # Calculate the conversion rate for the given amount
96
+ conversion_rate = (rate_to / rate_from) * amount
97
+ return f"{amount} {currency_from_code.upper()} is approximately {conversion_rate:.4f} {currency_to_code.upper()}."
98
+ else:
99
+ return f"Currency code '{currency_from_code}' or '{currency_to_code}' not found."
100
+ else:
101
+ return f"Failed to fetch exchange rates: {response.status_code} {response.text}."
102
+ except ValueError:
103
+ return "Invalid input format. Please use 'amount currency_from into currency_to'."
104
+ except Exception as e:
105
+ return f"An error occurred: {str(e)}."
106
+
107
+ def banking_service_query(query):
108
+ """Provide responses for general banking service queries."""
109
+ query = query.lower()
110
+ responses = {
111
+ "opening account": "To open an account in the UAE, you generally need a valid Emirates ID, a passport, and proof of residence.",
112
+ "loan services": "Banks in the UAE offer various types of loans including personal loans, home loans, and car loans.",
113
+ "home loan": "For a home loan, you need a steady source of income, credit history, and a down payment.",
114
+ # Add more responses as needed
115
+ }
116
+
117
+ for key in responses:
118
+ if key in query:
119
+ return responses[key]
120
+
121
+ return "Sorry, I could not find an answer to your banking query. Please try rephrasing your question."
122
+
123
+ def process_query(input_text):
124
+ """Determine if the query is about currency conversion or banking services."""
125
+ if " into " in input_text.lower() or " to " in input_text.lower():
126
+ return fetch_exchange_rate(input_text)
127
+ else:
128
+ return banking_service_query(input_text)
129
+
130
+ # Create Gradio interface
131
+ demo = gr.Interface(
132
+ fn=process_query,
133
+ inputs=gr.Textbox(label="Enter your query"),
134
+ outputs="text",
135
+ title="Currency Converter",
136
+ description="Enter currency conversion requests (e.g., '2000 AED into INR') or banking service queries."
137
+ )
138
+
139
+ if __name__ == "__main__":
140
+ print("Launching Gradio Interface...")
141
+ demo.launch()