RayBe commited on
Commit
3b07ddc
·
verified ·
1 Parent(s): 91577e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -10
app.py CHANGED
@@ -16,23 +16,17 @@ model.to(device)
16
  def extract_amount(input_text):
17
  """Extracts the first numeric value (with decimals) from the input text."""
18
  match = re.search(r'\b(\d+(?:\.\d+)?)\b', input_text)
19
- return match.group(1) if match else None
20
 
21
  def merge_json_with_amount(model_output, amount):
22
  """Ensures the extracted amount is inserted into the model's output JSON."""
23
  try:
24
  data = json.loads(model_output) # Try parsing the model output
 
 
 
25
  except json.JSONDecodeError:
26
  return model_output # Return raw model output if it's not valid JSON
27
- data["amount"] = amount
28
-
29
- # if amount:
30
- # try:
31
- # data["amount"] = float(amount) # Convert to float
32
- # except ValueError:
33
- # data["amount"] = amount # Keep as string if conversion fails
34
-
35
- return json.dumps(data, ensure_ascii=False)
36
 
37
  def generate_command(input_command):
38
  """Processes the input, extracts the amount manually, and runs the model."""
@@ -56,3 +50,4 @@ iface = gr.Interface(
56
 
57
  if __name__ == "__main__":
58
  iface.launch()
 
 
16
  def extract_amount(input_text):
17
  """Extracts the first numeric value (with decimals) from the input text."""
18
  match = re.search(r'\b(\d+(?:\.\d+)?)\b', input_text)
19
+ return match.group(1) if match else None # Keep as string
20
 
21
  def merge_json_with_amount(model_output, amount):
22
  """Ensures the extracted amount is inserted into the model's output JSON."""
23
  try:
24
  data = json.loads(model_output) # Try parsing the model output
25
+ if amount is not None:
26
+ data["amount"] = amount # Replace model's amount with extracted amount (as string)
27
+ return json.dumps(data, ensure_ascii=False)
28
  except json.JSONDecodeError:
29
  return model_output # Return raw model output if it's not valid JSON
 
 
 
 
 
 
 
 
 
30
 
31
  def generate_command(input_command):
32
  """Processes the input, extracts the amount manually, and runs the model."""
 
50
 
51
  if __name__ == "__main__":
52
  iface.launch()
53
+