Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
|
@@ -11,23 +12,44 @@ model = T5ForConditionalGeneration.from_pretrained(model_name)
|
|
| 11 |
device = torch.device("cpu")
|
| 12 |
model.to(device)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def generate_command(input_command):
|
| 15 |
-
"""Runs the model and
|
| 16 |
input_ids = tokenizer("extract: " + input_command, return_tensors="pt").input_ids.to(device)
|
| 17 |
-
|
| 18 |
-
output_ids = model.generate(input_ids, max_length=128, num_beams=1, early_stopping=True)
|
| 19 |
model_output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
iface = gr.Interface(
|
| 24 |
fn=generate_command,
|
| 25 |
inputs=gr.Textbox(lines=2, placeholder="Enter a command..."),
|
| 26 |
-
outputs=
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
)
|
| 30 |
|
| 31 |
if __name__ == "__main__":
|
| 32 |
iface.launch()
|
| 33 |
-
|
|
|
|
| 1 |
+
import re
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
|
|
|
| 12 |
device = torch.device("cpu")
|
| 13 |
model.to(device)
|
| 14 |
|
| 15 |
+
def extract_amount(input_text):
|
| 16 |
+
"""Finds the first number in the text (supports both . and , as decimal separators)."""
|
| 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)
|
| 23 |
+
|
| 24 |
+
output_ids = model.generate(input_ids, max_length=128, num_beams=1, early_stopping=True)
|
| 25 |
model_output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 26 |
|
| 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 |
+
|
| 40 |
+
# Gradio Interface
|
| 41 |
iface = gr.Interface(
|
| 42 |
fn=generate_command,
|
| 43 |
inputs=gr.Textbox(lines=2, placeholder="Enter a command..."),
|
| 44 |
+
outputs=[
|
| 45 |
+
gr.Textbox(label="Extracted Amount"),
|
| 46 |
+
gr.Textbox(label="Action"),
|
| 47 |
+
gr.Textbox(label="Currency"),
|
| 48 |
+
gr.Textbox(label="Recipient"),
|
| 49 |
+
],
|
| 50 |
+
title="Command Extractor",
|
| 51 |
+
description="Extracts amount separately and displays action, currency, and recipient from model output."
|
| 52 |
)
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
| 55 |
iface.launch()
|
|
|