RayBe commited on
Commit
46e299f
·
verified ·
1 Parent(s): b68b360

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -7
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import re
 
2
  import torch
3
  import gradio as gr
4
  from transformers import T5Tokenizer, T5ForConditionalGeneration
@@ -17,6 +18,13 @@ def extract_amount(input_text):
17
  match = re.search(r'\b\d{1,15}([.,]\d{1,15})?\b', input_text)
18
  return match.group(0) if match else "Not found" # Keep as string
19
 
 
 
 
 
 
 
 
20
  def generate_command(input_command):
21
  """Runs the model and extracts information."""
22
  input_ids = tokenizer("extract: " + input_command, return_tensors="pt").input_ids.to(device)
@@ -27,13 +35,13 @@ def generate_command(input_command):
27
  # Extract amount manually
28
  extracted_amount = extract_amount(input_command)
29
 
30
- try:
31
- model_data = eval(model_output) # Convert model output into a Python dictionary
32
- action = model_data.get("action", "Not found")
33
- currency = model_data.get("currency", "Not found")
34
- recipient = model_data.get("recipient", "Not found")
35
- except:
36
- action, currency, recipient = "Error", "Error", "Error"
37
 
38
  return extracted_amount, action, currency, recipient
39
 
 
1
  import re
2
+ import json
3
  import torch
4
  import gradio as gr
5
  from transformers import T5Tokenizer, T5ForConditionalGeneration
 
18
  match = re.search(r'\b\d{1,15}([.,]\d{1,15})?\b', input_text)
19
  return match.group(0) if match else "Not found" # Keep as string
20
 
21
+ def parse_model_output(model_output):
22
+ """Attempts to parse the model's output into a dictionary."""
23
+ try:
24
+ return json.loads(model_output) # Safely parse JSON
25
+ except json.JSONDecodeError:
26
+ return {} # Return an empty dictionary if parsing fails
27
+
28
  def generate_command(input_command):
29
  """Runs the model and extracts information."""
30
  input_ids = tokenizer("extract: " + input_command, return_tensors="pt").input_ids.to(device)
 
35
  # Extract amount manually
36
  extracted_amount = extract_amount(input_command)
37
 
38
+ # Parse the model output safely
39
+ model_data = parse_model_output(model_output)
40
+
41
+ # Extract required variables
42
+ action = model_data.get("action", "Not found")
43
+ currency = model_data.get("currency", "Not found")
44
+ recipient = model_data.get("recipient", "Not found")
45
 
46
  return extracted_amount, action, currency, recipient
47