Guhanselvam commited on
Commit
91804c1
·
verified ·
1 Parent(s): 39a0134

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -1
app.py CHANGED
@@ -92,13 +92,42 @@ def fetch_exchange_rate(input_text):
92
  currency_from_code = convert_name_to_code(currency_from)
93
  currency_to_code = convert_name_to_code(currency_to)
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  # Fetch exchange rates
96
  response = requests.get(base_url)
97
  if response.status_code == 200:
98
  data = response.json()
99
  rates = data.get('conversion_rates', {})
100
 
101
- rate_to = rates.get(currency_to_code)
 
102
 
103
  if rate_from and rate_to:
104
  # Calculate the conversion rate for the given amount
@@ -108,6 +137,7 @@ def fetch_exchange_rate(input_text):
108
  return f"Currency code '{currency_from_code}' or '{currency_to_code}' not found."
109
  else:
110
  return f"Failed to fetch exchange rates: {response.status_code} {response.text}."
 
111
  except ValueError:
112
  return "Invalid input format. Please use 'amount currency_from into currency_to'."
113
  except Exception as e:
 
92
  currency_from_code = convert_name_to_code(currency_from)
93
  currency_to_code = convert_name_to_code(currency_to)
94
 
95
+ f fetch_exchange_rate(input_text):
96
+ """Fetch and calculate the exchange rate based on input."""
97
+ try:
98
+ # Normalize the input to make it uniform
99
+ normalized_input = input_text.lower().replace(' to ', ' into ').replace(' ', '')
100
+
101
+ # If it contains an amount, it needs to be treated accordingly
102
+ if 'into' in normalized_input:
103
+ amount_currency_from, currency_to = normalized_input.split('into')
104
+ if amount_currency_from.isnumeric():
105
+ amount = float(amount_currency_from)
106
+ currency_from = currency_to.strip()
107
+ currency_to = ""
108
+ else:
109
+ amount_parts = amount_currency_from.split(maxsplit=1)
110
+ if len(amount_parts) != 2:
111
+ return "Invalid input format. Please use 'amount currency_from into currency_to'."
112
+ amount = float(amount_parts[0])
113
+ currency_from = amount_parts[1]
114
+ currency_to = currency_to.strip()
115
+ elif 'from' in normalized_input:
116
+ return "Please use 'amount currency_from into currency_to'."
117
+ else:
118
+ return "Invalid input format. Please use 'amount currency_from into currency_to'."
119
+
120
+ currency_from_code = convert_name_to_code(currency_from)
121
+ currency_to_code = convert_name_to_code(currency_to)
122
+
123
  # Fetch exchange rates
124
  response = requests.get(base_url)
125
  if response.status_code == 200:
126
  data = response.json()
127
  rates = data.get('conversion_rates', {})
128
 
129
+ rate_from = rates.get(currency_from_code)
130
+ rate_to = rates.get(currency_to_code)
131
 
132
  if rate_from and rate_to:
133
  # Calculate the conversion rate for the given amount
 
137
  return f"Currency code '{currency_from_code}' or '{currency_to_code}' not found."
138
  else:
139
  return f"Failed to fetch exchange rates: {response.status_code} {response.text}."
140
+
141
  except ValueError:
142
  return "Invalid input format. Please use 'amount currency_from into currency_to'."
143
  except Exception as e: