Spaces:
Sleeping
Sleeping
File size: 2,123 Bytes
b68b360 74e1a78 8920931 a09d2a8 8920931 2617109 4d4ccb0 8920931 b68b360 4963321 8920931 4963321 ded2ee6 b68b360 5ca9d2a ded2ee6 b68b360 4963321 b68b360 8920931 b68b360 062413a 4963321 b68b360 062413a 4963321 8920931 56991a9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import re
import torch
import gradio as gr
from transformers import T5Tokenizer, T5ForConditionalGeneration
# Load the fine-tuned model
model_name = "./t5-finetuned-final"
tokenizer = T5Tokenizer.from_pretrained(model_name)
model = T5ForConditionalGeneration.from_pretrained(model_name)
# Move model to CPU
device = torch.device("cpu")
model.to(device)
def extract_amount(input_text):
"""Finds the first number in the text (supports both . and , as decimal separators)."""
match = re.search(r'\b\d{1,15}([.,]\d{1,15})?\b', input_text)
return match.group(0) if match else "Not found" # Keep as string
def extract_variable(text, key):
"""Extracts a value following a key in the model output."""
match = re.search(rf'"{key}"\s*:\s*"([^"]+)"', text) # Matches "key": "value"
return match.group(1) if match else "Not found"
def generate_command(input_command):
"""Runs the model, extracts amount, and processes output variables."""
input_ids = tokenizer("extract: " + input_command, return_tensors="pt").input_ids.to(device)
output_ids = model.generate(input_ids, max_length=128, num_beams=1, early_stopping=True)
model_output = tokenizer.decode(output_ids[0], skip_special_tokens=True).strip()
# Extract amount manually
extracted_amount = extract_amount(input_command)
# Extract the rest of the values from the raw model output
action = extract_variable(model_output, "action")
currency = extract_variable(model_output, "currency")
recipient = extract_variable(model_output, "recipient")
return extracted_amount, action, currency, recipient
# Gradio Interface
iface = gr.Interface(
fn=generate_command,
inputs=gr.Textbox(lines=2, placeholder="Enter a command..."),
outputs=[
gr.Textbox(label="Amount"),
gr.Textbox(label="Action"),
gr.Textbox(label="Currency"),
gr.Textbox(label="Recipient"),
],
title="Intent Recognition Project",
description="Extracts amount separately and displays action, currency, and recipient from model output."
)
if __name__ == "__main__":
iface.launch()
|