Spaces:
Sleeping
Sleeping
Update app.py
Browse files18 unmodified lines1920212223246 unmodified lines313233343536373839404142434445464748495051525354555657585960616263646566676869705 unmodified lines767778798081821 unmodified line8485868788899018 unmodified linesexcept:passdef fix_json_output(output):"""Fixes common JSON formatting issues in the model's output.6 unmodified linesoutput = re.sub(r':\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*([,}])', r':"\1"\2', output)return outputdef correct_amount_format(output):"""Corrects amount formatting in the JSON output."""try:# Fix JSON formatting issuesoutput = fix_json_output(output)data = json.loads(output)def correct_value(value):if isinstance(value, str):# Fix amounts with multiple decimal points (e.g., 3140.98.0 → 3140.98)if re.match(r'^\d+\.\d+\.\d+$', value):value = value.split('.')[0] + '.' + value.split('.')[1]# Remove trailing .0 if it's not part of the original amountif re.match(r'^\d+\.0$', value):value = value.split('.')[0]return value# Correct each value in the JSON dataif isinstance(data, dict):for key in data:data[key] = correct_value(data[key])elif isinstance(data, list):for i in range(len(data)):data[i] = correct_value(data[i])return json.dumps(data, ensure_ascii=False)except json.JSONDecodeError:# Fallback for invalid JSON: basic correctionsoutput = re.sub(r'(\d+),(\d+)\b', r'\1.\2', output)return outputdef generate_command(input_command):prompt = "extract: " + input_commandinput_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)5 unmodified lines)result = tokenizer.decode(output_ids[0], skip_special_tokens=True)result = correct_amount_format(result)return resultiface = gr.Interface(1 unmodified lineinputs=gr.Textbox(lines=2, placeholder="Enter a command..."),outputs=gr.Textbox(label="Extracted JSON Output"),title="T5 Fine-Tuned Command Extractor",description="Extracts details in JSON format with exact amount preservation.",)if __name__ == "__main__":18 unmodified lines192021222324252627282930313233346 unmodified lines41424344454647484950515253545556575859606162636465666768695 unmodified lines75767778798081828384851 unmodified line8788899091929318 unmodified linesexcept:passdef extract_amount(input_text):"""Extracts the amount from the input text using a robust regex."""# Regex to match any valid number (integers or decimals)amount_match = re.search(r'\d+(?:\.\d+)?', input_text)if amount_match:return amount_match.group(0)return Nonedef fix_json_output(output):"""Fixes common JSON formatting issues in the model's output.6 unmodified linesoutput = re.sub(r':\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*([,}])', r':"\1"\2', output)return outputdef replace_amount_in_json(output, amount):"""Replaces the amount in the model's JSON output with the extracted amount."""try:# Fix JSON formatting issuesoutput = fix_json_output(output)data = json.loads(output)# Replace the amount field if it existsif "amount" in data:data["amount"] = float(amount) if '.' in amount else int(amount)return json.dumps(data, ensure_ascii=False)except json.JSONDecodeError:# Fallback for invalid JSON: return the original outputreturn outputdef generate_command(input_command):# Extract the amount from the inputamount = extract_amount(input_command)# Generate the JSON output using the modelprompt = "extract: " + input_commandinput_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)5 unmodified lines)result = tokenizer.decode(output_ids[0], skip_special_tokens=True)# Replace the model's amount with the extracted amountif amount:result = replace_amount_in_json(result, amount)return resultiface = gr.Interface(1 unmodified lineinputs=gr.Textbox(lines=2, placeholder="Enter a command..."),outputs=gr.Textbox(label="Extracted JSON Output"),title="T5 Fine-Tuned Command Extractor",description="Extracts details in JSON format and replaces the amount with the exact value from the input.",)if __name__ == "__main__":
app.py+26-23