Guhanselvam commited on
Commit
39a0134
·
verified ·
1 Parent(s): 576ba78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -16
app.py CHANGED
@@ -67,29 +67,38 @@ def convert_name_to_code(name):
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
@@ -100,7 +109,7 @@ def fetch_exchange_rate(input_text):
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
 
@@ -122,6 +131,7 @@ def banking_service_query(query):
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:
@@ -133,7 +143,7 @@ demo = gr.Interface(
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__":
 
67
  def fetch_exchange_rate(input_text):
68
  """Fetch and calculate the exchange rate based on input."""
69
  try:
70
+ # Normalize the input to make it uniform
71
+ normalized_input = input_text.lower().replace(' to ', ' into ').replace(' ', '')
72
 
73
+ # If it contains an amount, it needs to be treated accordingly
74
+ if 'into' in normalized_input:
75
+ amount_currency_from, currency_to = normalized_input.split('into')
76
+ if amount_currency_from.isnumeric():
77
+ amount = float(amount_currency_from)
78
+ currency_from = currency_to.strip()
79
+ currency_to = ""
80
+ else:
81
+ amount_parts = amount_currency_from.split(maxsplit=1)
82
+ if len(amount_parts) != 2:
83
+ return "Invalid input format. Please use 'amount currency_from into currency_to'."
84
+ amount = float(amount_parts[0])
85
+ currency_from = amount_parts[1]
86
+ currency_to = currency_to.strip()
87
+ elif 'from' in normalized_input:
88
+ return "Please use 'amount currency_from into currency_to'."
89
+ else:
90
  return "Invalid input format. Please use 'amount currency_from into currency_to'."
 
 
 
 
 
91
 
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
 
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:
114
  return f"An error occurred: {str(e)}."
115
 
 
131
 
132
  def process_query(input_text):
133
  """Determine if the query is about currency conversion or banking services."""
134
+ # Normalize input and check for conversion
135
  if " into " in input_text.lower() or " to " in input_text.lower():
136
  return fetch_exchange_rate(input_text)
137
  else:
 
143
  inputs=gr.Textbox(label="Enter your query"),
144
  outputs="text",
145
  title="Currency Converter",
146
+ description="Enter currency conversion requests (e.g., '2000 AED into INR', 'AED into INR') or banking service queries."
147
  )
148
 
149
  if __name__ == "__main__":