Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import re
|
| 3 |
import torch
|
| 4 |
import gradio as gr
|
| 5 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
|
@@ -9,43 +7,25 @@ model_name = "./t5-finetuned-final"
|
|
| 9 |
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 10 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 11 |
|
| 12 |
-
# Move model to CPU
|
| 13 |
device = torch.device("cpu")
|
| 14 |
model.to(device)
|
| 15 |
|
| 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 |
-
"""
|
| 33 |
-
amount = extract_amount(input_command) # Extract amount
|
| 34 |
-
|
| 35 |
input_ids = tokenizer("extract: " + input_command, return_tensors="pt").input_ids.to(device)
|
| 36 |
-
|
| 37 |
-
output_ids = model.generate(input_ids, max_length=128, num_beams=1, early_stopping=True) #
|
| 38 |
model_output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
return result
|
| 42 |
|
| 43 |
iface = gr.Interface(
|
| 44 |
fn=generate_command,
|
| 45 |
inputs=gr.Textbox(lines=2, placeholder="Enter a command..."),
|
| 46 |
-
outputs=gr.Textbox(label="
|
| 47 |
-
title="T5
|
| 48 |
-
description="
|
| 49 |
)
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
|
|
|
| 7 |
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 8 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 9 |
|
| 10 |
+
# Move model to CPU
|
| 11 |
device = torch.device("cpu")
|
| 12 |
model.to(device)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def generate_command(input_command):
|
| 15 |
+
"""Runs the model and returns its output as plain text."""
|
|
|
|
|
|
|
| 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) # Simple decoding
|
| 19 |
model_output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 20 |
|
| 21 |
+
return model_output # Return the model's raw output
|
|
|
|
| 22 |
|
| 23 |
iface = gr.Interface(
|
| 24 |
fn=generate_command,
|
| 25 |
inputs=gr.Textbox(lines=2, placeholder="Enter a command..."),
|
| 26 |
+
outputs=gr.Textbox(label="Model Output"),
|
| 27 |
+
title="T5 Model Output Test",
|
| 28 |
+
description="This version only shows the model's raw output."
|
| 29 |
)
|
| 30 |
|
| 31 |
if __name__ == "__main__":
|